24【在线日志分析】之Tomcat的支持log4j,日志输出为json格式

1.下载apache-tomcat-7.0.42及解压
[root@sht-sgmhadoopnn-01 app]# pwd
/root/learnproject/app
[root@sht-sgmhadoopnn-01 app]# wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.42/bin/apache-tomcat-7.0.42.tar.gz
[root@sht-sgmhadoopnn-01 app]# tar -xzvf apache-tomcat-7.0.42.tar.gz

2.下载 tomcat-juli.jar and tomcat-juli-adapters.jar
[root@sht-sgmhadoopnn-01 app]# cd apache-tomcat-7.0.42/lib/
[root@sht-sgmhadoopnn-01 lib]# wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.42/bin/extras/tomcat-juli-adapters.jar

3.下载 Download Log4J (Tomcat requires v1.2.x)
[root@sht-sgmhadoopnn-01 lib]# wget https://archive.apache.org/dist/logging/log4j/1.2.17/log4j-1.2.17.jar


4.替换 $CATALINA_HOME/bin/tomcat-juli.jar
[root@sht-sgmhadoopnn-01 lib]# cd ../bin
[root@sht-sgmhadoopnn-01 bin]# mv tomcat-juli.jar tomcat-juli.jar.bak
[root@sht-sgmhadoopnn-01 bin]# wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.42/bin/extras/tomcat-juli.jar


5.重命名 $CATALINA_BASE/conf/logging.properties to prevent java.util.logging generating zero length log files.
[root@sht-sgmhadoopnn-01 lib]# cd ../conf
[root@sht-sgmhadoopnn-01 conf]# mv logging.properties  logging.properties.bak
[root@sht-sgmhadoopnn-01 conf]#

6.修改conf/context.xml ,将<Context>为<Context swallowOutput="true">
[root@sht-sgmhadoopnn-01 conf]# vi context.xml
<Context swallowOutput="true">

 

7.创建log4j.properties,其catalina日志为滚动日志,json格式输出
[root@sht-sgmhadoopnn-01 conf]# cd ../lib
[root@sht-sgmhadoopnn-01 lib]# vi log4j.properties
log4j.rootLogger = INFO, CATALINA

# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.RollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina
log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern={"time":"%d{yyyy-MM-dd HH:mm:ss,SSS}","logtype":"%p","loginfo":"%c:%m"}%n
log4j.appender.CATALINA.MaxFileSize=2MB
log4j.appender.CATALINA.MaxBackupIndex=10

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'.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'.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'.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


8.启动
[root@sht-sgmhadoopnn-01 apache-tomcat-7.0.42]#
[root@sht-sgmhadoopnn-01 apache-tomcat-7.0.42]# bin/startup.sh
Using CATALINA_BASE:   /root/learnproject/app/apache-tomcat-7.0.42
Using CATALINA_HOME:   /root/learnproject/app/apache-tomcat-7.0.42
Using CATALINA_TMPDIR: /root/learnproject/app/apache-tomcat-7.0.42/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_67-cloudera
Using CLASSPATH:       /root/learnproject/app/apache-tomcat-7.0.42/bin/bootstrap.jar:/root/learnproject/app/apache-tomcat-7.0.42/bin/tomcat-juli.jar

9.检查日志格式
[root@sht-sgmhadoopnn-01 apache-tomcat-7.0.42]# tail -f ./logs/catalina
{"time":"2017-03-31 21:39:27,137","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deploying web application directory /root/learnproject/app/apache-tomcat-7.0.42/webapps/docs"}
{"time":"2017-03-31 21:39:27,231","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deploying web application directory /root/learnproject/app/apache-tomcat-7.0.42/webapps/ROOT"}
{"time":"2017-03-31 21:39:27,311","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deploying web application directory /root/learnproject/app/apache-tomcat-7.0.42/webapps/manager"}
{"time":"2017-03-31 21:39:27,452","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deploying web application directory /root/learnproject/app/apache-tomcat-7.0.42/webapps/examples"}
{"time":"2017-03-31 21:39:27,864","logtype":"INFO","loginfo":"org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/examples]:ContextListener: contextInitialized()"}
{"time":"2017-03-31 21:39:27,865","logtype":"INFO","loginfo":"org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/examples]:SessionListener: contextInitialized()"}
{"time":"2017-03-31 21:39:27,889","logtype":"INFO","loginfo":"org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/examples]:ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@6770983a')"}
{"time":"2017-03-31 21:39:27,897","logtype":"INFO","loginfo":"org.apache.coyote.http11.Http11Protocol:Starting ProtocolHandler ["http-bio-8080"]"}
{"time":"2017-03-31 21:39:27,922","logtype":"INFO","loginfo":"org.apache.coyote.ajp.AjpProtocol:Starting ProtocolHandler ["ajp-bio-8009"]"}
{"time":"2017-03-31 21:39:27,929","logtype":"INFO","loginfo":"org.apache.catalina.startup.Catalina:Server startup in 1828 ms"}

 

【备注】
log4j 的PatternLayout参数含义速查:
http://www.programgo.com/article/64053566088/

log4j.appender.CATALINA.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %p %c:%m%n

%d{yyyy-MM-dd HH:mm:ss,SSS} : 输出日期。允许使用%d{yyyy-MM-dd HH:mm:ss}格式化日期

%p: 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL

%c: 输出Logger所在的类别(即Logger的名字)。允许使用%c{数字}输出部分的名称(从右边往左边数)

%m: 输出代码中指定的消息
%n: 输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n”
%l: 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main(TestLog4.java: 10 )


下载及参照:
https://archive.apache.org/dist/logging/log4j/1.2.17/
http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.42/bin/extras/

https://tomcat.apache.org/tomcat-7.0-doc/logging.html
http://anthrax2.iteye.com/blog/2295857
http://blog.csdn.net/hbsong75/article/details/39432811

上一篇:安卓定制系统IUNI OS开启公测 仅支持三星S4


下一篇:Android应用坐标系统全面详解