[log4j] 格式化eventLog

格式化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's Formatter. 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的格式化输出。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值