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

Update the logger creation to override level priorities #129

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .nsprc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"1092330": {
"active": true,
"notes": "The latest version of word-wrap was published 6 years ago",
"expiry": "2023-11-01"
"expiry": "2024-06-01"
}
}
194 changes: 133 additions & 61 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createLogger } from './index'
import TransportStream from 'winston-transport'

describe('logger', () => {
it('verbose logs are screened out when level is debug', () => {
const transport = new InMemoryTransport({})
const logger = createLogger({ loggerOptions: { level: 'debug' }, transports: [transport] })
logger.debug('test a')
logger.verbose('test b')
expect(transport.logs.length).toBe(1)
expect(transport.logs[0].message).toBe('test a')
})

it('debug logs are not screened out when level is verbose', () => {
const transport = new InMemoryTransport({})
const logger = createLogger({ loggerOptions: { level: 'verbose' }, transports: [transport] })
logger.debug('test a')
logger.verbose('test b')
expect(transport.logs.length).toBe(2)
expect(transport.logs[0].message).toBe('test a')
expect(transport.logs[1].message).toBe('test b')
})
})

class InMemoryTransport extends TransportStream {
logs: {
level: string
message: string
}[]

constructor(opts: TransportStream.TransportStreamOptions) {
super(opts)
this.logs = []
}

//eslint-disable-next-line @typescript-eslint/no-explicit-any
log(info: any, next: () => void): void {
const { level, message } = info
this.logs.push({ level, message })
next()
}

close(): void {}
}
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@ export type Logger = Pick<
| 'log'
>

// winstonjs' default levels have debug and verbose reversed, which is confusing and causes filtering issues with Seq,
// CloudWatch etc (given they assume Verbose/Trace should be the lowest/noisiest log level)
const levels = {
error: 0,
warn: 1,
info: 3,
debug: 4,
verbose: 5,
}

export interface CreateLoggerOptions {
consoleFormat?: 'pretty' | 'json'
transports?: Transport[]
consoleOptions?: Omit<ConsoleTransportOptions, 'format'>
consoleFormats?: Format[]
omitPaths?: string[]
loggerOptions?: LoggerOptions
loggerOptions?: Omit<LoggerOptions, 'transports'>
}

export const createConsoleTransport = ({ consoleFormat = 'json', consoleOptions, consoleFormats, omitPaths }: CreateLoggerOptions) => {
Expand Down Expand Up @@ -61,6 +71,7 @@ export const createLogger = (options: CreateLoggerOptions): Logger => {

// create logger options using supplied options + transports
const loggerOptions: LoggerOptions = {
levels, // loggerOptions should still be allowed to override our defaults
...options.loggerOptions,
transports,
}
Expand Down
Loading