Tomcat7 log4j配置
一、 catalina.out 日志分割
1、安装cronolog
# wget http://down1.chinaunix.net/distfiles/cronolog-1.6.2.tar.gz
# tar zxvf cronolog-1.6.2.tar.gz
# cd cronolog-1.6.2
# ./configure
# make
# make install
查看cronolog安装后所在目录(验证安装是否成功)
# which cronolog
一般情况下显示为:/usr/local/sbin/cronolog
2、修改catalina.sh
注释 第372行 ,结果为 # touch "$CATALINA_OUT"
修改第393,394行,更改的结果为 org.apache.catalina.startup.Bootstrap "$@" start 2>&1 | /usr/local/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out >> /dev/null &
二、 配置log4j
1、 创建log4j.properties放在$CATALINA_BASE/lib
内如如下:
# appenders #
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.encoding = UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.file = ${catalina.base}/logs/catalina.
log4j.appender.CATALINA.encoding = UTF-8
log4j.appender.CATALINA.datePattern = '.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.CATALINA.append = true
log4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.file = ${catalina.base}/logs/localhost.
log4j.appender.LOCALHOST.encoding = UTF-8
log4j.appender.LOCALHOST.datePattern = '.'yyyy-MM-dd'.log'
#log4j.appender.LOCALHOST.maxFileSize = 100MB
#log4j.appender.LOCALHOST.maxBackupIndex = 10
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.LOCALHOST.append = true
log4j.appender.MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.file = ${catalina.base}/logs/manager.
log4j.appender.MANAGER.encoding = UTF-8
log4j.appender.MANAGER.datePattern = '.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.MANAGER.append = true
log4j.appender.HOST-MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.file = ${catalina.base}/logs/host-manager.
log4j.appender.HOST-MANAGER.encoding = UTF-8
log4j.appender.HOST-MANAGER.datePattern = '.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.HOST-MANAGER.append = true
## 将日志发送到邮件
log4j.appender.MAIL=com.gamaxpay.commonutil.log4j.SMTPAppender
log4j.appender.MAIL.Threshold=ERROR
log4j.appender.MAIL.BufferSize=1024
log4j.appender.MAIL.SMTPHost=smtp.exmail.qq.com
log4j.appender.MAIL.Subject=Error Log
log4j.appender.MAIL.SMTPUsername=system@mo9.com.cn
log4j.appender.MAIL.SMTPPassword=mokreditwin2012
log4j.appender.MAIL.From=system@mo9.com.cn
log4j.appender.MAIL.To=jian@mokredit.com
log4j.appender.MAIL.allowSendEmail=false
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] [%t] %l:%m%n
# root logger #
log4j.rootLogger = INFO, CONSOLE, CATALINA, MAIL
log4j.logger.org.springframework.web.servlet.handler.SimpleUrlHandlerMapping = WARN
log4j.logger.org.springframework.beans.factory.xml.XmlBeanDefinitionReader = WARN
log4j.logger.org.hibernate.cfg.annotations.EntityBinder = WARN
log4j.logger.org.hibernate.cfg.AnnotationBinder = WARN
log4j.logger.org.hibernate.cfg.annotations.CollectionBinder = WARN
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] = INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] = INFO, HOST-MANAGER
log4j.logger.de.javakaffee.web.msm = ERROR
log4j.logger.org.apache.catalina.core = INFO, LOCALHOST
log4j.logger.org.apache.catalina.session = INFO, LOCALHOST
2、 下载log4j-1.2.17.jar或者相应版本
3、 下载extra包
从http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.40/bin/extras/下载
tomcat-juli.jar 和 tomcat-juli-adapters.jar
4、配置
将log4j-1.2.17.jar和tomcat-juli-adapters.jar放入$CATALINA_HOME/lib
将tomcat-juli.jar放入$CATALINA_HOME/bin覆盖已有文件
创建commons-logging.properties,放入$CATALINA_HOME/lib,文件内容如下:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
删除文件$CATALINA_BASE/conf/logging.properties
5、启动Tomcat
转载于:https://blog.51cto.com/sxlfxx/1707697