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

feat: add custom logger based on winston #47

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@axiomhq/axiom-node": "^0.12.0",
"@nestjs/cache-manager": "^2.1.0",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.2",
Expand All @@ -53,7 +54,8 @@
"prom-client": "^15.1.2",
"redis": "^4.6.10",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
"rxjs": "^7.8.1",
"winston": "^3.11.0"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './temporal.service';
export * from './file-upload.service';
export * from './logger.service';
116 changes: 116 additions & 0 deletions packages/common/src/services/logger.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Logger, Injectable } from '@nestjs/common';
import * as winston from 'winston';
import { WinstonTransport as AxiomTransport } from '@axiomhq/axiom-node';

@Injectable()
export class StencilLogger extends Logger {
private static formatTimestamp(date: Date): string {
const hours = date.getHours();
const hours12 = hours % 12 || 12;
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');
const milliseconds = date.getMilliseconds().toString().padStart(3, '0');
const amPm = hours >= 12 ? 'PM' : 'AM';

return `${hours12}:${minutes}:${seconds}.${milliseconds} ${amPm}`;
}

private static combineLogs(params: any[]): string {
return params
?.map((param) => {
try {
param = JSON.stringify(param, null, 2);
} catch {
param = param;
}
return param;
})
.join(' ');
}

private formatLog(level, params) {
const timestamp = StencilLogger.formatTimestamp(new Date());
return {
level,
message: StencilLogger.combineLogs(params),
service: this.serviceName,
timestamp,
};
}

private readonly axiomLogger: winston.Logger;
private readonly serviceName: string;

constructor(serviceName) {
super();
const { combine, errors, json } = winston.format;
const axiomTransport = new AxiomTransport();
this.axiomLogger = winston.createLogger({
level: 'silly',
format: combine(errors({ stack: true }), json()),
transports: [axiomTransport],
exceptionHandlers: [axiomTransport],
rejectionHandlers: [axiomTransport],
});
this.serviceName = serviceName;
}

logToAxiomAndConsole(logData) {
switch (logData.level) {
case 'info':
super.log(logData?.message, this.serviceName, logData?.timestamp);
break;
case 'error':
super.error(logData?.message, this.serviceName, logData?.timestamp);
break;
case 'warn':
super.warn(logData?.message, this.serviceName, logData?.timestamp);
break;
case 'debug':
super.debug(logData?.message, this.serviceName, logData?.timestamp);
break;
case 'verbose':
super.verbose(logData?.message, this.serviceName, logData?.timestamp);
break;
default:
super.log(logData?.message, this.serviceName, logData?.timestamp);
break;
}
if (
process.env.ENVIRONMENT == 'Staging' ||
process.env.ENVIRONMENT == 'Production'
)
this.axiomLogger.log(logData);
}

log(...params: any[]) {
this.logToAxiomAndConsole(this.formatLog('info', params));
}

error(...params: any[]) {
this.logToAxiomAndConsole(this.formatLog('error', params));
}

warn(...params: any[]) {
this.logToAxiomAndConsole(this.formatLog('warn', params));
}

debug(...params: any[]) {
this.logToAxiomAndConsole(this.formatLog('debug', params));
}

verbose(...params: any[]) {
this.logToAxiomAndConsole(this.formatLog('verbose', params));
}

logWithCustomFields(customFields, level = 'info') {
return (...params: any[]) => {
let logData = this.formatLog(level, params);
logData = {
...customFields,
...logData,
};
this.logToAxiomAndConsole(logData);
};
}
}
Loading
Loading