node.js - Nodejs Winston logger - log output to both console and file -
i've written standard express server nodejs , winston logger. reason, log output written both console , designated log file.
every line in code:
winston.log('info', '**************************************');
is written both console , log file.
why happening?
he 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 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 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 app use `bodyparser()` middleware 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 '+usr); connection.query('select * eclipse_users username=? , 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 send'); response.json({msg:'user exists'}); winston.log('info', 'user exists - response sent'); } else { //user not found winston.log('info', 'user not found - response send'); response.json({msg:'user 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 '+usr); connection.query('select * eclipse_users username=? , 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 send'); response.send('1'); winston.log('info', 'user exists - response sent'); } else{ //user not exist winston.log('info', 'user not found - response 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 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 @ http://172.31.16.218:80');
winston adds console transport by default. if don't want log console have 2 options.
remove it:
winston.remove(winston.transports.console);
or instantiate own logger:
var logger = new (winston.logger)({ transports: [ new (winston.transports.file)({ filename: 'somefile.log' }) ] });
and use new logger:
logger.log('info', "this won't printed console");
Comments
Post a Comment