开发者

Limit logging to only one jar log4j

I have a project which is in turn used by several other projects. I want log4j to log only my logs to a file that I have specified in the properties file. Other project use their own logging mechanisms and I have no control over them. My log4j files should not affect other project's logging. How should i configure开发者_JAVA技巧 my log4j property file?

So far what I'm doing is setting log4j.rootLogger = ERROR and for my module log4j.logger.com.xyz.myproject = INFO, FILE. Will this work without affecting other project's loggers? Or possibly limit logging to only my jar?

Thanks


It depends on the package structure of the other projects. Supposing that

  • loggers from other projects are created by Logger.getLogger(ClassA.class) AND
  • some of them rely on root logger configuration (have no specific log4j.category.loggerName settings AND
  • these projects contain subpackages of the package used by your project (i.e. your project's package is com.abc.def and other projects have packages deeper in the hierarchy com.abc.def.ghi THEN

changing com.abc.def logging level would affect other projects - they'll start logging on the level defined by com.abc.def.

Verify that it's not the case and you should be safe.


I suppose your jar is entirely contained in your own package (ex com.foo.mypackage). In this case, is just enough to add to your log4j configuration something like:

# Print only messages of priority WARN or above in the package com.foo
log4j.category.com.foo=WARN
# Print only messages of priority DEBUG or above in the package com.foo.mypackage
log4j.category.com.foo.mypackage=DEBUG

Regards,

M.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