Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Tomcat supports log4j and what is the log output to json format

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about Tomcat's support for log4j and how the log output is in json format. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Download apache-tomcat-7.0.42 and decompress

[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

two。 Download 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 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. Replace $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. Rename $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. Modify the conf/context.xml, which will be

[root@sht-sgmhadoopnn-01 conf] # vi context.xml

7. Create a log4j.properties whose catalina log is scrolling and output in json format

[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": "% CVA% 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. Start

[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. Check the log format

[root@sht-sgmhadoopnn-01 apache-tomcat-7.0.42] # tail-f. / logs/catalina

{"time": "2017-03-31 21 INFO", "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 INFO", "logtype": "org.apache.catalina.startup.HostConfig:Deploying web application directory / root/learnproject/app/apache-tomcat-7.0.42/webapps/ROOT"}

{"time": "2017-03-31 21 INFO", "logtype": "org.apache.catalina.startup.HostConfig:Deploying web application directory / root/learnproject/app/apache-tomcat-7.0.42/webapps/manager"}

{"time": "2017-03-31 21 INFO", "logtype": "org.apache.catalina.startup.HostConfig:Deploying web application directory / root/learnproject/app/apache-tomcat-7.0.42/webapps/examples"}

{"time": "2017-03-31 21 INFO 39VR 27864", "logtype": "INFO", "loginfo": "org.apache.catalina.core.ContainerBase.[ Catalina] .localhost] .[ / examples]: ContextListener: contextInitialized ()"}

{"time": "2017-03-31 21 INFO 39V 27865", "logtype": "INFO", "loginfo": "org.apache.catalina.core.ContainerBase.[ Catalina] .localhost] .[ / examples]: SessionListener: contextInitialized ()"}

{"time": "2017-03-31 21 INFO 39V 27889", "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 INFO 39 INFO", "logtype": "INFO", "loginfo": "org.apache.coyote.http11.Http11Protocol:Starting ProtocolHandler [" http-bio-8080 "]"}

{"time": "2017-03-31 21 INFO 39 INFO", "logtype": "INFO", "loginfo": "org.apache.coyote.ajp.AjpProtocol:Starting ProtocolHandler [" ajp-bio-8009 "]"}

{"time": "2017-03-31 21 loginfo 39 INFO", "logtype": "INFO", "org.apache.catalina.startup.Catalina:Server startup in 1828 ms"}

[remarks]

Quick check the meaning of log4j's PatternLayout parameters:

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}: output date. Allow formatting date using% d {yyyy-MM-dd HH:mm:ss}

% p: output priority, i.e. DEBUG,INFO,WARN,ERROR,FATAL

% c: output the category in which Logger is located (that is, the name of Logger). Allow the name of the output section (from right to left) using% c {number}

% m: output the message specified in the code

% n: output a carriage return newline character with "\ r\ n" for Windows platform and "\ n" for Unix platform

% l: where the output log event occurs, including the class name, the thread that occurred, and the number of lines in the code. For example: Testlog4.main (TestLog4.java: 10)

After reading the above, do you have any further understanding of Tomcat's support for log4j and log output in json format? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report