返回顶部
首页 > 资讯 > 精选 >Tomcat的支持log4j及日志输出为json格式是怎样的
  • 243
分享到

Tomcat的支持log4j及日志输出为json格式是怎样的

2023-06-03 03:06:31 243人浏览 八月长安
摘要

今天就跟大家聊聊有关Tomcat的支持log4j及日志输出为JSON格式是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1.下载apache-tomcat-7.0.42及解压[

今天就跟大家聊聊有关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参数含义速查:

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 )

看完上述内容,你们对Tomcat的支持log4j及日志输出为json格式是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网精选频道,感谢大家的支持。

--结束END--

本文标题: Tomcat的支持log4j及日志输出为json格式是怎样的

本文链接: https://lsjlt.com/news/232552.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作