os : windows 10
"winston": "^3.12.0",
"winston-daily-rotate-file": "^5.0.0"
nodeJS : v20.11.1
nestJS : 10.3.2
I'm trying to use winston logger in Nestjs. While processing winston-daily-rotate-file
, the following error occurred in the login file management settings section.
https://github.com/winstonjs/winston-daily-rotate-file
I proceeded with the work by referring to the reference above, and my code is as follows.
import * as winston from 'winston';
import DailyRotateFile from 'winston-daily-rotate-file';
const { timestamp, colorize } = winston.format;
const config = {
levels: {
error: 0,
debug: 1,
warn: 2,
data: 3,
info: 4,
verbose: 5,
silly: 6,
custom: 7,
},
colors: {
error: 'red',
debug: 'blue',
warn: 'yellow',
info: 'green',
data: 'magenta',
verbose: 'cyan',
silly: 'grey',
custom: 'yellow',
},
};
const dataOnlyFormat = winston.format((info) => {
if (info.level === 'data') {
return info;
}
return false;
})();
const transport: DailyRotateFile = new DailyRotateFile({
filename: 'application-%DATE%.log',
datePattern: 'YYYY-MM-DD-HH',
dirname: `${__dirname}/../../../logs/data`,
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d',
format: winston.format.combine(dataOnlyFormat, timestamp(), winston.format.json()),
});
winston.addColors(config.colors);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const singleLineFormat = winston.format.printf(({ level, message, timestamp, context, ...meta }) => {
const metaString = JSON.stringify(meta);
return `👀 [****] ${timestamp} [${level}] \n📟 ${message} - ${metaString}\n\n`;
});
export const winstonConfig = {
levels: config.levels,
transports: [
new winston.transports.Console({=
level: process.env.NODE_ENV === 'release' ? 'info' : 'silly',
format: winston.format.combine(
timestamp({
format: 'YYYY. MM. DD. hh:mm:ss A',
}),
singleLineFormat,
colorize({ all: true }),
),
}),
transport,
],
};
An error occurs in transport below. Do you know the cause?
export const winstonConfig = {
levels: config.levels,
transports: [
new winston.transports.Console({
level: process.env.NODE_ENV === 'release' ? 'info' : 'silly',
format: winston.format.combine(
timestamp({
format: 'YYYY. MM. DD. hh:mm:ss A',
}),
singleLineFormat,
colorize({ all: true }),
),
}),
transport,
],
};
TypeError: winston_daily_rotate_file_1.default is not a constructor
at Object.<anonymous> (D:\***\src\configs\winston.config.ts:36:36)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Function.Module._load (node:internal/modules/cjs/loader:1023:12)
at Module.require (node:internal/modules/cjs/loader:1235:19)
at require (node:internal/modules/helpers:176:18)
at Object.<anonymous> (D:\***\src\app.module.ts:10:1)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
update this line:
import DailyRotateFile from 'winston-daily-rotate-file';
by
import 'winston-daily-rotate-file';
and this initialization:
const transport: DailyRotateFile = new DailyRotateFile
by
const transport = new winston.transports.DailyRotateFile