Vladimir
Vladimir

Reputation: 2553

jetty request log rotation - size-based

I need to setup the jetty-requests logs to rotate the logs based on size rather than time. Is there a way to achieve this?

I've read the documentation on http://wiki.eclipse.org/Jetty/Howto/Configure_Request_Logs but to no avail.

Any ideas?

Thanks, Vladimir

Upvotes: 2

Views: 4767

Answers (3)

Nicholas DiPiazza
Nicholas DiPiazza

Reputation: 10595

I will show the process of doing this against jetty-distribution-9.4.28.v20200408

We will achieve this by enabling Slf4j on Jetty, because it's the only currently supported way to roll logs based on size.

Read: https://www.eclipse.org/jetty/documentation/current/configuring-logging-modules.html#example-logging-slf4j

First install slf4j-log4j2 or one of the varieties of it.

cd $JETTY_HOME
java -jar start.jar --add-to-start=slf4j-log4j2

This will generate a file resources/log4j2.xml. Edit this file and add a new RollingRandomAccessFile logger and give it a new. Here we will create one with name reqlog.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="Jetty" >

  <properties>
    <property name="logging.dir">${sys:jetty.logging.dir:-logs}</property>
  </properties>

  <Appenders>
    <Console name="console" target="SYSTEM_ERR">
      <PatternLayout>
        <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n</Pattern>
      </PatternLayout>
    </Console>

    <RollingRandomAccessFile name="requestLogAppender"
      fileName="${logging.dir}/jetty-request-log.log"
      filePattern="${logging.dir}/jetty-request-log-%d{MM-dd-yyyy}.log.gz"
      ignoreExceptions="false">
      <PatternLayout>
        <Pattern>%d [%t] %-5p %c %x - %m%n</Pattern>
      </PatternLayout>

      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="10 MB"/>
      </Policies>
    </RollingRandomAccessFile>

    <RollingRandomAccessFile name="file"
      fileName="${logging.dir}/jetty.log"
      filePattern="${logging.dir}/jetty-%d{MM-dd-yyyy}.log.gz"
      ignoreExceptions="false">
      <PatternLayout>
        <Pattern>%d [%t] %-5p %c %x - %m%n</Pattern>
      </PatternLayout>

      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="10 MB"/>
      </Policies>
    </RollingRandomAccessFile>

  </Appenders>

  <Loggers>
    <Root level="info">
      <AppenderRef ref="console"/>
    </Root>
    <Logger name="reqlog" additivity="false">
       <AppenderRef ref="requestLogAppender" />
    </Logger>
    <!--
    To have all logger async 
    <AsyncRoot level="info">
      <AppenderRef ref="file"/>
    </AsyncRoot>
    -->
  </Loggers>

</Configuration>

Change the <SizeBasedTriggeringPolicy size="10 MB"/> as desired.

Now edit etc/jetty-requestlog.xml and change it so that it points to the reqlog logger:

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

<!-- =============================================================== --><!-- Configure the Jetty Request Log                                 --><!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- Configure Request Log for Server                            -->
  <!-- (Use RequestLogHandler for a context specific RequestLog    -->
  <!-- =========================================================== -->
  <Set name="RequestLog">
    <New id="RequestLog" class="org.eclipse.jetty.server.Slf4jRequestLog">
      <Set name="loggerName">reqlog</Set>
    </New>
  </Set>  
</Configure>

Now you are all set.

Prove it works by starting Jetty then

tail -f logs/jetty-request-log.log 
2020-04-24 22:18:10 INFO  reqlog:62 - 127.0.0.1 - - [25/Apr/2020:03:18:10 +0000] "GET / HTTP/1.1" 404 751 
2020-04-24 22:18:10 INFO  reqlog:62 - 127.0.0.1 - - [25/Apr/2020:03:18:10 +0000] "GET /favicon.ico HTTP/1.1" 200 1150

Upvotes: 2

claus
claus

Reputation: 425

You can just write your log to a single file and use your systems logrotate if you are on a linux/unix host. Here is a howto:

http://www.techrepublic.com/article/manage-linux-log-files-with-logrotate/

logrotate also offers to rotate the log based on file size.

Upvotes: 1

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49525

Jetty 9+ has a Slf4jRequestLog implementation that you can use to have Jetty write NCSA formatted logging events too.

Then you can configure your logging implementation of choice to roll logs, email events, route to syslog, etc.

One note, use setLoggerName(String) to configure the output logger name that this implementation will use. That way you can have this implementation output to say com.company.access which will be the name you use in your logging implementation to route/filter/append the access logging events.

This was setup this way to allow for multiple Access logging handlers to be on different names that the logging implementation then handles accordingly.

Upvotes: 1

Related Questions