Jetty9x+Logback Webapp日志输出

<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上。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值