Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config file does not allow subcategory to inherit appenders #824

Closed
pharapiak opened this issue Jan 14, 2019 · 5 comments
Closed

config file does not allow subcategory to inherit appenders #824

pharapiak opened this issue Jan 14, 2019 · 5 comments

Comments

@pharapiak
Copy link

pharapiak commented Jan 14, 2019

Contrary to the description of category hierarchies here, it seems that if I define a child category in a config file without appenders, the config file validation fails with the error,

category "myapp.child" is not valid (must be an object with properties "appenders" and "level").

Here's my config:

{
"appenders": {
"console": { "type": "console", "layout": { "type": "colored" } }
},
"categories": {
"default": { "appenders": [ "console" ], "level": "WARN" },
"myapp": { "appenders": [ "console" ], "level": "INFO" },
"myapp.child": { "level": "DEBUG" }
},
}

A quick scan of the code suggests that if we do a getLogger('myapp.child'), and there is no config for myapp.child, we'll inherit all of 'myapp's config, but if there is a config for that child, it needs to be complete.

@k7sleeper
Copy link

k7sleeper commented Mar 22, 2019

One of many workarounds:

You can call configure() without that incomplete child configuration and then execute:

log4js.getLogger('myapp.child').level = 'DEBUG';

@pharapiak
Copy link
Author

I'm working in an environment where all of the configuration is done with a config file, so I can't use a API-based workaround.

I come from a log4j / log4.net background, with a logger per source file (usually); with that setup, it's nice to be able to configure a hierarchy with inheritance.

@nomiddlename , would you consider a PR along these lines?

  • when defining a child category, all of the appenders and verbosity settings of the parent category apply, and the settings of the child category are handled as deltas.
  • child category appenders would be in addition to those of the parent.
  • child category verbosity would override those of the parent
  • this would apply recursively through a parent-child hierarchy

Is there a use case for allowing a child category's config to completely replace that of the parent?

@nomiddlename
Copy link
Collaborator

Sounds ok to me - having anything unspecified in the child category config inherit from the parent makes sense.

@pharapiak
Copy link
Author

One caution...

With the current implementation, if somebody cranks up the verbosity for a child category, that extra 'noise' won't be sent to the appenders specified for the parent, unless the same appenders are specified for the child.

The functionality I'm suggesting would break that, since child appenders would be in addition to those of the parent. Perhaps we should include an option to keep the old behaviour? Say, "inherit": "OFF"?

Log4j subcategories also inherit appenders, unless the child category has "additivity=false". IMO, "inherit=off" is more clear.
(see https://veerasundar.com/blog/2009/08/log4j-tutorial-additivity-what-and-why/)

@pharapiak
Copy link
Author

This PR has resolved this issue.

Unless a category has inherit=false, it will a) merge in unique appenders from parent categories, and b) inherit the level from the parent category if it doesn't specify its own level directly. This inheritance is recursive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants