Configuring Log4j Loggers Programmatically Ask Question

Configuring Log4j Loggers Programmatically Ask Question

I am trying to use SLF4J (with log4j binding) for the first time.

I would like to configure 3 different named Loggers that can be returned by a LoggerFactory which will log different levels and push the messages to different appenders:

  • Logger 1 "FileLogger" logs DEBUG and appends to DailyRollingFileAppender
  • Logger 2 "TracingLogger" logs TRACE+ and appends to a JmsAppender
  • Logger 3 "ErrorLogger" logs ERROR+ and appends to a different JmsAppender

Furthermore I want them configured programmatically (in Java, as opposed to XML or a log4j.properties file).

I imagine that, normally, I would define these Loggers somewhere in some bootstrapping code, like an init() method. However, because I want to use slf4j-log4j, I'm confused about where I could define loggers and make them available to the classpath.

I don't believe this is a violation of SLF4J's underlying purpose (as a facade), because my code using the SLF4J API won't ever know that these loggers exist. My code just makes normal calls to the SLF4J API, which then forwards them on to the log4j Loggers it finds on the classpath.

But how do I configure those log4j Loggers on the classpath...in Java?!

ベストアンサー1

You can add/remove Appender programmatically to Log4j:

  ConsoleAppender console = new ConsoleAppender(); //create appender
  //configure the appender
  String PATTERN = "%d [%p|%c|%C{1}] %m%n";
  console.setLayout(new PatternLayout(PATTERN)); 
  console.setThreshold(Level.FATAL);
  console.activateOptions();
  //add appender to any Logger (here is root)
  Logger.getRootLogger().addAppender(console);

  FileAppender fa = new FileAppender();
  fa.setName("FileLogger");
  fa.setFile("mylog.log");
  fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
  fa.setThreshold(Level.DEBUG);
  fa.setAppend(true);
  fa.activateOptions();

  //add appender to any Logger (here is root)
  Logger.getRootLogger().addAppender(fa);
  //repeat with all other desired appenders

I'd suggest you put it into an init() somewhere, where you are sure, that this will be executed before anything else. You can then remove all existing appenders on the root logger with

 Logger.getRootLogger().getLoggerRepository().resetConfiguration();

and start with adding your own. You need log4j in the classpath of course for this to work.

Remark:
You can take any Logger.getLogger(...) you like to add appenders. I just took the root logger because it is at the bottom of all things and will handle everything that is passed through other appenders in other categories (unless configured otherwise by setting the additivity flag).

If you need to know how logging works and how is decided where logs are written read this manual for more infos about that.
In Short:

  Logger fizz = LoggerFactory.getLogger("com.fizz")

は、カテゴリ "com.fizz" のロガーを提供します。
上記の例では、これは、それを使用してログに記録されたすべてのものが、ルート ロガーのコンソールとファイル アペンダーに参照されることを意味します。
アペンダーを Logger.getLogger("com.fizz").addAppender(newAppender) に追加すると、からのログ記録は、fizzルート ロガーとのすべてのアペンダーによって処理されますnewAppender
構成でロガーを作成するのではなく、システム内のすべての可能なカテゴリのハンドラーを提供するだけです。

おすすめ記事