log4j.xml示例

We can configure log4j using both property file as well as xml file. Today we will look into log4j.xml example and get the details of log4j.xml configuration.

我们可以使用属性文件和xml文件配置log4j。 今天,我们将研究log4j.xml示例,并获取log4j.xml配置的详细信息。

log4j.xml (log4j.xml)

Here is a typical log4j.xml example file.

这是一个典型的log4j.xml示例文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="https://jakarta.apache.org/log4j/"debug="false"><!-- console appender -->
<appender name="console" class="org.apache.log4j.ConsoleAppender"><param name="Target" value="System.out" /><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%-5p %c{1} - %m%n" /></layout><filter class="org.apache.log4j.varia.LevelMatchFilter"><param name="LevelToMatch" value="INFO" /><param name="AcceptOnMatch" value="true" /></filter><filter class="org.apache.log4j.varia.DenyAllFilter"/>
</appender><logger name="com.journaldev.log4j" additivity="false"><level value="DEBUG" /><appender-ref ref="console" />
</logger><root><priority value="DEBUG" /><appender-ref ref="console" />
</root></log4j:configuration>

If you are using Eclipse, you will notice that it’s not showing you XML element options, this is because it’s not able to find the log4j.dtd file. Something like below image.

如果您使用的是Eclipse,则会注意到它没有显示XML元素选项,这是因为它无法找到log4j.dtd文件。 如下图所示。

Change the DOCTYPE declaration to below to fix this. You would need internet connection to validate the log4j.xml file for this.

将DOCTYPE声明更改为以下即可解决此问题。 您将需要互联网连接来为此验证log4j.xml文件。

<!DOCTYPE log4j:configuration PUBLIC"-//APACHE//DTD LOG4J 1.2//EN" "https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

log4j.xml示例配置属性 (log4j.xml example configuration attributes)

Let’s look at the log4j.xml example configuration attributes – threshold and debug.

让我们来看看的log4j.xml例如配置属性- thresholddebug

<log4j:configuration xmlns:log4j="https://jakarta.apache.org/log4j/" threshold="info" debug="false">

The “threshold” attribute takes the minimum level below which all logging statements are disabled. This overrides all logger level configurations, use this only when you want to disable some lower level logging for all the loggers. In above case, all the trace and debug logs will be disabled even if any of the logger level is defined as debug.

“阈值”属性采用最低级别,在该级别之下,将禁用所有日志记录语句。 这将覆盖所有记录器级别的配置,仅当您要为所有记录器禁用一些较低级别的记录时才使用此配置。 在上述情况下,即使将任何记录器级别定义为调试,也将禁用所有跟踪和调试日志。

The “debug” attribute is used to switch the printing of internal log4j logging statements. You should keep it false to avoid bulk logging by log4j framework. If you will change it to true, you will get some logs like below.

“ debug”属性用于切换内部log4j日志语句的打印。 您应该将其保留为false,以避免通过log4j框架进行批量日志记录。 如果将其更改为true,则将获得以下类似的日志。

log4j: reset attribute= "false".
log4j: Threshold ="warn".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [com.journaldev.log4j] additivity to [false].
log4j: Level value for com.journaldev.log4j is  [TRACE].
log4j: com.journaldev.log4j level set to TRACE
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Setting property [target] to [System.out].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%-5p %c{1} - %m%n].
log4j: Adding appender named [console] to category [com.journaldev.log4j].
log4j: Level value for root is  [DEBUG].
log4j: root level set to DEBUG
log4j: Adding appender named [console] to category [root].

log4j.xml示例–追加程序 (log4j.xml example – appender)

The next important part is appender element. Here we define logging strategy, we can have multiple appenders defined in log4j.xml configuration.

下一个重要部分是appender元素。 在这里,我们定义了日志记录策略,我们可以在log4j.xml配置中定义多个附加程序。

Every appender has a distinct name and class to be used for logging. Then we set the parameters used by the appender class, every class has it’s own set of parameter that can be defined.

每个追加程序都有一个用于记录的独特名称和类。 然后我们设置appender类使用的参数,每个类都有自己的一组可定义的参数。

For example, “Target” is parameter in ConsoleAppender whereas “File”, “MaxFileSize”, “MaxBackupIndex” are parameters of RollingFileAppender.

例如,“ Target”是ConsoleAppender参数,而“ File”,“ MaxFileSize”,“ MaxBackupIndex”是RollingFileAppender参数。

