Jetty9x+Logback Webapp日志输出
2014-11-13 19:29
120 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">最近公司项目需要部署在Jetty环境下,项目中使用slf4j+logback作为日志框架。部署时发现项目中的logback.xml中的配置在Jetty环境中不起作用了。度年搜索无果后在Jetty官方网站上找到了解决办法:</span>
http://www.eclipse.org/jetty/documentation/current/example-logging-logback-centralized.html
配置步骤:
准备以下jar包,并复制到 jetty\lib\logging目录下:
准备一下jar包,复制到jetty\lib\webapp-logging目录下,没有该目录则创建它。
添加jetty-logging.properties文件至jetty\resources目录下:
添加jetty-logging.xml、jetty-webapp-logging.xml、jetty-mdc-handler.xml文件至jetty\etc\目录下:
jetty-logging.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- =============================================================== -->
<!-- Enable Centralized Logging in the Jetty Server -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Ref refid="DeploymentManager">
<Call name="addLifeCycleBinding">
<Arg>
<New class="org.eclipse.jetty.webapp.logging.CentralizedWebAppLoggingBinding">
</New>
</Arg>
</Call>
</Ref>
</Configure>
jetty-mdc-handler.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- =============================================================== -->
<!-- Configure the Jetty Slf4J/MDC Handler -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Get id="oldhandler" name="handler" />
<Set name="handler">
<New id="mdcHandler" class="org.eclipse.jetty.webapp.logging.ContextLogHandler">
<Set name="handler"><Ref refid="oldhandler" /></Set>
</New>
</Set>
</Configure>
添加logback.xml值Jetty\resources目录中:
修改Jetty\start.ini配置文件,添加:
至此已完成所有配置步骤,运行启动:
项目中若已设置好了logback日志文件的输出,此时就应该有输出内容了。注意logback依赖jar的版本需在1.0.x上。
http://www.eclipse.org/jetty/documentation/current/example-logging-logback-centralized.html
配置步骤:
准备以下jar包,并复制到 jetty\lib\logging目录下:
准备一下jar包,复制到jetty\lib\webapp-logging目录下,没有该目录则创建它。
添加jetty-logging.properties文件至jetty\resources目录下:
<pre># Configure Jetty for SLf4j Logging org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog
添加jetty-logging.xml、jetty-webapp-logging.xml、jetty-mdc-handler.xml文件至jetty\etc\目录下:
jetty-logging.xml:
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <!-- ======================================================= --> <!-- Configure java.util.logging to Slf4j bridge --> <!-- ======================================================= --> <Configure id="logging" class="org.eclipse.jetty.util.log.Log"> <Call class="org.slf4j.bridge.SLF4JBridgeHandler" name="removeHandlersForRootLogger"/> <Call class="org.slf4j.bridge.SLF4JBridgeHandler" name="install"/> </Configure>jetty-webapp-logging.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- =============================================================== -->
<!-- Enable Centralized Logging in the Jetty Server -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Ref refid="DeploymentManager">
<Call name="addLifeCycleBinding">
<Arg>
<New class="org.eclipse.jetty.webapp.logging.CentralizedWebAppLoggingBinding">
</New>
</Arg>
</Call>
</Ref>
</Configure>
jetty-mdc-handler.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- =============================================================== -->
<!-- Configure the Jetty Slf4J/MDC Handler -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Get id="oldhandler" name="handler" />
<Set name="handler">
<New id="mdcHandler" class="org.eclipse.jetty.webapp.logging.ContextLogHandler">
<Set name="handler"><Ref refid="oldhandler" /></Set>
</New>
</Set>
</Configure>
添加logback.xml值Jetty\resources目录中:
<?xml version="1.0" encoding="UTF-8"?> <!-- Example LOGBACK Configuration File http://logback.qos.ch/manual/configuration.html --> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${jetty.base}/logs/jetty.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>jetty_%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <root level="info"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> </configuration>
修改Jetty\start.ini配置文件,添加:
OPTIONS=resources,logging,webapp-logging
至此已完成所有配置步骤,运行启动:
java -jar start.jar
项目中若已设置好了logback日志文件的输出,此时就应该有输出内容了。注意logback依赖jar的版本需在1.0.x上。
相关文章推荐
- C++ 由Log日志输出理解回调函数
- web项目Log4j日志输出路径配置问题
- Log4j按级别输出日志到不同文件配置分析
- python输出日志到控制台和文件
- logback异步输出日志的配置方法和源码分析
- 如何正确地在SOE中输出日志信息
- spring集成slf4j日志输出
- MAVEN项目使用log4j配置输出日志
- 将ant执行的日志输出到文本中
- Java用webSocket实现tomcat的日志实时输出到web页面
- 自定义log日志输出到文件中保存
- 自定义未捕获异常时输出的日志为空或日志文件没有生成
- 灵活控制 Hibernate 的日志或 SQL 输出
- SpringBoot 如何关闭httpclient在控制台输出DEBUG日志
- Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志
- python+mitmproxy抓包过滤+redis消息订阅+websocket实时消息发送,日志实时输出到web界面
- Java 调整格式日志输出
- 一次关于loback日志无法输出到文件的问题解决
- DEBUG模式 输出日志
- Activity生命周期及如何存活 用log日志信息打印输出