Configure level per appender #153

Closed
vojtajina opened this Issue Aug 23, 2013 · 14 comments

Comments

Projects
None yet
5 participants
@vojtajina

Is there a way how to configure multiple appenders - each having a different level ?

@nomiddlename

This comment has been minimized.

Show comment Hide comment
@nomiddlename

nomiddlename Aug 25, 2013

Collaborator

Could you give me a bit more detail? Do you mean having separate appenders for debug messages and error messages? If so, log level filter might be what you want - take a look at the examples.

Collaborator

nomiddlename commented Aug 25, 2013

Could you give me a bit more detail? Do you mean having separate appenders for debug messages and error messages? If so, log level filter might be what you want - take a look at the examples.

@selezmar

This comment has been minimized.

Show comment Hide comment
@selezmar

selezmar Oct 12, 2013

It does not work. My appender:

var email_appender = {
    'type': 'smtp',
    'level': 'ERROR',
    'recipients': exports.ADMINS.join(','),
    'sender': '..@..',
    'subject': 'error',
    'sendInterval': 10,
    'transport': 'SMTP',
    'SMTP': {
        'host': 'smtp.yandex.ru',
        //'secureConnection': true,
        //'port': 465,
        'auth': {
            'user': '..@...com',
            'pass': '***'
        }
    }
}
exports.LOG4JS_CONFIG.appenders.push(email_appender);

But i still get info messages to my mail.

log4js@0.6.9
│ ├── async@0.1.15
│ ├── readable-stream@1.0.17
│ └── semver@1.1.4

It does not work. My appender:

var email_appender = {
    'type': 'smtp',
    'level': 'ERROR',
    'recipients': exports.ADMINS.join(','),
    'sender': '..@..',
    'subject': 'error',
    'sendInterval': 10,
    'transport': 'SMTP',
    'SMTP': {
        'host': 'smtp.yandex.ru',
        //'secureConnection': true,
        //'port': 465,
        'auth': {
            'user': '..@...com',
            'pass': '***'
        }
    }
}
exports.LOG4JS_CONFIG.appenders.push(email_appender);

But i still get info messages to my mail.

log4js@0.6.9
│ ├── async@0.1.15
│ ├── readable-stream@1.0.17
│ └── semver@1.1.4

@nomiddlename

This comment has been minimized.

Show comment Hide comment
@nomiddlename

nomiddlename Oct 13, 2013

Collaborator

I think you need to wrap your email appender in a log level filter, with the filter set to only allow error or above level messages through.

Collaborator

nomiddlename commented Oct 13, 2013

I think you need to wrap your email appender in a log level filter, with the filter set to only allow error or above level messages through.

@nomiddlename

This comment has been minimized.

Show comment Hide comment
@nomiddlename

nomiddlename Oct 13, 2013

Collaborator

Also, that's not the right way to configure log4js - you should either call log4js.configure with an object, or create a json file.

Collaborator

nomiddlename commented Oct 13, 2013

Also, that's not the right way to configure log4js - you should either call log4js.configure with an object, or create a json file.

@marcelog

This comment has been minimized.

Show comment Hide comment
@marcelog

marcelog Sep 18, 2014

Contributor

@nomiddlename I was looking into this same feature, and was trying to make a patch to support it. I think log4j has a similar feature with the threshold property, explained a bit more in this faq entry.

I was thinking that perhaps we can save the appender config somehow in the call to addAppender and then for each addListener call, instead of registering the appender, register a clojure that will filter the incoming log based on the appender threshold and the level of the log object that we got. In essence, this might mean to change the contract for addAppender, which is an exported function and might mean a change not backwards compatible. Any thoughts?

Contributor

marcelog commented Sep 18, 2014

@nomiddlename I was looking into this same feature, and was trying to make a patch to support it. I think log4j has a similar feature with the threshold property, explained a bit more in this faq entry.

I was thinking that perhaps we can save the appender config somehow in the call to addAppender and then for each addListener call, instead of registering the appender, register a clojure that will filter the incoming log based on the appender threshold and the level of the log object that we got. In essence, this might mean to change the contract for addAppender, which is an exported function and might mean a change not backwards compatible. Any thoughts?

@marcelog

This comment has been minimized.

Show comment Hide comment
@marcelog

marcelog Sep 18, 2014

Contributor

I'm playing around with another option.. to wrap the appender function in an object, that can keep the config, like:

"use strict";
var levels = require('./levels');

function Appender(config, appender) {
  this.config = config;
  this.appender = appender;
  if(!this.config.threshold) {
    this.config.threshold = levels.ALL;
  }
}

Appender.prototype.logHandler = function(self, args) {
  if(args[0].level.isGreaterThanOrEqualTo(self.config.threshold)) {
    self.appender.apply(self.appender, args);  
  }
};