<appender name="console" class="org.apache.log4j.ConsoleAppender"><param name="Target" value="System.out" /><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%-5p %c{1} - %m%n" /></layout>
</appender><!-- rolling file appender -->
<appender name="file" class="org.apache.log4j.RollingFileAppender"><param name="File" value="logs/main.log" /><param name="Append" value="true" /><param name="ImmediateFlush" value="true" /><param name="MaxFileSize" value="10MB" /><param name="MaxBackupIndex" value="5" /><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" /></layout>
</appender>

log4j.xml示例–布局 (log4j.xml example – layout)

Next part is layout where we define logging pattern through PatternLayout parameter ConversionPattern. This gets prefixed to all the logging messages. In above file appender, we are prefixing log messages with date, thread name, class name and line number. Below is a sample output for this pattern:

下一部分是布局,我们通过PatternLayout参数ConversionPattern定义日志记录模式。 这是所有日志消息的前缀。 在上面的文件附加器中,我们为日志消息添加了日期,线程名称,类名称和行号。 以下是此模式的示例输出:

2016-05-12 21:22:44,610 +0530 [main] DEBUG (Log4jExample.java:18) - XYZ Message

log4j xml配置过滤器和记录器 (log4j xml configuration filter and logger)

Next is the filter section where we can define our own custom filters or use any existing filters, for more details read log4j filters.

接下来是过滤器部分,我们可以定义自己的自定义过滤器或使用任何现有过滤器,有关更多详细信息,请阅读log4j过滤器 。

Next part is logger, we can have multiple loggers too. The “name” attribute is used to define the package where this logger will be used. Note that if there are multiple logger matches to a class, then most specific one is used.

下一部分是记录器,我们也可以有多个记录器。 “名称”属性用于定义将使用此记录器的软件包。 请注意,如果一个类有多个记录器匹配项,那么将使用最特定的一个。

log4j xml可加性 (log4j xml additivity)

The “additivity” attribute is a very important one – if it’s true then logging goes through hierarchy. For example if we have loggers defined as below.

“可加性”属性是一个非常重要的属性–如果为真,则日志记录将遍历层次结构。 例如,如果我们有如下定义的记录器。

<logger name="com.journaldev.log4j" additivity="true"><level value="DEBUG" /><appender-ref ref="jdbc" />
</logger><logger name="com.journaldev.log4j.model" additivity="true"><level value="DEBUG" /><appender-ref ref="file" />
</logger><root><priority value="DEBUG" /><appender-ref ref="console" />
</root>

And we are logging from some class in com.journaldev.log4j.model package, then it will be using appenders “file”, “jdbc” and “console” because of logger hierarchy. This can generate a lot of redundant logs, so we usually keep it false. It’s default value is “true” though.

而且我们从com.journaldev.log4j.model包中的某个类进行日志记录,由于日志记录器的层次结构,它将使用附加程序“ file”,“ jdbc”和“ console”。 这会生成很多冗余日志,因此我们通常将其保留为false。 它的默认值是“ true”。

A logger can use multiple appenders too.

记录器也可以使用多个追加程序。

<logger name="com.journaldev.log4j" additivity="true"><level value="TRACE" /><appender-ref ref="console" /><appender-ref ref="file" />
</logger>

log4j xml根记录器 (log4j xml root logger)

Finally root logger needs to be defined, this is the default level and appenders being used if there are no logger match found.

最后,需要定义根记录器,这是默认级别,如果找不到记录器匹配项,则使用附加程序。

Before I conclude this tutorial, one more point is that log4j framework looks for log4j.xml or log4j.properties file in the classpath. If you are using some different name for these configuration files then you need to configure them before using it.

在结束本教程之前,还有一点是,log4j框架在类路径中查找log4j.xmllog4j.properties文件。 如果为这些配置文件使用其他名称,则需要在使用它们之前对其进行配置。

DOMConfigurator.configure("log4j.xml");
// OR, don't use both
PropertyConfigurator.configure("log4j.properties");

For standalone java programs, you can do it in main method before program starts execution. For web applications, you can do it through Servlet Context Listener classes.

对于独立的Java程序,可以在程序开始执行之前在main方法中进行操作。 对于Web应用程序,可以通过Servlet Context Listener类来实现。

References:

参考文献

  • log4j PatternLayout optionslog4j PatternLayout选项
  • Referencing log4j.dtd in log4j.xml file在log4j.xml文件中引用log4j.dtd

翻译自: https://www.journaldev.com/10718/log4j-xml-example-configurations

log4j.xml示例

