[JAVA] How to output Jetty log to any directory

Overview

The lightweight web server Jetty outputs the following two log files.

By default, these log files are output to logs directly under the directory specified by the jetty.base property.

Jetty Reference Default Logging with Jetty's StdErrLog and Configuring Jetty Request Logs I referred to .eclipse.org/jetty/documentation/current/configuring-jetty-request-logs.html), but it was difficult to understand how to change the output destination to an arbitrary directory, so I summarized it in this article.

Contents

1. How to change the output destination of standard output / standard error output log

Log file settings are made from ʻetc / jetty-logging.xml`.

jetty-logging.xml


<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.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="logging" class="org.eclipse.jetty.util.log.Log">

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

    <Get name="rootLogger">
      <Call name="info"><Arg>Redirecting stderr/stdout to <Ref refid="ServerLogName"/></Arg></Call>
    </Get>
    <Call class="java.lang.System" name="setErr"><Arg><Ref refid="ServerLog"/></Arg></Call>
    <Call class="java.lang.System" name="setOut"><Arg><Ref refid="ServerLog"/></Arg></Call>

</Configure>

changes

<Arg><Property name="jetty.logs" default="./logs"/>/yyyy_mm_dd.stderrout.log</Arg>

It is now output as yyyy_mm_dd.stderrout.log directly under the directory specified by the jetty.logs property ($ pwd / logs if there is no property).

As a workaround, specify the value LOG_DIR = / test / logs in the system property of the Jetty start command and refer to LOG_DIR in jetty-logging.xml so that the log can be output to any directory. Will be.

$ java -DLOG_DIR=/test/logs -jar start.jar
<Arg><Property name="LOG_DIR"/>/yyyy_mm_dd.stderrout.log</Arg>

In this example, the standard output / standard error output log is output to /test/logs/yyyy_mm_dd.stderrout.log.

2. How to change the output destination of the access log

Log file settings are made from ʻetc / jetty-requestlog.xml`.

jetty-requestlog.xml


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

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

  <!-- =========================================================== -->
  <!-- Configure Request Log -->
  <!-- =========================================================== -->
  <Ref refid="Handlers">
    <Call name="addHandler">
      <Arg>
        <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler">
          <Set name="requestLog">
            <New id="RequestLogImpl" class="org.eclipse.jetty.server.AsyncNCSARequestLog">
              <Set name="filename"><Property name="jetty.base" default="." /><Property name="requestlog.filename" default="/logs/yyyy_mm_dd.request.log"/></Set>
              <Set name="filenameDateFormat"><Property name="requestlog.filenameDateFormat" default="yyyy_MM_dd"/></Set>
              <Set name="retainDays"><Property name="requestlog.retain" default="90"/></Set>
              <Set name="append"><Property name="requestlog.append" default="false"/></Set>
              <Set name="extended"><Property name="requestlog.extended" default="false"/></Set>
              <Set name="logCookies"><Property name="requestlog.cookies" default="false"/></Set>
              <Set name="LogTimeZone"><Property name="requestlog.timezone" default="GMT"/></Set>
            </New>
          </Set>
        </New>
      </Arg>
    </Call>
  </Ref>

</Configure>

changes

<Set name="filename"><Property name="jetty.base" default="." /><Property name="requestlog.filename" default="/logs/yyyy_mm_dd.request.log"/></Set>

It is now output as /logs/yyyy_mm_dd.request.log directly under the directory specified by the jetty.base property ($ pwd if there is no property).

Unlike the standard output / standard error output log, the output destination directory and output file name are specified separately for the access log. Note that the output file name includes the logs directory specification by default.

As a workaround, specify the value LOG_DIR = / test / logs in the system property of the Jetty start command and refer to LOG_DIR in jetty-requestlog.xml as in the standard output / standard error output log. This will allow you to output the log to any directory.

$ java -DLOG_DIR=/test/logs -jar start.jar
<Set name="filename"><Property name="LOG_DIR"/><Property name="requestlog.filename" default="/yyyy_mm_dd.request.log"/></Set>

The logs directory is removed from the output file name of the requestlog.filename property so that only the file name is used.

In this example, the access log is output to /test/logs/yyyy_mm_dd.request.log.

Recommended Posts

How to output Jetty log to any directory
How to enclose any character with "~"
[Ruby] How to use any? Method
Log output to file in Java
Output HTTP header of google-http-client to log
[Java] How to output and write files!
How to output Java string to console screen
[Java] How to get the current directory
How to output the sum of any three numbers, excluding the same value
[Apache Camel] Easy output of throughput to log
How to output Excel and PDF using Excella
(Memo) How to solve dummy output in Ubuntu 20.04
How to color code console output in Eclipse
[Java] How to get and output standard input
How to output CSV created by Rails to S3
How to deploy
Output of how to use the slice method
How to use Alibaba Cloud LOG Java Producer
How to log in automatically when Ubuntu restarts
[Ruby] How to use standard output in conditional branching
[Android Studio] How to change TextView to any font [Java]
How to input / output IBM mainframe files in Java?
Output log to external file with slf4j + logback with Maven
How to output standard from an array with forEach
How to judge the click of any area of the image
I want to simplify the log output on Android
About Java log output
How to develop OpenSPIFe
How to call AmazonSQSAsync
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to adapt Bootstrap
How to use Twitter4J
How to use active_hash! !!
How to install Docker
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to write dockerfile
How to uninstall Rails
How to install docker-machine
[How to use label]
How to make shaded-jar
How to write docker-compose
How to use identity
How to use hashes
How to write Mockito
How to create docker-compose
How to use JUnit 5
How to install MySQL
How to write migrationfile
How to build android-midi-lib
How to use Dozer.mapper
How to use Gradle