-
Notifications
You must be signed in to change notification settings - Fork 18
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
feat: add system logger #457
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
753a7da
feat: implement first go at system logger
Skn0tt 32c4629
chore: fix eslint
Skn0tt f985e05
chore: prettier
Skn0tt 0c0edcc
fix: skip request usage on v14
Skn0tt 5d27881
feat: remove sampling
Skn0tt 54badea
feat: remove log levels
Skn0tt 810112e
feat: remove withRequest
Skn0tt 463b687
chore: prettier
Skn0tt 139158e
fix: remove `exports`
Skn0tt 1ac7353
refactor: make class members explicit
Skn0tt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// this file is exported as @netlify/functions/internal, | ||
// and only meant for consumption by Netlify Teams. | ||
// While we try to adhere to semver, this file is not considered part of the public API. | ||
|
||
export { systemLogger, LogLevel } from './lib/system_logger.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const systemLogTag = '__nfSystemLog' | ||
|
||
const serializeError = (error: Error): Record<string, unknown> => { | ||
const cause = error?.cause instanceof Error ? serializeError(error.cause) : error.cause | ||
|
||
return { | ||
error: error.message, | ||
error_cause: cause, | ||
error_stack: error.stack, | ||
} | ||
} | ||
|
||
// eslint pretends there's a different enum at the same place - it's wrong! | ||
// eslint-disable-next-line no-shadow | ||
export enum LogLevel { | ||
Debug = 1, | ||
Log, | ||
Error, | ||
} | ||
|
||
class SystemLogger { | ||
private readonly fields: Record<string, unknown> | ||
private readonly logLevel: LogLevel | ||
|
||
constructor(fields: Record<string, unknown> = {}, logLevel = LogLevel.Log) { | ||
this.fields = fields | ||
this.logLevel = logLevel | ||
} | ||
|
||
private doLog(logger: typeof console.log, message: string) { | ||
logger(systemLogTag, JSON.stringify({ msg: message, fields: this.fields })) | ||
} | ||
|
||
log(message: string) { | ||
if (this.logLevel > LogLevel.Log) { | ||
return | ||
} | ||
|
||
this.doLog(console.log, message) | ||
} | ||
|
||
debug(message: string) { | ||
if (this.logLevel > LogLevel.Debug) { | ||
return | ||
} | ||
|
||
this.doLog(console.debug, message) | ||
} | ||
|
||
error(message: string) { | ||
if (this.logLevel > LogLevel.Error) { | ||
return | ||
} | ||
|
||
this.doLog(console.error, message) | ||
} | ||
|
||
withLogLevel(level: LogLevel) { | ||
return new SystemLogger(this.fields, level) | ||
} | ||
|
||
withFields(fields: Record<string, unknown>) { | ||
return new SystemLogger( | ||
{ | ||
...this.fields, | ||
...fields, | ||
}, | ||
this.logLevel, | ||
) | ||
} | ||
|
||
withError(error: unknown) { | ||
const fields = error instanceof Error ? serializeError(error) : { error } | ||
|
||
return this.withFields(fields) | ||
} | ||
} | ||
|
||
export const systemLogger = new SystemLogger() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const test = require('ava') | ||
|
||
const { systemLogger, LogLevel } = require('../../dist/internal') | ||
|
||
test('Log Level', (t) => { | ||
const originalDebug = console.debug | ||
|
||
const debugLogs = [] | ||
console.debug = (...message) => debugLogs.push(message) | ||
|
||
systemLogger.debug('hello!') | ||
t.is(debugLogs.length, 0) | ||
|
||
systemLogger.withLogLevel(LogLevel.Debug).debug('hello!') | ||
t.is(debugLogs.length, 1) | ||
|
||
systemLogger.withLogLevel(LogLevel.Log).debug('hello!') | ||
t.is(debugLogs.length, 1) | ||
|
||
console.debug = originalDebug | ||
}) | ||
|
||
test('Fields', (t) => { | ||
const originalLog = console.log | ||
const logs = [] | ||
console.log = (...message) => logs.push(message) | ||
systemLogger.withError(new Error('boom')).withFields({ foo: 'bar' }).log('hello!') | ||
t.is(logs.length, 1) | ||
t.is(logs[0][0], '__nfSystemLog') | ||
const log = JSON.parse(logs[0][1]) | ||
t.is(log.msg, 'hello!') | ||
t.is(log.fields.foo, 'bar') | ||
t.is(log.fields.error, 'boom') | ||
t.is(log.fields.error_stack.split('\n').length > 2, true) | ||
|
||
console.log = originalLog | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The edge functions logger has a simpler "Debug/Log/Error" hierarchy here. Since log levels were introduced there only very recently, we can still change what's being used here. I picked those levels because they match the Go levels. @eduardoboucas do you see any value in having an additional
Warn
level and using theInfo
name? Should we change this to beDebug/Log/Error
as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No strong opinions. I personally don't feel the need for extra levels, and it's not clear to me when we'd use
warn
vs.info
.If we do it, do we make
log()
an alias ofinfo()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then let's stick with Debug/Log/Error for now - we can always add the additional levels in the future.
That's the current impl, yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, missed that part!