1. Achieve the goal
Log information for different businesses needs to be printed to a different file, and a file is generated daily or hourly. For example, the registered information printed to Register.log, every morning to generate a register-month and a day. log files, log information logs printed to a Login.log file, login-month and day. Log.
2. Maven Configuration
<?xml version= "1.0" encoding= "UTF-8"?> <project "xmlns="
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>
3. configuration file
By default, log4j resolves the src/main/resources/log4j.properties file, and the following is the contents of the Log4.properties file.
### #父级Logger Log4j.rootcategory=info,stdout ### Logger Whether the logger inside the parent log4j.rootcategory (Appender above) is exported, and the default is True
When ### is true: the logger output of the child level will also output ### to false in the parent logger: Log log will only output in the current level of logger, not in the parent logger output Log4j.addivity.org.apache=true Logdir=/users/zhengcanrui/work/test/logs log4j.appender.stdout=
Org.apache.log4j.ConsoleAppender Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=%P\:%D{YYYYMMDD Hh\:mm\:ss}%c\:%m%n ######## #自定义日志输出 ##########
Log4j.appender.file=org.apache.log4j.dailyrollingfileappender Log4j.appender.file.file=${logdir}/file.log Log4j.appender.file.layout=org.apache.log4j.patternlayout Log4j.appender.file.layout.conversionpattern=%d-[ts]% P%t%c-%m%n # #register模块输出 to control the log information, such as: The following information is at least info will be exported to the register file Log4j.logger.register=info,register log4j.
Appender.register=org.apache.log4j.dailyrollingfileappender Log4j.appender.register.file=${logdir}/register.log Log4j.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%n log4j.additivity.register=true # #login模块输出
Log4j.logger.login=info,login Log4j.appender.login=org.apache.log4j.dailyrollingfileappender
log4j.appender.login.file=${logdir}/login.log# profile Output file name, this format file will generate a file in the morning, want to generate new files at other times to view the Datepattern 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 # Set this child logger output log is not exported in parent level logger Log4j.additivity.login=false # Open Source Library log Output Control # logger for Spring Log4j.logger.org.springframework=error # Logger for MEMCA Ched Log4j.logger.com.danga.memcached=error # Logger for c3p0 log4j.logger.com.mchange=error ORG.APACHE.COMMONS.HTTPC Lient=error Org.apache.http=error
Several attributes to note:
log4j.additivity-Module Name: Sets whether the child logger is output in the parent logger.
Name of module name: Log4j.appender. module name. Attribute.
The various attribute meanings in the configuration file, you can view: http://www.cnblogs.com/0201zcr/p/4761505.html
4. Test class
Import Org.apache.log4j.Logger;
Import Org.slf4j.LoggerFactory;
/** * Created by Zhengcanrui on 16/7/27. * * Public class Loggerutil {private static final Logger file = Logger.getlogger ("file"); private static final Logger Regi
Ster = Logger.getlogger ("register");
Private static final Logger login = Logger.getlogger ("login");
private static final Logger Goldcoin = Logger.getlogger ("Goldcoin");
Private static final Logger recharge = Logger.getlogger ("recharge");
private static final Logger JJJ = Logger.getlogger (LoggerUtil.class.getName ());
private static final Logger FILE = Logger.getlogger ("Appender1");
private static Org.apache.log4j.Logger log = Logger.getlogger (Loggerutil.class); public static void Loginfo (String log) {file.info (log),} public static void Registerinfo () {register.info ("[Register] D
DD ");} public static void Logininfo () {login.info ("[Login] 222"),} public static void Main (string[] args) {/*loginfo ("one");
Egisterinfo (); Logininfo (); */*login.info ("[Login]" bigLarge greatly greatly ");
Register.debug ("2222");
Register.info ("[Register] Everyone is all for all"); * *//jjj.info ("test");
Log.info (222);
File.info ("334343");
Register.info ("2222"); }
}
Results:
1) Console
info:20160801 12:27:57 appender1:334343
info:20160801 12:27:57 register:2222
2) Generate files
3 contents of the document
The above is a small set to introduce log4j different modules output to different documents, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!