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

[HTML]View Plaincopy
  1. <dependency>
  2. <groupId>org.slf4j</groupId>
  3. <artifactid>slf4j-log4j12</artifactid>
  4. <version>1.7.2</version>
  5. </Dependency>


You will then add three packages,

2. Then add log4j.properties under the project

The project path is as follows,

Log4j.properties

[HTML]View Plaincopy
  1. #config Root Logger
  2. Log4j.rootlogger = Info,system.out
  3. log4j.appender.system.out=Org.apache.log4j.ConsoleAppender
  4. log4j.appender.system.out.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.system.out.layout.conversionpattern=minaserver logger-->%5p{%f:%l}-%m%n
  6. #config this project.file logger
  7. log4j.logger.thisproject.file=info,thisproject.file.out
  8. log4j.appender.thisproject.file.out=Org.apache.log4j.DailyRollingFileAppender
  9. log4j.appender.thisproject.file.out.file=LogContentFile.log
  10. log4j.appender.thisproject.file.out.layout=org.apache.log4j.PatternLayout

3. Add in Code

[Java]View Plaincopy
    1. Private static final Logger Logger = Loggerfactory.getlogger (minaserver.   Class);


The next thing you can do is output,

[HTML]View Plaincopy
    1. Logger.info ("Now {}", "Starting server");

The output is as follows:

If you want to output the logger file:

[Plain]View Plaincopy
    1. Log4j.rootlogger=info,r,stdout
    2. Log4j.appender.stdout=org.apache.log4j.consoleappender
    3. Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
    4. log4j.appender.stdout.layout.conversionpattern=logger-->%5p{%f:%l}-%m%n
    5. Log4j.appender.r=org.apache.log4j.dailyrollingfileappender
    6. Log4j.appender.r.file=. /logs/log
    7. Log4j.appender.r.layout=org.apache.log4j.patternlayout
    8. log4j.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.