I am using winston to log files into server that is working as expected , now i want to set days limit , Lets say after 3 days i want to delete files that are logged 3 days ago , is it possible to achieve using winston rotation ?
main.js
winston.add(winston.transports.File, {
filename: './Logs/server.log',
maxsize:'15000',
timestamp:false
});
Command Prompt Log Clearing When the Command Prompt window opens, type the command "cd" (without quotes) and press "Enter," and then type "cd windows" before pressing "Enter" once more. You can then enter the command "del *. log /a /s /q /f" and press "Enter" to delete all log files from the Windows directory.
You can change the logging level in runtime by modifying the level property of the appropriate transport: var log = new (winston. Logger)({ transports: [ new (winston. transports.
Go to the example. log file in the logs folder to view the log. Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database. The Logger configuration below logs to a console and a file.
You can use Winston daily rotate for this.
Short answer: Yes, you can do that by adding maxFiles:3d
in transport
option.
If you do not set the maxFiles
, no log file will be deleted.
If you set maxFiles:3d
, it will delete all log files created in the last 3 days.
maxFiles:3
, it will delete log files when it exceeds more than 3.Note: You can archive files before deleting by using zippedArchive: true
, if not set the logs files will be deleted without any backup. Till now, I didn't get any way to auto-delete zip files.
Sample setup:
require('winston-daily-rotate-file');
var transport = new (winston.transports.DailyRotateFile)({
filename: 'application-%DATE%.log',
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d'
});
transport.on('rotate', function(oldFilename, newFilename) {
// do something fun
});
var logger = winston.createLogger({
transports: [
transport
]
});
logger.info('Hello World!');```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With