1. Achieve the goal

Log The ​​log information of different businesses needs to be printed into different files,Generate a file every day or every hour.For example, the registration information is printed to register.log, a register-year, month, and day.log file is generated every morning, and the log of the login information is printed to a login.log file, and login-year, month, and day.log.

Maven configuration

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

3.Configuration file

by default,log4j will parse the src/main/resources/log4j.properties file, the following is the content in the log4.properties file.

####Parent logger
log4j.rootcategory=info, stdout
###Whether the child logger will output in the appender of the parent logger (log4j.rootcategory set above),Default is true
###When true:After the child logger is output, it will also be output in the parent logger
When ###is false:log will only be output in the current level of logger.
Will not be output in the parent logger
logdir =/users/zhengcanrui/work/test/logs
log4j.appender.stdout.layout.conversionpattern =%p \:%d {yyyymmdd hh \:mm \:ss}%c \:%m%n
##########Custom log output ###########
log4j.appender.file.file=${logdir} /file.log
log4j.appender.file.layout.conversionpattern =%d- [ts]%p%t%c-%m%n
##register module output
####is used to control the log information.
For example:the following information at least info will be output to the register file
log4j.logger.register=info, register
log4j.appender.register.file=${logdir} /register.log
log4j.appender.register.datepattern="_" yyyy-mm-dd ".log"
log4j.appender.register.layout.conversionpattern =%d%p [%c%l%l]-%m%n
##loginmodule output
log4j.logger.login=info, login
log4j.appender.login.file=${logdir} /login.log#The file name of the configuration file output,This format file will generate a file in the early morning,If i want to generate new files at other times, you can view the related configuration of datepattern
log4j.appender.login.datepattern="_" yyyy-mm-dd ".log"
log4j.appender.login.layout.conversionpattern =%d%p [%c]-%m%n
#Set this child logger output log not to output in the parent logger
#Log output control for open source libraries
#logger for spring
#logger for memcached
#logger for c3p0

Several attributes to note:

Log4j.additivity-module name:Set whether the child logger is output in the parent logger.

Name of module name:log4j.appender.module name.Attribute.

significance The meaning of each attribute in the configuration file,You can view:http://www.cnblogs.com/0201zcr/p/4761505.html


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] greatly greatly greatly greatly ");
register.debug ("2222");
register.info ("[register] Everyone Everyone Everyone");* /
//jjj.info("test ");
file.info ("334343");
register.info ("2222");


1) console

info:20160801 12:27:57 appender1:334343

info:20160801 12:27:57 register:2222

2) Generate files

3) Contents of the file

  • Previous JS method to get the keys pressed by the keyboard and display them on the page
  • Next Summary of methods for creating random strings with alphanumerics in javascript