为什么在log4j.xml中需要root和logger [英] why do we need root and logger in log4j.xml

查看:988
本文介绍了为什么在log4j.xml中需要root和logger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅,如果这个问题太琐碎.我对log4j完全陌生.我已经看到有两个标记和标记,它们分别指向各种附加程序. 假设我要在代码库中将信息记录在文件中,将其发送到我的电子邮件中,然后将其打印到控制台.我希望将级别设置为信息.拥有一个引用了三个附加程序(文件,电子邮件和控制台)的单个标签是否足够?为什么我们需要相同的另一个标签?

pardon if the question is too trivial. I am completely new to log4j. I have seen that there are two tags and tags, which refer to various appenders. Say i want to log the information in my code base in a file, send it to my email and print it to console. I want the level set to info. Isnt it enough to have a single tag which has references to the three appenders ?( file, email and the console) why do we need another for the same ?

推荐答案

足够了.

在log4j中,记录器与程序包或有时与特定类相关联.记录器的包/类由属性名称"定义.记录器在其程序包以及所有子程序包及其类中记录消息.唯一的例外是root记录器,该记录器记录了应用程序中所有类的消息.

In log4j a logger is associated with a package or sometimes with a particular class. Package/class of a logger is defined by the attribute "name". A logger logs messages in its package and also in all the child packages and their classes. The only exception is the root logger that logs messages for the all classes in the application.

记录器也具有级别,并且可能具有一个或多个附加器(记录目标).

A logger also has level and may have one or many appenders (logging destinations) attached to it.

在下一个示例中,我们有两个记录器:

In the next example we have two loggers:

  • 将所有程序包中信息级别为INFO或更高的消息记录到各个目标的根记录器:控制台,电子邮件和文件,
  • "com.foo"记录器,将"com.foo"程序包及其子程序包中WARN或更高级别的消息记录到另一个文件中.

  • the root logger that logs messages with level INFO or above in the all packages to the various destinations: console, e-mail and file,
  • "com.foo" logger that logs messages with level WARN or above in package "com.foo" and its child packages to the another file.

<log4j:configuration>
    <!-- Declaration of appenders FILE, MAIL, CONSOLE and ANOTHERFILE -->
    ...
    <!-- -->

    <logger name="com.foo">
        <level value="warn"/>
        <appender-ref ref="ANOTHERFILE" /> 
    </logger>
    <root> 
        <priority value ="info" />
        <appender-ref ref="FILE" /> 
        <appender-ref ref="MAIL" />
        <appender-ref ref="CONSOLE" /> 
    </root>
</log4j:configuration>

您应该阅读更多有关log4j基本知识的信息.

You should read more about the log4j basics.

这篇关于为什么在log4j.xml中需要root和logger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