博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
24【在线日志分析】之Tomcat的支持log4j,日志输出为json格式
阅读量:5817 次
发布时间:2019-06-18

本文共 6222 字,大约阅读时间需要 20 分钟。

1.下载apache-tomcat-7.0.42及解压

[root@sht-sgmhadoopnn-01 app]# pwd
/root/learnproject/app
[root@sht-sgmhadoopnn-01 app]# wget
[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

3.下载 Download Log4J (Tomcat requires v1.2.x)

[root@sht-sgmhadoopnn-01 lib]# wget

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

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', )"}
{"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参数含义速查:

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 )

下载及参照:

转载地址:http://pvqbx.baihongyu.com/

你可能感兴趣的文章
Knockout.Js官网学习(enable绑定、disable绑定)
查看>>
hive基本操作与应用
查看>>
excel快捷键设置
查看>>
poj3692
查看>>
python之信号量【Semaphore】
查看>>
html5纲要,细谈HTML 5新增的元素
查看>>
Android应用集成支付宝接口的简化
查看>>
[分享]Ubuntu12.04安装基础教程(图文)
查看>>
#HTTP协议学习# (二)基本认证
查看>>
Android开发之线性布局详解(布局权重)
查看>>
WCF
查看>>
django 目录结构修改
查看>>
win8 关闭防火墙
查看>>
Android实例-录音与回放(播放MP3)(XE8+小米2)
查看>>
CSS——(2)与标准流盒模型
查看>>
MYSQL 基本SQL语句
查看>>
C#中的Marshal
查看>>
linux命令:ls
查看>>
Using RequireJS in AngularJS Applications
查看>>
hdu 2444(二分图最大匹配)
查看>>