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 22 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
64 changes: 47 additions & 17 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,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;
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 = {
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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we not want to use console.error() or console.warn() when those are the levels?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise can we structure this as

if (typeof console[level] === 'function') {
      console[level](JSON.stringify(logEntry));
else {
  console.log(JSON.stringify(logEntry));
}

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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);
}
Expand All @@ -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,
Expand Down
Loading