-
Notifications
You must be signed in to change notification settings - Fork 281
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 logger structure #981
Changes from 22 commits
50371b8
6712c81
087b4eb
e5dc894
a948c6e
b777f12
78c02e5
107bca2
8aaad22
d41d3d6
e1ce814
3ce4d35
88a2db9
3134919
53d9f25
c79b852
6d5df57
beef823
223100d
40953cf
7e63215
7f7e309
ba57c93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
// lib/logger.ts | ||
|
||
import type { Tracer } from 'dd-trace'; | ||
|
||
type LogLevel = 'info' | 'warn' | 'error' | 'debug' | 'verbose'; | ||
|
||
type LoggerOptions = { | ||
service: string; | ||
}; | ||
|
||
let ddTrace: Tracer | undefined; | ||
|
||
class CustomLogger { | ||
private static instance: CustomLogger; | ||
|
||
|
@@ -20,16 +26,41 @@ class CustomLogger { | |
return CustomLogger.instance; | ||
} | ||
|
||
private log(level: LogLevel, message: string, meta?: Record<string, unknown>) { | ||
private createDatadogLog(level: LogLevel, message: string, meta?: Record<string, unknown>) { | ||
let traceId: string | undefined; | ||
let spanId: string | undefined; | ||
|
||
//TODO: initialice ddTrace through dd-tracer | ||
if (ddTrace) { | ||
// Access trace information server-side | ||
const currentSpan = ddTrace.scope().active(); | ||
traceId = currentSpan?.context().toTraceId() ?? undefined; | ||
spanId = currentSpan?.context().toSpanId() ?? undefined; | ||
} | ||
|
||
const logEntry = { | ||
message: `[${this.service}] ${message}`, | ||
level, | ||
dd: { | ||
trace_id: traceId, | ||
span_id: spanId, | ||
}, | ||
...meta, | ||
}; | ||
|
||
if (level === 'debug' || level === 'verbose' || level === 'info') { | ||
console.log(`[${this.service}] ${message}`, meta); | ||
console.log(JSON.stringify(logEntry)); | ||
} else if (typeof console[level] === 'function') { | ||
console[level](`[${this.service}] ${message}`, meta); | ||
console[level](JSON.stringify(logEntry)); | ||
} else { | ||
console.log(`[${this.service}] ${message}`, meta); | ||
console.log(JSON.stringify(logEntry)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we not want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. otherwise can we structure this as
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using the previous condition for that. there is a warn and error function so it will be going to that. This else in particular is kind of overkill, everything should fall between the first other 2 conditions. |
||
} | ||
} | ||
|
||
private log(level: LogLevel, message: string, meta?: Record<string, unknown>) { | ||
this.createDatadogLog(level, message, meta); | ||
} | ||
|
||
public info(message: string, meta?: Record<string, unknown>) { | ||
this.log('info', message, meta); | ||
} | ||
|
@@ -39,19 +70,18 @@ class CustomLogger { | |
} | ||
|
||
public error(message: string, error: Error | unknown, meta?: Record<string, unknown>) { | ||
var e; | ||
if (error instanceof Error) { | ||
e = { | ||
name: error.name, | ||
cause: error.cause, | ||
message: error.message, | ||
stack: error.stack, | ||
}; | ||
} else { | ||
e = { | ||
message: JSON.stringify(error), | ||
}; | ||
} | ||
const e = | ||
error instanceof Error | ||
? { | ||
name: error.name, | ||
cause: error.cause, | ||
message: error.message, | ||
stack: error.stack, | ||
} | ||
: { | ||
message: JSON.stringify(error), | ||
}; | ||
|
||
if (error) { | ||
this.log('error', message, { | ||
...meta, | ||
|
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.
similarly here
currentSpan?.context()?.toSpanId()
? if context() is null or undefined we'll get an error.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.
context() shouldn't be nil, since it is part of the definition of the object itself