解決jetty7.x log日誌異常巨大問題

最近從jetty6升級到了jetty7發現硬盤空間動不動就滿了,我找... 我找.... 我找緣由.... 發現是jetty7 logs目錄搞得鬼,僅僅是個開發環境,1天1GB的日誌,瘋了! 這要是弄到生產環境還不完蛋啦!!我相信jetty的開發者不會這麼缺心眼的、少智慧的,因而乎googling.....java

E文太差先找中文的,發現了兩位網友寫的文章,說jetty本身實現了log系統,須要複寫這個類.....  當時暈倒,不用這麼麻煩吧。。。 因而覺然的開始 googling E文,終於找到了解決辦法web

jetty7有兩套log系統,默認使用本身的 org.eclipse.jetty.util.log 若是配置來log4j則使用log4j。shell

${jetty.home}/lib/ext 放入log4j jar包apache

在.jettyrc中加入 (jettyrc是什麼我就不解釋了)api

-Dlog4j.configuration=file:/home/jetty/app/jetty7/resources/log4j.properties

# This is not needed by Jetty - but it helps with many web apps.

log4j.rootLogger=WARN, stdout

log4j.appender.stdout=org.apache.log4j.RollingFileAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %t $
log4j.appender.stdout.File=${log4j.logdir}/stderrout.log
log4j.appender.stdout.MaxFileSize=204800KB
log4j.appender.stdout.MaxBackupIndex=10


以後修改etc/jetty-logging.xmlapp

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">


<!-- =============================================================== -->
<!-- Configure stderr and stdout to a Jetty rollover log file        -->
<!-- this configuration file should be used in combination with      -->
<!-- other configuration files.  e.g.                                -->
<!--    java -jar start.jar etc/jetty-logging.xml                    -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <New id="ServerLog" class="java.io.PrintStream">
      <Arg>
        <New class="org.eclipse.jetty.util.RolloverFileOutputStream">
          <Arg><Property name="jetty.logs" default="/var/log/jetty"/>/yyyy_mm_dd.stderrout.log</Arg>
          <Arg type="boolean">false</Arg>
          <Arg type="int">30</Arg>
          <Arg><Call class="java.util.TimeZone" name="getTimeZone"><Arg>GMT</Arg></Call></Arg>
          <Get id="ServerLogName" name="datedFilename"/>
        </New>
      </Arg>
    </New>

    <Call class="org.eclipse.jetty.util.log.Log" name="info"><Arg>Redirecting stderr/stdout to <Ref id="ServerLogName"/></Arg></Call>
    <Call class="java.lang.System" name="setErr"><Arg><Ref id="ServerLog"/></Arg></Call>
    <Call class="java.lang.System" name="setOut"><Arg><Ref id="ServerLog"/></Arg></Call>

</Configure>

30我猜是保留的文件個數,這個還有在考證

重啓jetty,世界清爽啦!~~~eclipse

英文以下:webapp

With Jetty 7.x you have 2 options.
Both require you to tell Jetty what kind of advanced logger you want to use.
* java.util.logging
* SLF4J

- java.util.logging -
Set a system property called "org.eclipse.jetty.util.log.class" to
"org.eclipse.jetty.util.log.JavaUtilLog" and from there you have all of the
standard java.util.logging configuration options to write to a file / roll
the log / etc ...

- SLF4J -
You'll want to setup SLF4J, have JettyLog use its SLF4J impl.
The mere existence of slf4j-api.jar in the classpath is enough to trigger
this behavior.
Download the slf4j-api.jar of your choice, and put it in
${jetty.home}/lib/ext
Be sure you checkout $ java -jar start.jar --version to see if it will load
into the Jetty Classpath (not your webapps)

Then you'll want to worry about how to take the SLF4J produced logging
events and route them to a logging impl you like.
Check out the docs at http://slf4j.org/ to understand how to setup slf4j.
For example: you can have SLF4J use log4j to write the logs to disk.

I personally like logback http://logback.qos.ch/ opposed to log4j, as it
allows me greater log routing control than log4j alone.
(Example: I can route all commons-logging & log4j & java.util.logging &
slf4j & stderr & stdout generated logging events to a file controlled by the
logback configuration under slf4j)
相關文章
相關標籤/搜索