[log4j] 格式化eventLog

来源:互联网 发布:上海汇钰网络通信设备 编辑:程序博客网 时间:2024/04/28 23:11

格式化log msg

Substituting Parameters

Frequently the purpose of logging is to provide information about what is happening in the system, which requires including information about the objects being manipulated. In Log4j 1.x this could be accomplished by doing:

  1. if (logger.isDebugEnabled()) {
  2. logger.debug("Logging in user " + user.getName() + " with birthday " + user.getBirthdayCalendar());
  3. }

Doing this repeatedly has the effect of making the code feel like it is more about logging than the actual task at hand. In addition, it results in the logging level being checked twice; once on the call to isDebugEnabled and once on the debug method. A better alternative would be:

logger.debug("Logging in user {} with birthday {}", user.getName(), user.getBirthdayCalendar());

With the code above the logging level will only be checked once and the String construction will only occur when debug logging is enabled.

如source code

import org.apache.logging.log4j.Level;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;public class HelloWorld {    private static Logger logger = LogManager.getLogger("HelloWorld");    public static void main(String[] args) {        logger.debug("logginer is { } with {} .", "user", "QQ");
        logger.debug("logginer is {} with {} .", "user", "QQ");
 }
}
运行结果就是:

logginer is { } with [user, QQ] .

logginer is userwith QQ .

因此,使用{}做字符替换的占位符。 如果是多个字符串对应一个占位符的话,这些参数会被组织成一个字符数组。

函数原型

 /**   * Logs a message with parameters at the {@link Level#DEBUG DEBUG} level.   * @param message the message to log; the format depends on the message factory.   * @param params parameters to the message.   * @see #getMessageFactory()   */  void debug(String message, Object... params);


Formatting Parameters

Substituting parameters leaves formatting up to you if toString() is not what you want. To facilitate formatting, you can use the same format strings as Java'sFormatter. For example:

  1. public static Logger logger = LogManager.getFormatterLogger("Foo");
  2.  
  3. logger.debug("Logging in user %s with birthday %s", user.getName(), user.getBirthdayCalendar());
  4. logger.debug("Logging in user %1$s with birthday %2$tm %2$te,%2$tY", user.getName(), user.getBirthdayCalendar());
  5. logger.debug("Integer.MAX_VALUE = %,d", Integer.MAX_VALUE);
  6. logger.debug("Long.MAX_VALUE = %,d", Long.MAX_VALUE);

To use a formatter Logger, you must call one of the LogManager getFormatterLogger method. The output for this example shows that Calendar toString() is verbose compared to custom formatting:

例如:

import org.apache.logging.log4j.Level;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;public class HelloWorld {    private static Logger logger = LogManager.getFormatterLogger(HelloWorld.class.getName());    public static void main(String[] args) {        logger.debug("logginer is { } with {} .", "user", "QQ");        logger.debug("logginer is {} with {} .", "user", "QQ");        logger.debug("logginer is with %s .", "user", "QQ");        logger.debug("logginer is %s with %s .", "user", "QQ");        logger.debug("Integer: %,d. ",Integer.MAX_VALUE );        logger.debug("Long: %,d. ",Long.MAX_VALUE );    }} 

输出结果:

logginer is { } with {} .
logginer is {} with {} . 这两行都没有替换,看来用{}表示的占位符对于getFormatterLogger类是不work的。
logginer is with user .
logginer is user with QQ .
Integer: 2,147,483,647.
Long: 9,223,372,036,854,775,807

getFormatterLogger能提供类似于C的格式化输出。