您的位置:首页 > 移动开发 > WebAPP

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目录下:

<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上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: