简体   繁体   English

Winston日志文件在应用重启时丢失

[英]Winston log files lost on app restart

I'm using Winston logger for node.js, and everytime I restart the app, the logs get overwritten by the blank ones, starting at the moment of the restart. 我使用Winston logger作为node.js,每次重新启动应用程序时,从重新启动时开始,空白日志就会覆盖日志。 I need to keep the logs, SPECIALLY when I have to restart the app, since it's surely due to an error. 我必须保留日志,特别是在必须重新启动应用程序时,因为肯定是由于错误。 I've read the documentation at GitHub, but found nothing about this. 我已经在GitHub上阅读了文档,但是对此一无所获。 This is how I'm using the transports: 这就是我使用交通工具的方式:

winston.add(winston.transports.Console, {
    level: config.logLevel,
    silent: false,
    colorize: true, 
    timestamp: true
});
winston.add(winston.transports.File, {
    filename: config.logFile,
    maxsize: 524288000, // 500MB
    maxFiles: 4,
    handleExceptions: true,
    json: false,
    level: 'debug'
});

Is there any way to rotate the logs on app restart so I can see what happenned? 有什么方法可以轮流重新启动应用程序的日志,以便我可以看到发生了什么? Thanks! 谢谢!

You can do that by using a stream , instead of a filename, as an option for the file transport: https://github.com/winstonjs/winston/blob/master/docs/transports.md#file-transport Open a file for appending and then 您可以通过使用 (而不是文件名)作为文件传输的选项来做到这一点: https : //github.com/winstonjs/winston/blob/master/docs/transports.md#file-transport打开文件用于附加,然后

winston.add(winston.transports.File, { stream: my_already_opened_file} )

If that's not possible, you could try and generate a new filename based on the process number, for instance, or use another logging option. 如果不可能,则可以尝试根据进程号生成新的文件名,例如,或使用其他日志记录选项。 There does not seem to be a way in winston to append existing log files. 在Winston中似乎没有一种方法可以附加现有的日志文件。 There are also several ways of generating random or temporary file names: nodejs - Temporary file name but you can always use the filename itself as a store for a sequence name: "log-x.log", por instance, and read the file at the beginning of your program and create another with the sequence number incremented by one 还有几种生成随机或临时文件名的方法: nodejs-临时文件名,但您始终可以使用文件名本身作为序列名的存储:“ log-x.log”,por实例,并在以下位置读取文件程序的开头并创建另一个,其序号加一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM