Tomcat运行仅一天磁盘空间突然就增加了很多,发现是日志文件太大了,修改tomcat的日志配置即可。
查看目录所占空间大小:
[root@XXX webapps]du -sh
清理方法:
[root@XXX logs]# cat > catalina.out
最好还是用log4日志,下面是配置Tomcat与log4的过程
1、下载与Tomcat相应版本的tomcat-juli.jar 和 tomcat-juli-adapters.jar,及log4j-1.2.17.jar,放在tomcat/lib目录中
再将tomcat-juli.jar 复制到tomcat/bin目录中,替换掉原来的
2、修改 Tomcat 的 conf/context.xml 文件,将<Context>改为<Context swallowOutput="true">
3、创建log4j.properties放在tomcat/lib
# 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
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
4、重启Tomcat观察logs下的日志文件
后记:注意日志保存的文件名要保留"."号,否则不会分类
生成的日志文件简洁,比tomcat自带的好多了
来源:oschina
链接:https://my.oschina.net/u/1168260/blog/298080