tomcat使用log4j管理日誌

一、JDK+tomcat環境  參考:http://www.cnblogs.com/zzzhfo/p/6444029.htmlhtml

二、下載相關軟件web

log4j下載地址express

http://www.apache.org/dist/logging/log4j/apache

http://www.apache.org/dist/logging/log4j/1.2.17/bootstrap

http://www.apache.org/dist/logging/log4j/1.2.17/log4j-1.2.17.tar.gz  tomcat

三、中止服務session

shutdown.sh
  •  將log4j.properties和log4j-1.2.17.jar放到tomcat/lib目錄下

配置log4j.propertiesapp

這裏方便測試less

'.'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包。webapp

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

相關文章
相關標籤/搜索