Nodejs Winston记录器 - 将输出记录到控制台和文件

时间:2015-05-14 22:35:17

标签: node.js logging winston

我已经用Nodejs和Winston记录器编写了一个标准的快速服务器。 出于某种原因,日志输出将写入控制台和指定的日志文件。

代码中的每一行:

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

写入控制台和日志文件。

为什么会这样?

他是我的代码:

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');

1 个答案:

答案 0 :(得分:4)

Winston添加了控制台传输by default。如果您不想登录控制台,您有两个选择。

删除它:

winston.remove(winston.transports.Console);

或实例化您自己的记录器:

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

并使用您的新记录器:

logger.log('info', "this won't be printed to the console");