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
这里方便测试
'.'yyyy-MM-dd-HH-mm'.log'设置为每分钟
log4j.rootLogger = INFO, CATALINA
# Define all the appenders
log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File = ${catalina.base}/logs/catalina
log4j.appender.CATALINA.Append = true
log4j.appender.CATALINA.Encoding = UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern = '.'yyyy-MM-dd-HH-mm'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File = ${catalina.base}/logs/localhost
log4j.appender.LOCALHOST.Append = true
log4j.appender.LOCALHOST.Encoding = UTF-8
log4j.appender.LOCALHOST.DatePattern = '.'yyyy-MM-dd-HH-mm'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File = ${catalina.base}/logs/manager
log4j.appender.MANAGER.Append = true
log4j.appender.MANAGER.Encoding = UTF-8
log4j.appender.MANAGER.DatePattern = '.'yyyy-MM-dd-HH-mm'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.HOST-MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File = ${catalina.base}/logs/host-manager
log4j.appender.HOST-MANAGER.Append = true
log4j.appender.HOST-MANAGER.Encoding = UTF-8
log4j.appender.HOST-MANAGER.DatePattern = '.'yyyy-MM-dd-HH-mm'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
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
# Configure which loggers log to which appenders
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
到官网下载 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/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.7.0_79
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
查看日志
首先要访问自己的工程,让其产生日志。
[root@localhost logs]# ls
catalina.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.log
catalina.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.log
catalina.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.log
catalina.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.log
catalina.out.2017-04-19-14-35.log catalina.out.2017-04-23-19-08.log localhost_access_log.2017-04-19.txt manager
catalina.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.log
catalina.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.open-open.com/doc/list/134