Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do we need root and logger in log4j.xml

Tags:

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 ?

like image 486
Parameswar Avatar asked Oct 12 '12 14:10

Parameswar


People also ask

What are Appenders and loggers in log4j?

Log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as console, files, NT event logs, Swing components, JMS, remote UNIX syslog daemons, sockets, etc.

What is root level in logging?

By default, the root log level is WARN, so every log with lower level (for example via logging.info("info") ) will be ignored. Another particularity of the root logger is that its default handler will be created the first time a log with a level greater than WARN is logged.

What are the three most important components of log4j?

log4j has three main components: loggers: Responsible for capturing logging information. appenders: Responsible for publishing logging information to various preferred destinations. layouts: Responsible for formatting logging information in different styles.

What does root level mean log4j?

The level of the root logger is defined as DEBUG, The DEBUG appender named FILE to it. The appender FILE is defined as org. apache. log4j.


1 Answers

It is enough.

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:

  • 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> 

You should read more about the log4j basics.

like image 188
Artem Shafranov Avatar answered Sep 19 '22 14:09

Artem Shafranov