1.配置默认的 logger :rootLogger
#config root logger, this is the default logger
log4j.rootLogger = DEBUG,stdout,rolling_file
# Console Appender #
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.encoding=UTF-8
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d[%p] %c[%M(%L)] - %m%n
log4j.appender.stdout.Threshold=WARN
# Rolling File #
log4j.appender.rolling_file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.rolling_file.Threshold=WARN
log4j.appender.rolling_file.encoding=UTF-8
log4j.appender.rolling_file.File=data/logs/server.log
# create one file every hour
log4j.appender.rolling_file.DatePattern='_'yyyy-MM-dd_HH
log4j.appender.rolling_file.layout=org.apache.log4j.PatternLayout
log4j.appender.rolling_file.layout.ConversionPattern=%d[%p] %c[%M(%L)] - %m%n
作用:在类中获取 logger 的实例时,如果整个项目只配置了一个 rootLogger,那么在使用
private static Logger logger = Logger.getLogger(XXX.class);
的时候,默认获取的是 rootLogger
2.配置指定名称的 logger
# create logger for specified log
log4j.logger.cookieUser=INFO,cookieUser
# dont append info to rootLogger
log4j.additivity.cookieUser = false
log4j.appender.cookieUser=org.apache.log4j.DailyRollingFileAppender
log4j.appender.cookieUser.File=data/logs/cookieUser.log
log4j.appender.cookieUser.DatePattern='_'yyyy-MM-dd_HH
log4j.appender.cookieUser.layout=org.apache.log4j.PatternLayout
log4j.appender.cookieUser.layout.ConversionPattern=%m%n
作用:有时候我们可能想把不同的日志输出到不同的日志文件中去,这个时候我们就想使用指定名称的 logger
private static Logger cookieUserlogger = Logger.getLogger("cookieUser");
之后的所有此 logger 输出内容都会被输出到 data/logs/cookieUser.log 文件中
3.为指定包配置默认的 logger
# create logger for specified class
log4j.logger.com.log.test1=INFO,crowdInfoRefresher
log4j.additivity.crowdInfoRefresher = false
log4j.appender.crowdInfoRefresher=org.apache.log4j.DailyRollingFileAppender
log4j.appender.crowdInfoRefresher.File=data/logs/test1.log
log4j.appender.crowdInfoRefresher.DatePattern='_'yyyy-MM-dd
log4j.appender.crowdInfoRefresher.layout=org.apache.log4j.PatternLayout
log4j.appender.crowdInfoRefresher.layout.ConversionPattern=%m%n
作用:有时候我们想把指定包里面的日志输出到指定的文件里面,这个时候我们就需要用到这个配置了。
这个时候通过
private static Logger logger = Logger.getLogger(XXX.class);
获取的 logger 将不再是 rootLogger 而是为指定包配置的 logger
当然你如果想要在此使用 rootLogger 那么你就需要使用
private static Logger logger = Logger.getRootLogger();
来获取 rootLogger。
参数解释:
DailyRollingFileAppender:按照天生成日志,需要配合 DatePattern 参数来指定是按照天生成日志还是安装小时生成日志。
按照天:log4j.appender.crowdInfoRefresher.DatePattern='_'yyyy-MM-dd
按照小时:log4j.appender.cookieUser.DatePattern='_'yyyy-MM-dd_HH
示例:log4j.properties 文件
#config root logger, this is the default logger log4j.rootLogger = DEBUG,stdout,rolling_file # Console Appender # log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.encoding=UTF-8 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d[%p] %c[%M(%L)] - %m%n log4j.appender.stdout.Threshold=WARN # Rolling File # log4j.appender.rolling_file=org.apache.log4j.DailyRollingFileAppender log4j.appender.rolling_file.Threshold=WARN log4j.appender.rolling_file.encoding=UTF-8 log4j.appender.rolling_file.File=data/logs/server.log # create one file every hour log4j.appender.rolling_file.DatePattern='_'yyyy-MM-dd_HH log4j.appender.rolling_file.layout=org.apache.log4j.PatternLayout log4j.appender.rolling_file.layout.ConversionPattern=%d[%p] %c[%M(%L)] - %m%n # create logger for specified log log4j.logger.cookieUser=INFO,cookieUser # dont append info to rootLogger log4j.additivity.cookieUser = false log4j.appender.cookieUser=org.apache.log4j.DailyRollingFileAppender log4j.appender.cookieUser.File=data/logs/cookieUser.log log4j.appender.cookieUser.DatePattern='_'yyyy-MM-dd_HH log4j.appender.cookieUser.layout=org.apache.log4j.PatternLayout log4j.appender.cookieUser.layout.ConversionPattern=%m%n # create logger for specified class log4j.logger.com.log.test1=INFO,crowdInfoRefresher log4j.additivity.crowdInfoRefresher = false log4j.appender.crowdInfoRefresher=org.apache.log4j.DailyRollingFileAppender log4j.appender.crowdInfoRefresher.File=data/logs/test1.log log4j.appender.crowdInfoRefresher.DatePattern='_'yyyy-MM-dd log4j.appender.crowdInfoRefresher.layout=org.apache.log4j.PatternLayout log4j.appender.crowdInfoRefresher.layout.ConversionPattern=%m%n
测试类:LogUtils.java
package com.log.test; import org.apache.log4j.Logger; public class LogUtils { /** * 获取 RootLogger * 此 logger 的输出目的地就是 rootLogger 配置的目的地 */ private static Logger roorInfoLogger = Logger.getRootLogger(); /** * 获取默认的 logger,如果此包没有配置默认的 logger,那么获取的 logger * 将会是 rootLogger,如果此包配置了 默认的 logger,那么获取的将会是配置的默认的 logger */ private static Logger logger = Logger.getLogger(LogUtils.class); /** * 获取指定的名称的 logger */ private static Logger cookieUserlogger = Logger.getLogger("cookieUser"); public static void main(String[] args){ // 在此 roorInfoLogger 跟 logger 其实用的同一个 logger roorInfoLogger.info("哈哈哈哈:roorInfoLogger:info"); roorInfoLogger.warn("哈哈哈哈:roorInfoLogger"); cookieUserlogger.info("哈哈哈哈:cookieUser"); logger.warn("啦啦啦啦啦"); System.out.println("aaaaa"); } }
测试类:
package com.log.test1; import org.apache.log4j.Logger; public class Test1 { /** * 如果 log4j 为本类所在的包配置了 logger,那么 logger 获取的是配置的那个 logger * 如果没有为此类所在的包配置 logger 那么 logger 获取的将会是 rootLogger */ // private static Logger logger = Logger.getRootLogger(); private static Logger logger = Logger.getLogger(Test1.class); public static void main(String[] args) { logger.warn("Test1:haha"); } }