1、JDK+tomcat环境 参考:http://www.cnblogs.com/zzzhfo/p/6444029.html
2、下载相关软件
log4j下载地址
http://www.apache.org/dist/logging/log4j/
http://www.apache.org/dist/logging/log4j/1.2.17/
http://www.apache.org/dist/logging/log4j/1.2.17/log4j-1.2.17.tar.gz
3、停止服务
shutdown.sh
将log4j.properties和log4j-1.2.17.jar放到tomcat/lib目录下
配置log4j.properties
tomcat每次启动时,自动在logs目录下生产以下日志文件,按照日期自动备份
localhost.2016-07-05.txt //经常用到的文件之一 ,程序异常没有被捕获的时候抛出的地方
catalina.2016-07-05.txt //经常用到的文件之一,程序的输出,tomcat的日志输出等等
manager.2016-07-05.txt //估计是manager项目专有的
host-manager.2016-07-05.txt//估计是manager项目专有的
localhost_access_log.2016-10-01.txt //tomcat访问日志记录,需要配置
这里方便测试
'.'yyyy-MM-dd-HH-mm'.log'设置为每分钟log4j.rootLogger = INFO, CATALINA# Define all the appenderslog4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.CATALINA.File = ${catalina.base}/logs/catalinalog4j.appender.CATALINA.Append = truelog4j.appender.CATALINA.Encoding = UTF-8# Roll-over the log once per daylog4j.appender.CATALINA.DatePattern = '.'yyyy-MM-dd-HH-mm'.log'log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayoutlog4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%nlog4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.LOCALHOST.File = ${catalina.base}/logs/localhostlog4j.appender.LOCALHOST.Append = truelog4j.appender.LOCALHOST.Encoding = UTF-8log4j.appender.LOCALHOST.DatePattern = '.'yyyy-MM-dd-HH-mm'.log'log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayoutlog4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%nlog4j.appender.MANAGER = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.MANAGER.File = ${catalina.base}/logs/managerlog4j.appender.MANAGER.Append = truelog4j.appender.MANAGER.Encoding = UTF-8log4j.appender.MANAGER.DatePattern = '.'yyyy-MM-dd-HH-mm'.log'log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayoutlog4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%nlog4j.appender.HOST-MANAGER = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.HOST-MANAGER.File = ${catalina.base}/logs/host-managerlog4j.appender.HOST-MANAGER.Append = truelog4j.appender.HOST-MANAGER.Encoding = UTF-8log4j.appender.HOST-MANAGER.DatePattern = '.'yyyy-MM-dd-HH-mm'.log'log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayoutlog4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%nlog4j.appender.CONSOLE = org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.Encoding = UTF-8log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n# Configure which loggers log to which appenderslog4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, LOCALHOSTlog4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] =\ INFO, MANAGERlog4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] =\ INFO, HOST-MANAGER官网下载 tomcat-juli.jar和tomcat-juli-adapters.jar包。
https://tomcat.apache.org/
https://tomcat.apache.org/download-70.cgi
http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.77/bin/extras/tomcat-juli-adapters.jar
http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.77/bin/extras/tomcat-juli.jar
将tomcat-juli-adapters.jar包放到tomcat/lib目录下
将tomcat-juli.jar包放到tomcat/bin目录下(替换原有的)
删除或重命名tomcat/conf目录下的logging.properties文件
修改tomcat/conf/目录下context.xml
将<Context>改为<Context swallowOutput="true">
[root@localhost conf]# cat context.xml <?xml version='1.0' encoding='utf-8'?><!-- 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.--><!-- The contents of this file will be loaded for each web application --><!--<Context swallowOutput="true"> --><Context swallowOutput="true"> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- <Context swallowOutput="true"> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --></Context>
启动服务
[root@localhost tomcat]# bin/startup.sh Using CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usr/local/jdk1.7.0_79Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jarTomcat started.
查看日志
首先要访问自己的工程,让其产生日志。
[root@localhost logs]# lscatalina.2017-04-19-13-48.log catalina.out.2017-04-23-18-43.log catalina.out.2017-04-23-19-14.log localhost localhost.log.2017-04-19-14-35.logcatalina.out catalina.out.2017-04-23-18-58.log catalina.out.2017-04-23-19-15.log localhost.2017-04-19-13-48.log localhost.log.2017-04-19-14-36.logcatalina.out.2017-04-19-13-51.log catalina.out.2017-04-23-19-04.log catalina.out.2017-04-23-19-20.log localhost.2017-04-19-13-50.log localhost.log.2017-04-19-15.logcatalina.out.2017-04-19-13-55.log catalina.out.2017-04-23-19-07.log localhost.2017-04-19-13-51.log localhost.log.2017-04-23-18.logcatalina.out.2017-04-19-14-35.log catalina.out.2017-04-23-19-08.log localhost_access_log.2017-04-19.txt managercatalina.out.2017-04-19-14-36.log catalina.out.2017-04-23-19-09.log host-manager localhost_access_log.2017-04-23.txt manager.logcatalina.out.2017-04-19-15.log catalina.out.2017-04-23-19-10.log host-manager.log localhost.log catalina.out.2017-04-23-18-42.log catalina.out.2017-04-23-19-13.log localhost.log.2017-04-19-13-55.log
本文出自http://www.cnblogs.com/zzzhfo/p/6729333.html