Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(app-shell,app-shell-odd): update winston and improve logging #16516

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions app-shell-odd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
},
"homepage": "https://github.com/Opentrons/opentrons",
"workspaces": {
"nohoist": [
"**"
]
"nohoist": ["**"]
},
"devDependencies": {
"@opentrons/app": "link:../app"
Expand Down Expand Up @@ -58,7 +56,7 @@
"semver": "5.7.2",
"tempy": "1.0.1",
"uuid": "3.2.1",
"winston": "3.1.0",
"winston": "3.15.0",
"yargs-parser": "13.1.2"
}
}
111 changes: 43 additions & 68 deletions app-shell-odd/src/log.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// create logger function
import { inspect } from 'util'
import fse from 'fs-extra'
import path from 'path'
import dateFormat from 'dateformat'
import winston from 'winston'

import { getConfig } from './config'

import type Transport from 'winston-transport'
import Transport from 'winston-transport'

Check failure on line 9 in app-shell-odd/src/log.ts

View workflow job for this annotation

GitHub Actions / js checks

All imports in the declaration are only used as types. Use `import type`

Check failure on line 9 in app-shell-odd/src/log.ts

View workflow job for this annotation

GitHub Actions / js checks

All imports in the declaration are only used as types. Use `import type`
import type { Config } from './config'

const ODD_DIR = '/data/ODD'
const LOG_DIR = path.join(ODD_DIR, 'logs')
const ERROR_LOG = path.join(LOG_DIR, 'error.log')
const COMBINED_LOG = path.join(LOG_DIR, 'combined.log')
const FILE_OPTIONS = {

Check failure on line 16 in app-shell-odd/src/log.ts

View workflow job for this annotation

GitHub Actions / js checks

'FILE_OPTIONS' is assigned a value but never used

Check failure on line 16 in app-shell-odd/src/log.ts

View workflow job for this annotation

GitHub Actions / js checks

'FILE_OPTIONS' is assigned a value but never used
// JSON logs
format: winston.format.json(),
// 1 MB max log file size (to ensure emailablity)
Expand All @@ -25,66 +24,61 @@
tailable: true,
}

let config: Config['log']
let transports: Transport[]
let log: winston.Logger
// Use our own logger type because winston (a) doesn't allow these by default
// but (b) does it by binding something other than a function to these props.
export type OTLogger = Omit<
winston.Logger,
'emerg' | 'alert' | 'crit' | 'warning' | 'notice'
>

export function createLogger(filename: string): winston.Logger {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!config) config = getConfig('log')
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!transports) initializeTransports()

return createWinstonLogger(filename)
export function createLogger(label: string): OTLogger {
const rootLogger = ensureRootLogger()
return rootLogger.child({ label })
}

function initializeTransports(): void {
let error = null
let _rootLog: OTLogger | null = null

// sync is ok here because this only happens once
try {
fse.ensureDirSync(LOG_DIR)
} catch (e: unknown) {
error = e
function ensureRootLogger(): OTLogger {
if (_rootLog == null) {
return buildRootLogger()
} else {
return _rootLog
}
}

transports = createTransports()
log = createWinstonLogger('log')
function buildRootLogger(): OTLogger {
const config = getConfig('log')

// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (error) log.error('Could not create log directory', { error })
log.info(`Level "error" and higher logging to ${ERROR_LOG}`)
log.info(`Level "${config.level.file}" and higher logging to ${COMBINED_LOG}`)
log.info(`Level "${config.level.console}" and higher logging to console`)
const transports = createTransports(config)

const formats = [
winston.format.timestamp(),
winston.format.metadata({
key: 'meta',
fillExcept: ['level', 'message', 'timestamp', 'label'],
}),
]

_rootLog = winston.createLogger({
transports,
format: winston.format.combine(...formats),
})
const loggingLog = _rootLog.child({ label: 'logging' })
loggingLog.info(`Level "error" and higher logging to ${ERROR_LOG}`)
loggingLog.info(
`Level "${config.level.file}" and higher logging to ${COMBINED_LOG}`
)
loggingLog.info(
`Level "${config.level.console}" and higher logging to console`
)
return _rootLog
}

function createTransports(): Transport[] {
function createTransports(config: Config['log']): Transport[] {
const timeFromStamp = (ts: string): string =>
dateFormat(new Date(ts), 'HH:MM:ss.l')

return [
// error file log
new winston.transports.File(
Object.assign(
{
level: 'error',
filename: ERROR_LOG,
},
FILE_OPTIONS
)
),

// regular combined file log
new winston.transports.File(
Object.assign(
{
level: config.level.file,
filename: COMBINED_LOG,
},
FILE_OPTIONS
)
),

// console log
new winston.transports.Console({
level: config.level.console,
Expand All @@ -105,22 +99,3 @@
}),
]
}

function createWinstonLogger(label: string): winston.Logger {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/prefer-optional-chain
log && log.debug(`Creating logger for ${label}`)

const formats = [
winston.format.label({ label }),
winston.format.timestamp(),
winston.format.metadata({
key: 'meta',
fillExcept: ['level', 'message', 'timestamp', 'label'],
}),
]

return winston.createLogger({
transports,
format: winston.format.combine(...formats),
})
}
6 changes: 2 additions & 4 deletions app-shell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
},
"homepage": "https://github.com/Opentrons/opentrons",
"workspaces": {
"nohoist": [
"**"
]
"nohoist": ["**"]
},
"devDependencies": {
"electron-notarize": "^1.2.1",
Expand Down Expand Up @@ -68,7 +66,7 @@
"tempy": "1.0.1",
"usb": "^2.11.0",
"uuid": "3.2.1",
"winston": "3.1.0",
"winston": "3.15.0",
"yargs-parser": "13.1.2"
}
}
Loading
Loading