log4j.xml示例_log4j.xml示例配置相关推荐

  1. 可扩展标记语言XML之一:XML的概念、作用与示例

    哈喽大家好啊,乐字节小乐又来给大家分享Java技术文章了.上次已经讲完了Java多线程相关知识(可以看我博客文章), 这次文章将讲述可扩展标记语言XML 一. 标记语言 标记语言,是一种将文本(Tex ...

  2. xml格式是什么示例_什么是对抗示例?

    xml格式是什么示例 In recent times, Machine Learning (a subset of Artificial Intelligence) has been at the f ...

  3. php rss xml,php 一个完全面向对象的RSS/XML类的简单示例

    这篇文章主要为大家详细介绍了php 一个完全面向对象的RSS/XML类的简单示例,具有一定的参考价值,可以用来参考一下. 感兴趣的小伙伴,下面一起跟随512笔记的小编罗X来看看吧. 经测试代码如下: ...

  4. php post 获取xml,php 获取post的xml数据并解析示例

    这篇文章主要为大家详细介绍了php 获取post的xml数据并解析示例,具有一定的参考价值,可以用来参考一下. 对php获取post过来的xml数据并解析感兴趣的小伙伴,下面一起跟随512笔记的小编两 ...

  5. 项目构建之springboot集成lomback.xml,和log4j基于properties方式的日志配置记录

    文章目录 springboot集成lomback.xml 描述 在yml中定义的一些配置信息 创建logback-spring.xml文件 logback-spring.xml配置如下: **log4 ...

  6. Maven 2 + Hibernate 3.2 + MySQL示例(XML映射)

    注意 本文已过时,请参阅最新的Hibernate 3.6教程-Maven 3 + Hibernate 3.6 + Oracle 11g示例(XML映射) . 本快速指南向您展示如何使用Maven生成一 ...

  7. php xml写入数据库中,PHP读取xml并写入数据库示例

    $xml = simplexml_load_file('books.xml'); // print_r($xml); // echo " "; $t = $xml->to; ...

  8. php xml 格式化,php简单处理XML数据的方法示例

    本文实例讲述了php简单处理XML数据的方法.分享给大家供大家参考,具体如下: 把XML转换成对象直接调用里面的属性 $note=<< Tove Jani Reminder XML; $x ...

  9. java log4j 相对路径_log4j中配置日志文件相对路径[续集]

    这篇文章是基于流传在网上较为完整的关于"log4j中配置日志文件相对路径"问题的解决方法.该篇博文几经转载,流传至今.这是51cto博客里的一篇此文的转载[http://share ...

最新文章

  1. 2021年大数据ELK(四):Lucene的美文搜索案例
  2. Residual Networks
  3. 数据库中一些知识的整理
  4. Python基础教程:repr()与str() 的区别
  5. opera在我的博客发不了帖子,郁闷
  6. 关于getCurrentUrl的获取问题
  7. recycleview 使用详解,添加头部尾部,混合item,侧滑菜单,跳转到指定位置,实现九宫格布局
  8. 【计算机网络复习 数据链路层】3.6.5 PPP、HDLC
  9. iOS求职之OC面试题完整版---持续更新中...
  10. 无需训练 RNN 或生成模型,如何编写一个快速且通用的 AI “讲故事”项目?
  11. 运算放大器相关参数基本知识(一)
  12. Spring Bean初始化的几种方法以及执行顺序
  13. Xamarin.Forms入门-使用 Xamarin.Forms 来创建跨平台的用户界面
  14. atitit.404错误的排查流程总结
  15. 感恩节,《2012》,尖叫
  16. 计算机网络ospf流程图,计算机网络7-OSPF祥解.ppt
  17. MySQLdb 使用方法
  18. python写xml多了ns0_python – SUDS生成的XML不正确
  19. [经验分享]破解Word中保护文档密码
  20. STM32系列(HAL库)——F103C8T6使用SPI方式点亮OLED

热门文章

  1. (安卓)一键锁屏 ---亲测!
  2. 几种JS倒计时代码 【转】
  3. Hystrix入门与分析(一):初识Hystrix
  4. 2015 圣诞 限免软件分享
  5. JavaScript 弹出层,背景变暗
  6. 数据结构上机实践第五周项目3 - 括号的匹配
  7. RCNN学习笔记(1):Rich feature hierarchies for accurate object detection and semantic segmentation
  8. c语言文件操作常用函数及读写文件代码举列
  9. python百度云安装包_phython爬全百度评价_python安装包百度云
  10. opencv在linux设置环境变量,linux下设置opencv环境变量