Different log4j modules are output to different files.
1. Goals
Logs of different services must be printed to different files, and one file is generated every day or every hour. For example, the registration information is printed to register. log, a register-year, month, and day is generated every morning. log File. The log of the logon information is printed to a login. in the log file, login-year, month, and day. log.
Overall architecture:
2. maven Configuration
<?xml version="1.0" encoding="UTF-8"?><project xmlns="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 File
By default, log4j parsesSrc/main/resources/log4j. properties file. The content in the log4.properties file is as follows:#### Parent-level LoggerLog4j. rootCategory = info, stdout
### Whether the Sub-Logger will contain the parent-level Logger (the log4j set above. rootCategory) in the appender. The default value is true ### true: After the sub-level Logger is output, it will also be output in the parent-level Logger ### when it is false: log logs are only output in the current Logger, and log4j.addivity.org is not output in the parent Logger. apache = truelogdir =/Users/zhengcanrui/WORK/test/logs log4j. appender. stdout = org. apache. log4j. leleappenderlog4j. appender. stdout. layout = org. apache. log4j. patternLayoutlog4j. appender. stdout. layout. conversionPattern = % p \: % d {yyyyMMdd HH \: mm \: ss} % c \: % m % n ######### custom log output ########## 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 module output ### used to control the log information, such: at least INFO information is output to log4j in the register file. 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. additi.pdf. register = true # output by the login module # output by INFO and several times above
Log4j. logger. login = INFO, login
# Output log4j. appender. login = org. apache. log4j. DailyRollingFileAppender as a file type
# Output path
Log4j. appender. login. File =$ {logdir}/login. log
# Name of the file output from the configuration file. This format file will generate a file in the early morning. If you want to generate a new file at other times, you can view the related configuration log4j of DatePattern. appender. login. datePattern = '_ 'yyyy-MM-dd '. log'
# Output format
Log4j. appender. login. layout = org. apache. log4j. PatternLayout log4j. appender. login. layout. ConversionPattern = % d % p [% c]-% m % n
# Set this sub-Logger to output logs not in the parent logger to output log4j. additi.pdf. 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.logger.com. mchange = error org. apache. commons. httpclient = error org. apache. http = error
Several important attributes:
Log4j. additivity-Module name: Set whether the Sub-logger is output in the parent Logger.
Module name: log4j. appender. Module name. attribute.
Meaning of each attribute in the configuration file, you can view: http://www.cnblogs.com/0201zcr/p/4761505.html
The Logger class is the core of a log package. The Logger name is case-sensitive and has an inheritance relationship between names. Sub-names are prefixed by the parent name and separated by the dot ".". For example, x. y is the father Logger of x. y. z. The Logger system has a root logger, which is the ancestor of all logger. It always exists and cannot be obtained by name. It can be obtained by Logger. getRootLogger.
4. Testing
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 register = 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] ddd ");} public static void loginInfo () {login.info ("[login] 222");} public static void main (String [] args) {/* logInfo ("11 "); registerInfo (); loginInfo (); * // * login.info ("[login]"); register. debug ("2222"); register.info ("[register] Everyone everyone"); * // jjj.info ("test"); // log.info (222 ); FILE.info ("334343"); register.info ("2222 ");}}
Result:
1) Console
INFO:20160801 12:27:57 appender1: 334343INFO:20160801 12:27:57 register: 2222
2) generate a file
3) File Content
Thank you!