Skip to content

Instantly share code, notes, and snippets.

@yunfan
Created March 20, 2025 02:16
Show Gist options
  • Save yunfan/3d4c5f128475a6dc411f8bf92594777d to your computer and use it in GitHub Desktop.
Save yunfan/3d4c5f128475a6dc411f8bf92594777d to your computer and use it in GitHub Desktop.
my logger for tsrpc
import * as winston from 'winston';
import DailyRotateFile from 'winston-daily-rotate-file';
import { Config } from '../config';
class MyLogger {
logger: winston.Logger;
defaultLogLevel: string;
constructor(logger: winston.Logger, defaultLogLevel: string) {
this.logger = logger;
this.defaultLogLevel = defaultLogLevel;
}
log(msg: string, ...meta: any[]) {
this.logger.log(this.defaultLogLevel, msg, ...meta);
}
debug(msg: string, ...meta: any[]) {
this.logger.debug(msg, ...meta);
}
warn(msg: string, ...meta: any[]) {
this.logger.warn(msg, ...meta);
}
info(msg: string, ...meta: any[]) {
this.logger.info(msg, ...meta);
}
error(msg: string, ...meta: any[]) {
this.logger.error(msg, ...meta);
}
}
export function initLogger(logLevel: string = 'debug') {
const mine = winston.createLogger({
transports: [new DailyRotateFile(Config.rotatedLogOptions), new winston.transports.Console()],
});
return new MyLogger(mine, logLevel);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment