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 logger structure #981

Merged
merged 23 commits into from
Sep 6, 2024
Merged
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
78 changes: 58 additions & 20 deletions apps/web/src/utils/logger.ts
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;

Expand All @@ -20,14 +26,47 @@ class CustomLogger {
return CustomLogger.instance;
}

private log(level: LogLevel, message: string, meta?: Record<string, unknown>) {
if (level === 'debug' || level === 'verbose' || level === 'info') {
console.log(`[${this.service}] ${message}`, meta);
} else if (typeof console[level] === 'function') {
console[level](`[${this.service}] ${message}`, meta);
} else {
console.log(`[${this.service}] ${message}`, meta);
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;
Copy link
Collaborator

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.

Copy link
Collaborator Author

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

}

const logEntry = JSON.stringify({
message: `[${this.service}] ${message}`,
level,
dd: {
trace_id: traceId,
span_id: spanId,
},
...meta,
});

switch (level) {
case 'debug':
case 'verbose':
case 'info':
console.log(logEntry);
break;
case 'warn':
console.warn(logEntry);
break;
case 'error':
console.error(logEntry);
break;
default:
console.log(logEntry);
}
}

private log(level: LogLevel, message: string, meta?: Record<string, unknown>) {
this.createDatadogLog(level, message, meta);
}

public info(message: string, meta?: Record<string, unknown>) {
Expand All @@ -39,19 +78,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,
Expand Down
Loading