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
[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://anthrax2.iteye.com/blog/2295857
http://blog.csdn.net/hbsong75/article/details/39432811
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30089851/viewspace-2136429/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【REDO】Oracle 日誌挖掘,分析歸檔日誌線上日誌主要步驟Oracle
- Golang:將日誌以Json格式輸出到KafkaGolangJSONKafka
- log4j日誌輸出級別(轉)
- logback 日誌輸出格式
- 啟動tomcat時,日誌裡大量輸出建立資料來源dataSource的日誌Tomcat
- 收集、分析線上日誌資料實戰——ELK
- Docker安裝ELK並實現JSON格式日誌分析DockerJSON
- 日誌分析必備指令集【來自一段線上日誌的檢視的經歷】
- 排查log4j不輸出日誌到檔案的問題
- Spring Boot mybatis-config 和 log4j 輸出sql 日誌Spring BootMyBatisSQL
- python如何輸出日誌?Python
- logback下日誌輸出前處理操作——以日誌脫敏為例
- 日誌分析-apache日誌分析Apache
- 如何利用NLog輸出結構化日誌,並在Kibana優雅分析日誌?
- 如何將Spring Boot應用中日誌輸出格式改為JSON?-DEVSpring BootJSONdev
- python日誌重複輸出Python
- [20181225]如何清除註冊的線上日誌.txt
- 失敗登入行為輸出至alert日誌
- 日誌11月24日
- 指令碼日誌的標準輸出指令碼
- PowerJob 線上日誌飽受好評的祕訣:小但實用的分散式日誌系統分散式
- [日誌分析篇]-利用ELK分析jumpserver日誌-日誌拆分篇Server
- 操作日誌記錄(包括輸出至自定義日誌檔案)
- C語言log日誌管理-支援檔案與終端輸出C語言
- C#按照日期輸出程式日誌C#
- Java 如何正確地輸出日誌Java
- tomcat日誌集中採集、分析與展示的幾種方法Tomcat
- Tomcat訪問日誌淺析Tomcat
- 瘋狂刪除tomcat日誌Tomcat
- Nginx日誌格式設定Nginx
- 自定義Nginx日誌格式Nginx
- crash日誌分析
- FDOAGENT日誌分析
- 獲取Tomcat更詳細的日誌Tomcat
- log4j日誌級別以及配置
- Confluence 6 log4j 日誌級別
- logrus hook輸出日誌到本地磁碟Hook
- Robotframework-ride 日誌輸出中文亂碼FrameworkIDE
- Logstash 配置Java日誌格式的方法Java