[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:
- if (logger.isDebugEnabled()) {
- logger.debug("Logging in user " + user.getName() + " with birthday " + user.getBirthdayCalendar());
- }
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:
- public static Logger logger = LogManager.getFormatterLogger("Foo");
- logger.debug("Logging in user %s with birthday %s", user.getName(), user.getBirthdayCalendar());
- logger.debug("Logging in user %1$s with birthday %2$tm %2$te,%2$tY", user.getName(), user.getBirthdayCalendar());
- logger.debug("Integer.MAX_VALUE = %,d", Integer.MAX_VALUE);
- 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的格式化输出。
- [log4j] 格式化eventLog
- Log4j格式化说明
- C# EventLog
- Android EventLog
- Log4j学习Layout格式化器
- Log4j扩展使用--日志格式化器Layout
- Log4j扩展使用--日志格式化器Layout
- Log4j扩展使用--日志格式化器Layout
- C#学习:使用EventLog
- Get-EventLog 帮助信息
- Windows的EventLog
- WYTC-C#-EventLog元件
- Eventlog to Syslog Utility
- Winform 组件EventLog
- C# EventLog类应用
- 【framework】EventLog分析
- 【framework】EventLog分析
- Android EventLog简介
- inux输入子系统(8)--input core
- 响应式Web设计
- 利用devcon安装驱动
- 文本文件和二进制文件区别及java中字节流和字符流归纳
- Reactor模式,或者叫反应器模式
- [log4j] 格式化eventLog
- OSS研究
- 配置cakephp的debugkit
- 电影网站站架构(第一版)
- MFC CDialogEx.DoModal 弹出Dialog显示图标
- spring mvc返回json格式错误
- Python学习笔记之中文注释
- cgroup--cpu spuset 子系统功能测试
- QT编译错误 No such file or directory #include<QApplication>解决方法