Objective
Elkstack Crawl Analysis Application log is a prerequisite for development to change the output format of the log to JSON format, otherwise the output of the log is difficult to analyze.
Main flow: Tomcat receives data from log4j output log-Logstash and sends it to Redis--logstash get data from Redis and write to ES
How does tomcat output the application log? tomcat7.0 Configuring LOG4J Logs
Detail Document Http://tomcat.apache.org/tomcat-7.0-doc/logging.html
First, create the Log4j.properties file
under $CATALINA _base/lib folder, create a new log4j.properties file with the following contents
Log4j.rootlogger = INFO, CATALINA
# define all the appenderslog4j.appender.catalina = org.apache.log4j.dailyrollingfileappenderlog4j.appender.catalina.file = ${catalina.base}/logs/ catalinalog4j.appender.catalina.append = truelog4j.appender.catalina.encoding = utf-8# Roll-over the log once per daylog4j.appender.CATALINA.DatePattern = ' .‘ Yyyy-mm-dd '. Log ' log4j.appender.catalina.layout = org.apache.log4j.patternlayoutlog4j.appender.catalina.layout.conversionpattern = %d [%t] % -5p %c- %m%nlog4j.appender.localhost = org.apache.log4j.dailyrollingfileappenderlog4j.appender.localhost.file = ${catalina.base}/logs/ localhostlog4j.appender.localhost.append = truelog4j.appender.localhost.encoding = utf-8log4j.appender.localhost.datepattern = '. ' Yyyy-mm-dd '. Log ' Log4j.appender.localhost.layout = org.apache.log4j.patternlayoutlog4j.appender.localhost.layout.conversionpattern = %d [%t] %-5p %c- %m% nlog4j.appender.manager = org.apache.log4j.dailyrollingfileappenderlog4j.appender.manager.file = ${catalina.base}/logs/managerlog4j.appender.manager.append = truelog4j.appender.manager.encoding = utf-8log4j.appender.manager.datepattern = '. ' Yyyy-mm-dd '. Log ' log4j.appender.manager.layout = org.apache.log4j.patternlayoutlog4j.appender.manager.layout.conversionpattern = %d [%t] %- 5p %c- %m%nlog4j.appender.host-manager = org.apache.log4j.dailyrollingfileappenderlog4j.appender.host-manager.file = ${catalina.base}/logs/ host-managerlog4j.appender.host-manager.append = truelog4j.appender.host-manager.encoding = UTF-8log4j.appender.HOST-MANAGER.DatePattern = '. ' Yyyy-mm-dd '. Log ' log4j.appender.host-manager.layout = Org.apache.log4j.PatternLayoutlog4j.appender.HOST-manager.layout.conversionpattern = %d [%t] %-5p %c- %m%nlog4j.appender.console = org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.Encoding = utf-8log4j.appender.console.layout = org.apache.log4j.patternlayoutlog4j.appender.console.layout.conversionpattern = %d [%t] %- 5p %c- %m%n# configure which loggers log to which Appenderslog4j.logger.org.apache.catalina.core.ContainerBase. [Catalina]. [Localhost] = info, localhostlog4j.logger.org.apache.catalina.core.containerbase. [Catalina]. [localhost]. [/manager] =info, managerlog4j.logger.org.apache.catalina.core.containerbase. [Catalina]. [localhost]. [/host-manager] =info, host-managerIi. Download jar Package (i) Log4j.jar (1.2.x version)
http://mvnrepository.com/artifact/log4j/log4j/1.2.17
(ii) Tomcat-juli.jar, Tomcat-juli-adapters.jar
http://tomcat.apache.org/download-70.cgi
Note: Be sure to download the "extras" package, which differs from the package that comes with it, the official note:
This tomcat-juli.jar differs from the default one. It contains the full Apache Commons Logging implementation and thus are able to discover the presence of log4j and Configur E itself.
Third, replace the jar package
Put Log4j.jar, Tomcat-juli-adapters.jar into the $CATALINA _base/lib Directory
Replace the downloaded Tomcat-juli.jar $CATALINA the Tomcat-juli.jar in the _home/bin directory
Note: Two packages are placed in different locations
Iv. deletion of Logging.properties
Delete the Tomcat default log file $catalina_base/conf/logging.properties file
V. Launch TOMCAT
log4j output log can be seen in the $catalina_base/logs directory after startup
Developing custom Application log Output
General development of Java programs, running Tomcat in the application, the log format definition is generally placed under the project: Web-inf/class directory, is generally called log4j.properties
The author uses the Logstash version of 2.3, is the latest version, if the small partner to learn from the exception, you can look at the version, compare official documents.
Allow development to make the following changes in Log4j.properties:
#log4j. rootlogger = [ level ] , appendername, appendername, , ..... log4j.rootlogger = info,console,infor,errorr,logstash#branch loggerlog4j.logger.org.springframework=warn#log4j.logpathlog4j.logpath=/opt/tomcat_xxxxxweb/log# consolelog4j.appender.console = org.apache.log4j.consoleappenderlog4j.appender.console.layout = org.apache.log4j.patternlayoutlog4j.appender.console.layout.conversionpattern = [%p] %d{ yyyy-mm-dd hh:mm:ss} [%l] %m%n#inforlog4j.appender.infor = org.apache.log4j.dailyrollingfileappenderlog4j.appender.infor.file =${log4j.logpath}/ xxxxxx.loglog4j.appender.infor.layout = org.apache.log4j.patternlayoutlog4j.appender.infor.layout.conversionpattern=[%p] %d{yyyy-mm-dd hh:mm : ss} [%l] %m%nlog4j.appender.infor.datepattern= '. ' YyyyMMdd '. Log ' log4j.appender.infor.threshold = info#errorrlog4j.appender.errorr = Org.apache. log4j. dailyrollingfileappenderlog4j.appender.errorr.file =${log4j.logpath}/ xxxxxerror.loglog4j.appender.errorr.layout = ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.ERRORR.LAYOUT.CONVERSIONPATTERN=[%P] %D{YYYY-MM-DD HH: Mm:ss} [%l] %m%nlog4j.appender.errorr.datepattern= '. ' YyyyMMdd '. Log ' log4j.appender.errorr.threshold = error#logstashlog4j.appender.logstash= org.apache.log4j.net.socketappenderlog4j.appender.logstash.port=12201log4j.appender.logstash.remotehost= sz-a-xxxxxlogstash01-logstash-xen.xxxxx.comlog4j.appender.logstash.reconnectiondelay= 60000log4j.appender.logstash.locationinfo=truelog4j.appender.logstash.threshold = infoLogstash How do I accept log4j logs and write to Redis?
The installation of Logstash is no longer described here, and it is recommended to use Yum installation management, which is more convenient.
Logstash configuration files can be placed in/ETC/LOGSTASH/CONF.D
Logstash configuration file Display:
input {tcp {port => "11011" type => "Syslog"}log4j {type => "Log4j-json-web" port=>12202}log4j {type => "Log4j-json-tomcat" port=>12201}} filter {if [type] == "syslog" {grok {match => { "message" => "%{syslogtimestamp:syslog_timestamp} %{sysloghost:syslog_hostname} %{data:syslog_ Program} (?: \ [%{posint:syslog_pid}\])?: %{greedydata:syslog_message} " }}}if [type] == " Log4j-json-web " {json {source => " message "}}}output {if [type] == " Syslog " {redis {host => " sz-a-xxxxxredis01-redis-xen.xxxxx.com "data_type => "List" key => "Logstash:syslog-log"}}if [type] == "Log4j-json-web" { redis {host => "sz-a-xxxxxredis01-redis-xen.xxxxx.com" data_type => "list" key => "Logstash:xxxxxweb-web-log "}}if [type] == " Log4j-json-tomcat " {redis {host => " sz-a-xxxxxredis01-redis-xen.xxxxx.com "data_type => " list "key => " Logstash: Xxxxxweb-tomcat-log "}}}
A logstash process can have multiple input inputs and then set the Type property for each input, which you need to be aware of at this time!! Do not appear in the Log Type field, or it is the big PIT!!!!
Ouput can determine input source and output to a different place through the if statement
Redis uses list mode to store data so that it acts as a cache and does not explode Redis
elkstack-based on Java Engineering Tomcat Application log processing-01