Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs Winston logger - log output to both console and file

I've written a standard express server with Nodejs and Winston logger. For some reason, the log output is written both to console and the designated log file.

Every line in the code:

winston.log('info', '**************************************');

Is written to both console and log file.

Why is this happening?

He is my code:

var express = require('express');
var bodyParser = require('body-parser');
var mysql = require ('mysql');
var winston = require('winston');

var app = express();

//Init Winston logger, max file size 5MB with 10 file retention
winston.add(winston.transports.File, { filename: './logs/eclipse_server.log', level: 'info',handleExceptions: true,
            maxsize: 5242880,maxFiles: 10});

winston.log('info', '**************************************');
winston.log('info', 'Eclipse web server - start up process');
winston.log('info', 'Express server mode initialized');                 
winston.log('info', 'Initialize database connection');
var connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: '12345678',
    database: 'project_eclipse',
    port: 3306 });

    winston.log('info', 'Database connection initialized');
    winston.log('info', 'Database connection attempted');
    connection.connect(function(err){
    if(!err) { 
        winston.log('info', 'Database connection success - connected'); 
    } else {
        winston.log('error', 'Database connection - failed');   
        winston.log('error', err);
    }
    }); 


// instruct the app to use the `bodyParser()` middleware for all routes

winston.log('info', 'using bodyParser()');  
app.use(bodyParser());
winston.log('info', 'using bodyParser.text()'); 
app.use(bodyParser.text());
winston.log('info', 'initialize HTML directory');
app.use(express.static(__dirname + '/public'));
winston.log('info', 'initialized HTML directory');

app.post('/', function(request, response){
    winston.log('info', 'In game POST login request recieved');
    var usr = request.body.usr;
    var pass = request.body.pass;

    //var client_ip = req.header('x-forwarded-for') || req.connection.remoteAddress;

    winston.log('info', 'login request recieved from  '+usr);

    connection.query('SELECT * FROM eclipse_users WHERE username=? AND password = md5(?)', [ usr, pass ], function(err, rows, fields) {

      if (!err)
      {
        var n_rows = rows.length;
         if(n_rows==1)
         {   
            //user exists
            winston.log('info', 'user exists - response will be send');
            response.json({msg:'user exists'});
            winston.log('info', 'user exists - response sent');
         }
        else
        {
            //user not found
            winston.log('info', 'user not found - response will be send');
            response.json({msg:'user does not exist'});
            winston.log('info', 'user not found - response sent');
        }
      }
     else
        //SQL query error
        {
        winston.log('error', 'Error while performing select query');    
        winston.log('error', err);
        connection.end();}
      });
});

app.post('/weblogin', function(request, response){

    winston.log('info', 'web POST login request recieved');
    var usr = request.body.usr;
    var pass = request.body.password;

    //var client_ip = req.header('x-forwarded-for') || req.connection.remoteAddress;

    winston.log('info', 'login request recieved from  '+usr);

    connection.query('SELECT * FROM eclipse_users WHERE username=? AND password = md5(?)', [ usr, pass ], function(err, rows, fields) {

      if (!err)
      {
        var n_rows = rows.length;
         if(n_rows==1)
         {
            //user exists
            winston.log('info', 'user exists - response will be send');
            response.send('1');
            winston.log('info', 'user exists - response sent');
         }
        else{
            //user does not exist
            winston.log('info', 'user not found - response will be send');
            response.send('0');
            winston.log('info', 'user not found - response sent');
        }
    }
      else
        //SQL query error
        {
        winston.log('error', 'Error while performing select query');    
        winston.log('error', err);
        connection.end();}
      });
});

app.post('/myaction', function(request, response) {

  winston.log('info', 'web POST register user request recieved');
    var usr = request.body.username;
    var pass = request.body.pass;
    var uemail = request.body.mail;

    winston.log('info', 'preparing sql query');
    var check = connection.query('INSERT INTO eclipse_users (username, password,email) VALUES (?, md5(?),?)', [ usr, pass,uemail ], function(err,result) {

    if (!err)
    {
        winston.log('info', 'new user registered');
        response.send('User registered');}
    else
    {
        winston.log('error', 'err');
        response.send('ERROR');
    }

    });

});

winston.log('info', 'binding port 80 on IP 172.31.16.218');
app.listen(80,"172.31.16.218");
winston.log('info', 'server running at http://172.31.16.218:80');
like image 507
David Faizulaev Avatar asked May 14 '15 22:05

David Faizulaev


1 Answers

Winston adds the Console transport by default. If you don't want log to the console you have two options.

Remove it:

winston.remove(winston.transports.Console);

Or instantiate your own logger:

var logger = new (winston.Logger)({
    transports: [
      new (winston.transports.File)({ filename: 'somefile.log' })
    ]
  });

And use your new logger:

logger.log('info', "this won't be printed to the console");
like image 119
victorkt Avatar answered Nov 15 '22 11:11

victorkt