log4j different modules output to different files

Source: Internet
Author: User
Tags log log

1. Achieve the goal

Log information for different businesses needs to be printed to a different file, generating a file every day or every hour. For example, the registered information is printed to Register.log, and a register-day is generated every morning. log files, logs of login information are printed into a login.log file, login-month day. Log.

2. Maven Configuration

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelversion>4.0.0</modelversion>    <groupId>test</groupId>    <Artifactid>Common</Artifactid>    <version>1.0-snapshot</version>    <Dependencies>        <Dependency>            <groupId>Org.slf4j</groupId>            <Artifactid>Slf4j-api</Artifactid>            <version>1.7.6</version>        </Dependency>        <Dependency>            <groupId>Org.slf4j</groupId>            <Artifactid>Slf4j-log4j12</Artifactid>            <version>1.7.6</version>        </Dependency>    </Dependencies></Project>

3. Configuration files

By default, log4j resolves the src/main/resources/log4j.properties file, and the following is the contents of the Log4.properties file.

# # # #父级Loggerlog4j. rootcategory=info,stdout### Child Logger If the appender inside the parent logger (log4j.rootcategory set above) is output, the default is True # # # is true: sub-level logger output will also be in the parent logger inside Output # # # is false: Log log will only be in the current level of logger output, does not output log4j.addivity.org.apache=truelogdir=/users/zhengcanrui/work/test/logs in the parent logger log4j.appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=%P\:%D{YYYYMMDD HH\:mm\:ss}%c\:%m% n######## #自定义日志输出 ########## log4j.appender.file= Org.apache.log4j.dailyrollingfileappenderlog4j.appender.file.file=${logdir}/file.loglog4j.appender.file.layout =org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversionpattern=%d-[ts]%p%t%c-%m%n# #register模块输出 # # # # is used to control the log information, such as: The following information is at least info will be output to the register file Log4j.logger.register=info,register log4j.appender.register= Org.apache.log4j.DailyRollingFileAppender log4j.appender.register.file=${logdir}/ Register.loglog4j.appender.register.datepattern= ' _ ' yyyy-mm-dd '. log' Log4j.appender.register.layout=org.apache.log4j.patternlayout Log4j.appender.register.layout.ConversionPattern =%d%p [%c%l%l]-%m%nlog4j.additivity.register=true # #login模块输出 Log4j.logger.login=info,login log4j.appender.login= Org.apache.log4j.DailyRollingFileAppender Log4j.appender.login.file=${logdir}/login.log
#配置文件输出的文件命名, this format file will generate a file in the wee hours and want to generate new files at other times to view Datepattern's related configuration log4j.appender.login.datepattern= ' _ ' yyyy-mm-dd '. Log ' Log4j.appender.login.layout=org.apache.log4j.patternlayout log4j.appender.login.layout.conversionpattern=%d %p [%c]-%m%n# Setting this sub logger output log is not in the parent level logger output Log4j.additivity.login=false # Log output control for open source libraries # logger for spring Log4j.logger . Org.springframework=error # Logger for MemCached Log4j.logger.com.danga.memcached=error # logger for c3p0 LOG4J.L Ogger.com.mchange=error Org.apache.commons.httpclient=error Org.apache.http=error

A few attributes to note:

log4j.additivity-Module Name: Sets whether the sub-logger is output in the parent logger.

Name of module: Log4j.appender. Module name. Properties.

The various attribute meanings in the configuration file can be viewed: http://www.cnblogs.com/0201zcr/p/4761505.html

4. Test class

ImportOrg.apache.log4j.Logger;Importorg.slf4j.LoggerFactory;/*** Created by Zhengcanrui on 16/7/27.*/ Public classLoggerutil {Private Static FinalLogger file = Logger.getlogger ("File"); Private Static FinalLogger Register = logger.getlogger ("register"); Private Static FinalLogger login = Logger.getlogger ("Login"); Private Static FinalLogger Goldcoin = Logger.getlogger ("Goldcoin"); Private Static FinalLogger recharge = Logger.getlogger ("Recharge"); Private Static FinalLogger JJJ = Logger.getlogger (loggerutil.class. GetName ()); Private Static FinalLogger FILE = Logger.getlogger ("Appender1"); Private StaticOrg.apache.log4j.Logger log = Logger.getlogger (loggerutil.class);  Public Static voidLoginfo (String log) {file.info (log); }     Public Static voidRegisterinfo () {Register.info ("[Register] DDD" ); }     Public Static voidLogininfo () {Login.info ("[Login] 222" ); }     Public Static voidMain (string[] args) {/*Loginfo ("11");        Registerinfo (); Logininfo ();*/        /*login.info ("[login] greatly greatly greatly");        Register.debug ("2222"); Register.info ("[Register] everyone for all");*/        //jjj.info ("test"); //Log.info (222);File.info ("334343"); Register.info ("2222"); }}

Results:

1) Console

info:20160801 12:27:57 appender1:334343INFO:20160801 12:27:57 register:2222

2) Generate file

3) Contents of the file

Thanks: Thank you for reading!

log4j different modules output to different files

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.