Log4j - PatternLayout2024 年 8 月 29 日 | 阅读 3 分钟 Log4j 提供了 org.apache.log4j.PattrernLayout 类,用于根据模式以特定格式生成您的日志信息。 PatternLayout 扩展了抽象的 org.apache.log4j.Layout 类,并重写了 format() 方法,以根据提供的模式构造日志信息。 PatternLayout 也是一个简单的布局对象,它提供了 Bean 属性,即 conversionPattern,可以使用配置文件进行设置 conversionPattern:此属性用于设置转换模式。 默认为 %r [%t] %p %c %x - %m%n 模式转换字符让我们看看下表描述了转换模式中使用的字符以及我们可以在自定义模式中使用的所有其他字符
格式修饰符默认情况下,相关信息显示为普通输出。 但是,log4j 提供了格式修饰符; 借助它,可以更改最大字段宽度、最小字段宽度和对齐方式。 让我们看一些修饰符
PatternLayout 示例让我们看一个 Patternlayout 的简单示例。 以下是 PatternLayout 的简单配置文件 log4j.properties Log4jExample.java 当您编译并运行上述程序时,您将在 c:/usr/home/log4j 目录中获得一个 log.out 文件,其中将包含以下日志信息 输出 2019-09-16-main--DEBUG-Log4jExample:Hello this is an debug message 2019-09-16-main--INFO - Log4jExample:Hello this is an info message |
我们请求您订阅我们的新闻通讯以获取最新更新。