Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Connect/express logger can send messages only to one log4js logger #154

Closed
bedrorom opened this Issue Sep 1, 2013 · 6 comments

Comments

Projects
None yet
3 participants

bedrorom commented Sep 1, 2013

When I try to redirrect messages from express logger to two log4js loggers using

app.use(log4js.connectLogger(logger1, { level: log4js.levels.INFO, format: 'format1 :method :url :status' }));
app.use(log4js.connectLogger(logger2, { level: log4js.levels.INFO, format: 'format2 :method :url :status' }));

only the first log4js logger is used, the second one (logger2) is ignored.
Here is a code for reproducing the issue

// modified https://github.com/nomiddlename/log4js-node/blob/master/examples/example-connect-logger.js

//load modules
var log4js = require('log4js');
var express = require("express");
var app = express();

//config
log4js.configure({
  appenders: [{ 
    type: 'console', 
    category: ['log1', 'log2']
  }]
});

//define loggers
var logger1 = log4js.getLogger('log1');
var logger2 = log4js.getLogger('log2');

//both loggers accept info and above levels
logger1.setLevel('INFO');
logger2.setLevel('INFO');

//express app
app.configure(function() {
  app.use(express.favicon(''));
  app.use(log4js.connectLogger(logger1, { level: log4js.levels.INFO, format: 'format1 :method :url :status' }));
  app.use(log4js.connectLogger(logger2, { level: log4js.levels.INFO, format: 'format2 :method :url :status' })); // this line is ignored
});

//route
app.get('/', function(req,res) {
  res.send('hello world');
});

//start app
app.listen(5000);

//testing loggers
logger1.info('server runing at localhost:5000');
logger2.info('server runing at localhost:5000');

Expected output after one browser request

[2013-09-01 14:01:10.035] [INFO] log1 - server runing at localhost:5000
[2013-09-01 14:01:10.038] [INFO] log2 - server runing at localhost:5000
[2013-09-01 14:01:12.202] [INFO] log1 - format1 GET / 200
[2013-09-01 14:01:12.202] [INFO] log2 - format2 GET / 200

Real output after one browser request

[2013-09-01 14:01:10.035] [INFO] log1 - server runing at localhost:5000
[2013-09-01 14:01:10.038] [INFO] log2 - server runing at localhost:5000
[2013-09-01 14:01:12.202] [INFO] log1 - format1 GET / 200
Collaborator

nomiddlename commented Sep 3, 2013

This is intentional - most connect middleware adds a check to see if it is already included in a particular route, to avoid processing a request twice. However, in this instance I don't think that processing the request twice will cause too many problems so maybe we could relax the requirement.

bedrorom commented Sep 3, 2013

My original intention was to redirrect express messages to two appenders with a different format. Relaxing the requitement would allow me to do this so I think it is a good idea.

Contributor

Smurf-IV commented Mar 17, 2016

What happened to this?
I want to use ajax logging to only send to a server side client log file, and all the other actual Put / Get requests to another log file ?

Collaborator

nomiddlename commented Mar 18, 2016

I think you can still do what you want, without changes. Try adding the logging middleware on each route, instead of globally.

Contributor

Smurf-IV commented Apr 6, 2016

Ok, I have this is the config file for just logging the specific calls that come through to a certain url
{ "type": "dateFile", "filename": "accessClient.log", "pattern": "-yyyy-MM-dd_hh", "category": "httpClient", "alwaysIncludePattern": false, "layout": { "type": "pattern", "pattern": "%m" } }, etc.

Then in the server code I exclude the url from the express logger

var httpLogger = log4js.getLogger('http');
app.use(log4js.connectLogger(httpLogger, { nolog: ['.gif', '.css', '.jpg', '.png', '/clientLog'] }));

and then for the url I want to use I do this:

var httpClientLogger = log4js.getLogger('httpClient');
app.post('/clientLog', bodyParser.urlencoded({extended: true}), function (req, res) {
    const level = req.body.level;
    if (httpClientLogger.isLevelEnabled(level)) {
        const timeStamp = moment(req.body.timestamp, 'x').format();
        httpClientLogger.log(level, '%s[%s] %s %s', timeStamp, req.headers.cookie, (level + ' ').slice(0, 5), req.body.message);
    }
    res.status(204).end(); // 204 - No Content: Server successfully processed request, No content to return
});
Collaborator

nomiddlename commented Jul 8, 2017

Closing this, because it looks like there's a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment