SLF4J and Log4j Shared (Maven) configuration

Source: Internet
Author: User

Briefly:

Add logger log output, below is the configuration information for the memo


Steps:

1. Add dependency in Maven's Porn.xml file as follows

<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency>

You will then add three packages,

2. Then add log4j.properties under the project

The project path is as follows,

Log4j.properties

#config root Loggerlog4j.rootlogger = info,system.outlog4j.appender.system.out= org.apache.log4j.consoleappenderlog4j.appender.system.out.layout= Org.apache.log4j.patternlayoutlog4j.appender.system.out.layout.conversionpattern=minaserver Logger-->%5p{%F: %l}-%m%n#config this project.file loggerlog4j.logger.thisproject.file=info, thisproject.file.outlog4j.appender.thisproject.file.out= org.apache.log4j.dailyrollingfileappenderlog4j.appender.thisproject.file.out.file= Logcontentfile.loglog4j.appender.thisproject.file.out.layout=org.apache.log4j.patternlayout




3. Add in Code

private static final Logger Logger = Loggerfactory.getlogger (Minaserver.class);



The next thing you can do is output,

Logger.info ("Now {}", "Starting server");



The output is as follows:


If you want to output the logger file:

Log4j.rootlogger=info,r,stdout Log4j.appender.stdout=org.apache.log4j.consoleappender log4j.appender.stdout.layout=org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern= logger-->%5p{%f:%l}-%m%n Log4j.appender.r=org.apache.log4j.dailyrollingfileappender log4j.appender.R.File=. /logs/loglog4j.appender.r.layout=org.apache.log4j.patternlayoutlog4j.appender.r.layout.conversionpattern= logger-->%5p{%f:%l}-%m%n





Here is the configuration information for the log4j.properties, thanks to the resources provided by the predecessor

#输出格式
#%m the message specified in the output code
#%p output priority, i.e. Debug,info,warn,error,fatal
#%r output the number of milliseconds that the log information is consumed from the application boot to output
#%c the class that the output belongs to, which is usually the full name of the class
#%t output The name of the thread that generated the log event
#%n output a carriage return newline character, Windows platform is "\ r \ n", Unix platform is "\ n"
#%d the date or time of the output log time, the default format is ISO8601, or you can specify a format later, such as:%d{yyyy MMM dd hh:mm:ss,sss}, output similar to: October 18, 2002 22:10:28,921
#%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)

Examples of formatting:
LOG4J.APPENDER.THISPROJECT.FILE.OUT.LAYOUT.CONVERSIONPATTERN=LOG4J--&GT;%D{YYYY MMM DD HH:mm:ss,SSS}%5p{%F\:%L}-% m%n


Attention:

1. Small knowledge of information formatting
These parameters may be mixed with some numbers such as:%5p it means how many spaces are added before the output of this parameter, and the function of "\" Inside is the escape character

2.log4j.properties File Drop Location
(1) In a Java project project, it is placed in the root directory of the project and not in the SRC directory of the project.
(2) in the Javaweb project, it is placed in the SRC directory, because this time Tomcat will go to the default loading it, do not need us to manually load the log4j configuration file, only need to configure the configuration to obtain their own required logger instance can be, So we know if we're not using a tomcat container, do we need to load it manually or at least configure it? such as using non-Apache servers such as WebSphere.

3. Location configuration for logs inside Java Web projects
(1) If you want to specify the location of the log file is the Log.txt file under the D drive.
Log4j.appender.thisproject.file.out.file=d:\\log.txt
(2) If the location of the specified log file is a file under the working directory of the current Tomcat
Log4j.appender.thisproject.file.out.file=${catalina.home}/logs/logs_tomcat.log

Loading mechanism of 4.log4j

Log4j.properties loading mechanism? In fact Log4j.properties is just log4j configuration file. When the program starts, the log4j component reads Log4j.properties, and does not differ greatly from reading the normal configuration file. Get some log4j property values for the user configuration, and call the method that you want to set for the Log4j property.

Think of Log4j.properties as a struts.xml or a hibernate-cfg.xml. But the difference is that unlike struts and hibernate, log4j is not a standalone component and cannot be initialized by itself, and it is generally what components need it to initialize. For example, Hibernate's default log component is log4j, which initializes the log4j when hibernate is initialized. If you do not configure Log4j.properties,hibernate will throw an exception, but you can still initialize normally.


SLF4J and Log4j Shared (Maven) configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.