40 lines
1019 B
TypeScript
40 lines
1019 B
TypeScript
import winston from 'winston';
|
|
|
|
const { combine, timestamp, json, printf, colorize, errors } = winston.format;
|
|
|
|
const devFormat = printf(({ level, message, timestamp, ...metadata }) => {
|
|
let msg = `${timestamp} [${level}]: ${message}`;
|
|
if (Object.keys(metadata).length > 0) {
|
|
msg += ` ${JSON.stringify(metadata)}`;
|
|
}
|
|
return msg;
|
|
});
|
|
|
|
export const createLogger = (service: string): winston.Logger => {
|
|
const isDev = process.env.NODE_ENV !== 'production';
|
|
const logLevel = process.env.LOG_LEVEL || (isDev ? 'debug' : 'info');
|
|
|
|
return winston.createLogger({
|
|
level: logLevel,
|
|
defaultMeta: { service },
|
|
format: isDev
|
|
? combine(
|
|
colorize(),
|
|
timestamp({ format: 'HH:mm:ss' }),
|
|
errors({ stack: true }),
|
|
devFormat
|
|
)
|
|
: combine(
|
|
timestamp(),
|
|
json()
|
|
),
|
|
transports: [
|
|
new winston.transports.Console({
|
|
stderrLevels: ['error'],
|
|
}),
|
|
],
|
|
});
|
|
};
|
|
|
|
export const logger = createLogger('App');
|