本文共 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.gz2.下载 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]# wget3.下载 Download Log4J (Tomcat requires v1.2.x)
[root@sht-sgmhadoopnn-01 lib]# wget4.替换 $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.propertieslog4j.rootLogger = INFO, CATALINA# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.RollingFileAppenderlog4j.appender.CATALINA.File=${catalina.base}/logs/catalinalog4j.appender.CATALINA.layout=org.apache.log4j.PatternLayoutlog4j.appender.CATALINA.layout.ConversionPattern={"time":"%d{yyyy-MM-dd HH:mm:ss,SSS}","logtype":"%p","loginfo":"%c:%m"}%nlog4j.appender.CATALINA.MaxFileSize=2MBlog4j.appender.CATALINA.MaxBackupIndex=10log4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppender
log4j.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.DailyRollingFileAppender
log4j.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.DailyRollingFileAppender
log4j.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.ConsoleAppender
log4j.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 appenders
log4j.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-MANAGER8.启动[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.42Using CATALINA_HOME: /root/learnproject/app/apache-tomcat-7.0.42Using CATALINA_TMPDIR: /root/learnproject/app/apache-tomcat-7.0.42/tempUsing JRE_HOME: /usr/java/jdk1.7.0_67-clouderaUsing 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/