log4j.properties配置的原文:
# # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # # Define some default values that can be overridden by system properties. # # For testing, it may also be convenient to specify # -Dflume.root.logger=DEBUG,console when launching flume. #flume.root.logger=info,console flume.root.logger=INFO,LOGFILE,console,debug flume.log.dir=../logs flume.log.file=run_info.log log4j.logger.org.apache.flume.lifecycle = INFO log4j.logger.org.jboss = WARN log4j.logger.org.mortbay = INFO log4j.logger.org.apache.avro.ipc.NettyTransceiver = WARN log4j.logger.org.apache.hadoop = INFO # Define the root logger to the system property "flume.root.logger". log4j.rootLogger=${flume.root.logger} # Stock log4j rolling file appender # Default log rotation configuration log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender log4j.appender.LOGFILE.MaxFileSize=50MB log4j.appender.LOGFILE.MaxBackupIndex=5 log4j.appender.LOGFILE.File=${flume.log.dir}/${flume.log.file} log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss.SSS} %-5p [%t] (%C.%M:%L) %x - %m%n # Warning: If you enable the following appender it will fill up your disk if you don't have a cleanup job! # This uses the updated rolling file appender from log4j-extras that supports a reliable time-based rolling policy. # Add "DAILY" to flume.root.logger above if you want to use this log4j.appender.DAILY=org.apache.log4j.rolling.RollingFileAppender log4j.appender.DAILY.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy log4j.appender.DAILY.rollingPolicy.ActiveFileName=${flume.log.dir}/${flume.log.file} log4j.appender.DAILY.rollingPolicy.FileNamePattern=${flume.log.dir}/${flume.log.file}.%d{yyyy-MM-dd} log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout log4j.appender.DAILY.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss.SSS} %-5p [%t] (%C.%M:%L) %x - %m%n # console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.target=System.err log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss.SSS} (%t) [%F:%L] %m%n #warn log4j.logger.warn=warn log4j.appender.warn=org.apache.log4j.RollingFileAppender log4j.appender.warn.MaxFileSize=50MB log4j.appender.warn.MaxBackupIndex=5 log4j.appender.warn.File=${flume.log.dir}/run_warn.log log4j.appender.warn.Append=true log4j.appender.warn.Threshold =WARN log4j.appender.warn.layout=org.apache.log4j.PatternLayout log4j.appender.warn.layout.ConversionPattern=[ %p ] %-d{yyyy-MM-dd HH:mm:ss.SSS} [%F:%L] %m%n #error log4j.logger.error=error log4j.appender.error=org.apache.log4j.RollingFileAppender log4j.appender.error.MaxFileSize=50MB log4j.appender.error.MaxBackupIndex=5 log4j.appender.error.File=${flume.log.dir}/run_error.log log4j.appender.error.Append=true log4j.appender.error.Threshold =ERROR log4j.appender.error.layout=org.apache.log4j.PatternLayout log4j.appender.error.layout.ConversionPattern=[ %p ] %-d{yyyy-MM-dd HH:mm:ss.SSS} [%F:%L] %m%n #debug log4j.logger.debug=debug log4j.appender.debug=org.apache.log4j.RollingFileAppender log4j.appender.debug.MaxFileSize=50MB log4j.appender.debug.MaxBackupIndex=5 log4j.appender.debug.File=${flume.log.dir}/run_debug.log log4j.appender.debug.Append=true log4j.appender.debug.Threshold =DEBUG log4j.appender.debug.layout=org.apache.log4j.PatternLayout log4j.appender.debug.layout.ConversionPattern=[ %p ] %-d{yyyy-MM-dd HH:mm:ss.SSS} [%F:%L] %m%n
让日志回滚,只需要配置它的类型及文件大小(MaxFileSize--默认是10M)、文件数(MaxBackupIndex--默认是1)
如:
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender log4j.appender.LOGFILE.MaxFileSize=2MB log4j.appender.LOGFILE.MaxBackupIndex=5 log4j.appender.LOGFILE.File=${flume.log.dir}/${flume.log.file} log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss.SSS} %-5p [%t] (%C.%M:%L) %x - %m%n
实现效果:
备份文件数是5个,大小是2M。当run_info.log文件达到2M时,run_info.log.5 会被删除,其他文件会被重命名:run_info.log-->run_info.log.1,run_info.log.1-->run_info.log.2,以此类推。
除了配置之外,还有需要注意的是jar包
log4j-1.2.16.jar
slf4j-api-1.6.4.jar
slf4j-log4j12-1.6.1.jar
之前上个一个当,把slf4j-simple-1.6.4.jar加进去了,一直不打日志。找了好久才发现是jar包加错了。