Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: When zippedArchive is true, if restart server, will create new info.2024-03-30.log file #2438

Open
kipa-zhang opened this issue Mar 30, 2024 · 0 comments

Comments

@kipa-zhang
Copy link

kipa-zhang commented Mar 30, 2024

🔎 Search Terms

zippedArchive, maxsize, log rotation, restart

The problem

20240330090648

Hi friend,
I'm using winston to zip the large log files and I found that when I restart server the winston will recreate a new info.date.log file instead of info.date.log.n even the former info.date.log.gz already exist.

I hope it continues from info.date.log.n file and also zip from .log.n.gz.

Anyone could help?

What version of Winston presents the issue?

v3.13.0

What version of Node are you using?

v16.14.2

If this worked in a previous version of Winston, which was it?

No response

Minimum Working Example

const { createLogger, format, transports } = require('winston');
const moment = require('moment');
require('winston-daily-rotate-file'); 

const dailyRotateFileConfig = {
    datePattern: 'YYYY-MM-DD',
    zippedArchive: true,
    maxSize: '10k',
    maxFiles: '1d',
};

const customFilePrintFormat = function (ifConsole = false) {
    return format.combine(
        format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
        format.printf((i) => {
            if (ifConsole) {
                return format.colorize().colorize(i.level, `[${ i.timestamp }] [${ i.level.toString().toUpperCase() }] `) + i.message
            } else {
                return `[${ i.timestamp }] [${ i.level.toString().toUpperCase() }] ${ i.message }`
            }
        }),
    );
}

const fileLogger = function (flag) {
    return createLogger({
        format: customFilePrintFormat(),
        transports: [
            new transports.DailyRotateFile({
                level: 'info',
                dirname: `d://winston-logs/info-${ flag }`,
                filename: `info.%DATE%.log`,
                ...dailyRotateFileConfig
            }),
            new transports.DailyRotateFile({
                level: 'error',
                dirname: `d://winston-logs/error-${ flag }`,
                filename: `error.%DATE%.log`,
                ...dailyRotateFileConfig
            }),
            new transports.Console({
                format: customFilePrintFormat(true),
            })
        ],
        exitOnError: false,
    });
}

let log = null;
const initLogger = function () {
    log = fileLogger()
    // log = fileLogger(moment().format('YYMMDDHHmmss'))
}

module.exports = {
    initLogger,
    logger: function (label) {
        return {
            info: function(...str) {
                log.info(`[${ label }] ` + str.join(' '))
            },
            warn: function(...str) {
                log.warn(`[${ label }] ` + str.join(' '))
            },
            error: function(...str) {
                if (str.length > 1 && str[1]?.stack) {
                    log.error(`[${ label }] ` + str[1].stack)
                } else if (str.length > 0 && str[0]?.stack) {
                    log.error(`[${ label }] ` + str[0].stack)
                } else {
                    log.error(`[${ label }] ` + str.join(' '))
                }
            }
        }
    }, 
};

Additional information

No response

@kipa-zhang kipa-zhang changed the title [Bug]: When zippedArchive is true, if restart server, will create new *.log file [Bug]: When zippedArchive is true, if restart server, will create new info.2024-03-30.log file Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant