-1

I am new to Node JS. I was looking for a logging library for Node and I found Winston. Now, I want to archive the log files once they reach certain size with the timestamp included in name.

For ex:

Current log file: logs/devlog.log(10 MB)

since it reached 10 MB, I want to archive it

archived log file: Archive/devlog-03.02.2016.log

please help me do that in winston or any other solution. Thanks in advance

My code for app.js(Node server):

    console.log('Hello world');
var winston = require('winston');
var date1 = new Date();
winston.emitErrs = true;
var date = new Date();
var loggerName = "server.app.js";
var winston = require('winston'),
path = require('path'),
transports = [];

transports.push(new winston.transports.DailyRotateFile({
    name: 'file',
    datePattern: '.yyyy-MM-ddTHH-mm',
    filename: path.join(__dirname, "logs", "log_file.log"),
    maxFiles: 5,
    maxsize: 100000000
}));

var logger = new winston.Logger({ transports: transports });
//winston.loggers.add('ServerLogger', {
//    transports: [
//        new (winston.transports.File)({
//            name: 'Devlogger', //Name of the transport
//            filename: 'logs/devlogNormalF.log', // log file name
//            json: false,
//            maxsize: 1048576, //1MB
//            maxFiles: 10, // 10 Files max
//            timestamp: function () {
//                return new Date().toISOString();//.substring(0,23);
//            }
//        }
//        ), new archiveFile(options)
//    ]
//});
//var logger = winston.loggers.get('ServerLogger');
logger.info ('Logger Name: '+loggerName+' App starting...');
logger.info('Logger Name: ' + loggerName + 'Hello world');
logger.info('Logger Name: ' + loggerName + ' Accessing test/user');
logger.error('Logger Name: ' + loggerName + ' Cannot access test/user/user.png');
var express = require('express');
var app = express();
app.get('/', function (req, res) {
    res.send('Hello World!');
    var date2 = new Date();
    var diff = date2 - date1;
    logger.info('Start '+ date1+' Stop '+date2)
    logger.info('Logger Name: ' + loggerName + ' Response time of Node is ' + diff+' ms');
});

app.listen(3000, function () {
    logger.warn('Logger Name: ' + loggerName + ' Example app listening on port 3000!');
    logger.info('Logger Name: ' + loggerName + ' App ending...');
});
user1840131
  • 11
  • 1
  • 6

1 Answers1

1

Winston supports log roation using datepattern.
You can specify datepattern, maxfilesize and maxfile like so:

var winston     = require ('winston'),
    path        = require ('path'),
    transports  = [];

transports.push(new winston.transports.DailyRotateFile({
  name: 'file',
  datePattern: '.yyyy-MM-ddTHH-mm',
  filename: path.join(__dirname, "logs", "log_file.log"),
  maxFiles: 5,
  maxsize: 100000000

}));

var logger = new winston.Logger({transports: transports});

I have made some changes to your code.I tested it and it works fine.Its creating logs with datepattern.

 console.log('Hello world');
var date1 = new Date();
var date = new Date();
var loggerName = "server.app.js";
var winston = require('winston');
var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.DailyRotateFile)({ 
    name: 'file',
    datePattern: '.yyyy-MM-ddTHH-mm',
    filename: "log_file",
    maxFiles: 5,
    maxsize: 100000000



})
  ]
});



logger.info ('Logger Name: '+loggerName+' App starting...');
logger.info('Logger Name: ' + loggerName + 'Hello world');
logger.info('Logger Name: ' + loggerName + ' Accessing test/user');
logger.error('Logger Name: ' + loggerName + ' Cannot access test/user/user.png');

var express = require('express');
var app = express();
app.get('/', function (req, res) {
    res.send('Hello World!');
    var date2 = new Date();
    var diff = date2 - date1;
    logger.info('Start '+ date1+' Stop '+date2)
    logger.info('Logger Name: ' + loggerName + ' Response time of Node is ' + diff+' ms');
});

app.listen(3000, function () {
    logger.warn('Logger Name: ' + loggerName + ' Example app listening on port 3000!');
    logger.info('Logger Name: ' + loggerName + ' App ending...');
});
Junu
  • 49
  • 2
  • I tried doing this code, It is giving me an error stating: – user1840131 Mar 02 '16 at 15:35
  • I tried doing this code, It is giving me an error stating: transports.push(new winston.transports.DailyRotateFile({ ^ TypeError: winston.transports.DailyRotateFile is not a function at Object. (C:\Users\user123231\Documents\Visual Studio 2015\Projects\LogTester\LogTester\app.js:16:17) at Module._compile (module.js:413:34) at Object.Module._extensions..js – user1840131 Mar 02 '16 at 15:44
  • I just updated the grammar in the response. Try @Junu. – K.Nicholas Mar 02 '16 at 16:10
  • Please check winston object you have declared and check whether you have used the same in transports.push. – Junu Mar 02 '16 at 16:15
  • I just added the code to the question. please have a look – user1840131 Mar 02 '16 at 16:41
  • Test the above code .This works fine in my server.I have tested it. – Junu Mar 02 '16 at 17:32
  • @Junu It is still giving me the same error. Type Error: winston.transports.DailyRotateFile is not a function. What might be the issue? :( – user1840131 Mar 02 '16 at 17:35
  • Check your winston npm module.Update it or install it again. – Junu Mar 03 '16 at 09:33
  • Oh! May I know which winston version are you using? – user1840131 Mar 03 '16 at 13:26
  • Mine was winston 1.0.0.I think you have the latest version. – Junu Mar 03 '16 at 13:37
  • Yes I have 2.x version. Do you suggest me to rollback to older version? – user1840131 Mar 03 '16 at 14:02
  • Run the below 2 commands.sudo npm remove winston; sudo npm install winston@1.0.1 – Junu Mar 03 '16 at 14:24
  • Thank you. It is working but does not perform what I want. If you see the question, If a file reaches certain file size then it should be archived. Is there any other library which can do this? – user1840131 Mar 03 '16 at 18:51
  • You can write a cronjob to do that.You can check the filesize and see if reaches a certain size then zip it.O else you can do it in the same script itself because it will be listening to port 3000 forever. – Junu Mar 04 '16 at 09:39
  • Oh can you help me by adding that cron job code to my current file. Will this cronjob work on windows because i am using Visual studio 2015 for development – user1840131 Mar 17 '16 at 20:17
  • Refer this http://stackoverflow.com/questions/7195503/setting-up-a-cron-job-in-windows – Junu Mar 24 '16 at 08:53
  • is there any other solution?? I need atleast last 10 days logfiles to be archived using winston and rest can be deleted. can this be done?? @Junu – user1840131 Mar 28 '16 at 06:01
  • Bottom you can write a funcion to check the filesize and run this all the time to see if it reaches the limit size .when it reaches arrchive the file from node itself.Refer this https://techoverflow.net/blog/2012/09/16/get-filesize-in-node-js/ – Junu Mar 30 '16 at 10:25