exports.Appender = Appender;

And in log4js.js, inside configureAppenders:

      try {
        appender = appenderMakers[appenderConfig.type](appenderConfig, options);
        var appenderObject = new appenderMod.Appender(appenderConfig, appender);
        addAppender(function() {
          appenderObject.logHandler(appenderObject, arguments);
        }, appenderConfig.category);
      } catch(e) {
        throw new Error("log4js configuration problem for " + util.inspect(appenderConfig), e);
      }

This seems to be a nicer approach, will try to put together a patch, but it would be great to hear any insights from you :)

Contributor

marcelog commented Sep 18, 2014

I'm playing around with another option.. to wrap the appender function in an object, that can keep the config, like:

"use strict";
var levels = require('./levels');

function Appender(config, appender) {
  this.config = config;
  this.appender = appender;
  if(!this.config.threshold) {
    this.config.threshold = levels.ALL;
  }
}

Appender.prototype.logHandler = function(self, args) {
  if(args[0].level.isGreaterThanOrEqualTo(self.config.threshold)) {
    self.appender.apply(self.appender, args);  
  }
};

exports.Appender = Appender;

And in log4js.js, inside configureAppenders:

      try {
        appender = appenderMakers[appenderConfig.type](appenderConfig, options);
        var appenderObject = new appenderMod.Appender(appenderConfig, appender);
        addAppender(function() {
          appenderObject.logHandler(appenderObject, arguments);
        }, appenderConfig.category);
      } catch(e) {
        throw new Error("log4js configuration problem for " + util.inspect(appenderConfig), e);
      }

This seems to be a nicer approach, will try to put together a patch, but it would be great to hear any insights from you :)

@marcelog

This comment has been minimized.

Show comment Hide comment
@marcelog

marcelog Sep 18, 2014

Contributor

Just noticed your comment about wrapping the appender in a logLevelFilter, doh. I'll try to use that. The patch is done, by the way, just need to write a unit test in case the 'threshold' option is desirable inside the appender (also means less configuration overhead, instead of wrapping around each appender inside a logLevelFilter, there's only this new option per appender). Just let me know if this is something that 'get in' :) Cheers.

Contributor

marcelog commented Sep 18, 2014

Just noticed your comment about wrapping the appender in a logLevelFilter, doh. I'll try to use that. The patch is done, by the way, just need to write a unit test in case the 'threshold' option is desirable inside the appender (also means less configuration overhead, instead of wrapping around each appender inside a logLevelFilter, there's only this new option per appender). Just let me know if this is something that 'get in' :) Cheers.

@leegee

This comment has been minimized.

Show comment Hide comment
@leegee

leegee Nov 10, 2016

Did this never get released?
TIA
Lee

leegee commented Nov 10, 2016

Did this never get released?
TIA
Lee

@nomiddlename

This comment has been minimized.

Show comment Hide comment
@nomiddlename

nomiddlename Nov 11, 2016

Collaborator

I think log level filters do the same job, and you can specify a min log level and a max log level on them. There's also a category filter as well.

Collaborator

nomiddlename commented Nov 11, 2016

I think log level filters do the same job, and you can specify a min log level and a max log level on them. There's also a category filter as well.

@leegee

This comment has been minimized.

Show comment Hide comment
@leegee

leegee Nov 11, 2016

Can a log level or category filter restrict a specific appender to logging messages on or above a specific level?

leegee commented Nov 11, 2016

Can a log level or category filter restrict a specific appender to logging messages on or above a specific level?

@nomiddlename

This comment has been minimized.

Show comment Hide comment
@nomiddlename

nomiddlename Mar 27, 2017

Collaborator

Closing this one, as logLevelFilter and categoryFilter do the job (and in version 2.x setting levels for categories becomes a bit more explicit).

Collaborator

nomiddlename commented Mar 27, 2017

Closing this one, as logLevelFilter and categoryFilter do the job (and in version 2.x setting levels for categories becomes a bit more explicit).

@leegee

This comment has been minimized.

Show comment Hide comment
@leegee

leegee Mar 28, 2017

leegee commented Mar 28, 2017

@nomiddlename

This comment has been minimized.

Show comment Hide comment
@nomiddlename

nomiddlename Mar 30, 2017

Collaborator

Hi @leegee - the log4js example application uses a log level filter. Take a look at the config - does that help?

Collaborator

nomiddlename commented Mar 30, 2017

Hi @leegee - the log4js example application uses a log level filter. Take a look at the config - does that help?

@leegee

This comment has been minimized.

Show comment Hide comment
@leegee

leegee Mar 31, 2017

Yes, but why not put it in the docs?

leegee commented Mar 31, 2017

Yes, but why not put it in the docs?

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