33 lines
946 B
TypeScript
33 lines
946 B
TypeScript
import winston from 'winston';
|
|
|
|
export type Logger = winston.Logger;
|
|
|
|
export function createLogger(): Logger {
|
|
return winston.createLogger({
|
|
level: process.env.LOG_LEVEL || 'info',
|
|
format: winston.format.combine(
|
|
winston.format.timestamp(),
|
|
winston.format.errors({ stack: true }),
|
|
winston.format.json()
|
|
),
|
|
transports: [
|
|
new winston.transports.Console({
|
|
format: winston.format.combine(
|
|
winston.format.colorize(),
|
|
winston.format.printf(({ level, message, timestamp, ...meta }) => {
|
|
const metaStr = Object.keys(meta).length ? ` ${JSON.stringify(meta)}` : '';
|
|
return `${timestamp} [${level}]: ${message}${metaStr}`;
|
|
})
|
|
),
|
|
}),
|
|
new winston.transports.File({
|
|
filename: 'logs/error.log',
|
|
level: 'error',
|
|
}),
|
|
new winston.transports.File({
|
|
filename: 'logs/combined.log',
|
|
}),
|
|
],
|
|
});
|
|
}
|