| 1 | import winston from "winston"; |
| 2 | |
| 3 | class LoggerClass { |
| 4 | private static instance: LoggerClass; |
| 5 | private winston: winston.Logger; |
| 6 | |
| 7 | private constructor() { |
| 8 | this.winston = winston.createLogger({ |
| 9 | level: "info", |
| 10 | format: winston.format.combine( |
| 11 | winston.format.colorize(), |
| 12 | winston.format.timestamp(), |
| 13 | winston.format.printf(({ level, message, timestamp, ...meta }) => { |
| 14 | const metaStr = Object.keys(meta).length |
| 15 | ? ` ${JSON.stringify(meta)}` |
| 16 | : ""; |
| 17 | return `[@continuedev] ${level}: ${message}${metaStr}`; |
| 18 | }), |
| 19 | ), |
| 20 | transports: [ |
| 21 | // Write all logs with importance level of `info` or higher to `info.log` |
| 22 | ...(process.env.NODE_ENV === "test" |
| 23 | ? [ |
| 24 | new winston.transports.File({ |
| 25 | filename: "e2e.log", |
| 26 | level: "info", |
| 27 | }), |
| 28 | ] |
| 29 | : []), |
| 30 | // Use stderr to avoid corrupting IPC stdout stream in the binary |
| 31 | new winston.transports.Console({ |
| 32 | stderrLevels: ["error", "warn", "info", "debug"], |
| 33 | }), |
| 34 | ], |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | public static getInstance(): LoggerClass { |
| 39 | if (!LoggerClass.instance) { |
| 40 | LoggerClass.instance = new LoggerClass(); |
| 41 | } |
| 42 | return LoggerClass.instance; |
| 43 | } |
| 44 | |
| 45 | public log(message: string, meta?: any): void { |
| 46 | this.winston.info(message, meta); |
| 47 | } |
| 48 | |
| 49 | public debug(message: string, meta?: any): void { |
| 50 | this.winston.debug(message, meta); |
| 51 | } |
| 52 | |
| 53 | public info(message: string, meta?: any): void { |
| 54 | this.winston.info(message, meta); |
| 55 | } |
| 56 | |
| 57 | public warn(message: string, meta?: any): void { |
| 58 | this.winston.warn(message, meta); |
| 59 | } |
| 60 |
nothing calls this directly
no outgoing calls
no test coverage detected