【问题标题】:Unable to make jetty use logback for logging无法让码头使用 logback 进行日志记录
【发布时间】:2021-05-02 19:53:08
【问题描述】:

我跟随 this page 在我的 Amazon EC2 实例(Ubuntu 14.04.2 LTS)上安装了码头。码头正在运行。但是,我不能让它使用 logback 进行日志记录。以下是相关信息。我在这里做错了什么?

服务检查

$ service jetty check
Checking arguments to Jetty:
START_INI      =  /opt/jetty/start.ini
START_D        =  /opt/jetty/start.d
JETTY_HOME     =  /opt/jetty
JETTY_BASE     =  /opt/jetty
JETTY_CONF     =  /opt/jetty/etc/jetty.conf
JETTY_PID      =  /tmp/jetty.pid
JETTY_START    =  /opt/jetty/start.jar
JETTY_LOGS     =  /opt/jetty/logs
JETTY_STATE    =  /opt/jetty/jetty.state
CLASSPATH      =
JAVA           =  /usr/bin/java
JAVA_OPTIONS   =  -Djetty.logs=/opt/jetty/logs -Djetty.home=/opt/jetty -Djetty.base=/opt/jetty -Djava.io.tmpdir=/tmp
JETTY_ARGS     =  jetty.state=/opt/jetty/jetty.state jetty-started.xml
RUN_CMD        =  /usr/bin/java -Djetty.logs=/opt/jetty/logs -Djetty.home=/opt/jetty -Djetty.base=/opt/jetty -Djava.io.tmpdir=/tmp -jar /opt/jetty/start.jar jetty.state=/opt/jetty/jetty.state jetty-started.xml

/etc/default/jetty

$ cat /etc/default/jetty
# Defaults for jetty see /etc/init.d/jetty for more

# change to 0 to allow Jetty to start
NO_START=0

# change to 'no' or uncomment to use the default setting in /etc/default/rcS
VERBOSE=yes

# Run Jetty as this user ID (default: jetty)
# Set this to an empty string to prevent Jetty from starting automatically
JETTY_USER=jetty

# Listen to connections from this network host
# Use 0.0.0.0 as host to accept all connections.
# Uncomment to restrict access to localhost
JETTY_HOST=0.0.0.0

# The network port used by Jetty
JETTY_PORT=8080

# Timeout in seconds for the shutdown of all webapps
JETTY_SHUTDOWN=30

# Additional arguments to pass to Jetty
#JETTY_ARGS=

# Extra options to pass to the JVM
#JAVA_OPTIONS="-Xmx256m -Djava.awt.headless=true -Djava.library.path=/usr/lib"

# Home of Java installation.
JAVA=/usr/bin/java
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
JETTY_HOME=/opt/jetty

# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
# defined in /etc/default/jetty). Should contain a list of space separated directories.
#JDK_DIRS="/usr/lib/jvm/default-java /usr/lib/jvm/java-6-sun"

# Java compiler to use for translating JavaServer Pages (JSPs). You can use all
# compilers that are accepted by Ant's build.compiler property.
#JSP_COMPILER=jikes

# Jetty uses a directory to store temporary files like unpacked webapps
#JETTY_TMP=/var/cache/jetty

# Jetty uses a config file to setup its boot classpath
#JETTY_START_CONFIG=/etc/jetty/start.config

# Default for number of days to keep old log files in /var/log/jetty/
#LOGFILE_DAYS=14

/opt/jetty/etc/jetty.conf

$ cat /opt/jetty/etc/jetty.conf
# ========================================================
# jetty.conf Configuration for jetty.sh script
# --------------------------------------------------------
# This file is used by the jetty.sh script to provide
# extra configuration arguments for the start.jar command
# created by that script.
#
# Each line in this file becomes an arguement to start.jar
# in addition to those found in the start.ini file
# =======================================================
#jetty-logging.xml
jetty-started.xml

/opt/jetty/modules/logging.mod

$ cat /opt/jetty/modules/logging.mod
#
# Jetty std err/out logging
#

[name]
logging

[files]
logs/

[lib]
lib/logging/*.jar
resources/

[ini-template]
## Logging Configuration
# Configure jetty logging for default internal behavior STDERR output
# -Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog

# Configure jetty logging for slf4j
-Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog

# Configure jetty logging for java.util.logging
# -Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.JavaUtilLog

# STDERR / STDOUT Logging
# Number of days to retain logs
# jetty.log.retain=90
# Directory for logging output
# Either a path relative to ${jetty.base} or an absolute path
# jetty.logs=logs

/opt/jetty/resources/logback.xml

$ cat /opt/jetty/resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Insert the current time formatted as "yyyyMMdd'T'HHmmss" under
       the key "bySecond" into the logger context. This value will be
       available to all subsequent configuration elements. -->
    <timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>

    <!-- console appender -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{100} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/opt/jetty/logs/server.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>/opt/jetty/logs/server.%d{yyyy-MM-dd}.log</fileNamePattern>

            <!-- keep 30 days' worth of history -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{100} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.eclipse.jetty" level="INFO"/>
    <logger name="org.springframework" level="INFO"/>
    <logger name="org.apache" level="INFO"/>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

/opt/jetty/start.ini

$ cat /opt/jetty/start.ini
#===========================================================
# Jetty Startup
#
# Starting Jetty from this {jetty.home} is not recommended.
#
# A proper {jetty.base} directory should be configured, instead
# of making changes to this {jetty.home} directory.
#
# See documentation about {jetty.base} at
# http://www.eclipse.org/jetty/documentation/current/startup.html
#
# A demo-base directory has been provided as an example of
# this sort of setup.
#
# $ cd demo-base
# $ java -jar ../start.jar
#
#===========================================================

# To disable the warning message, comment the following line
--module=home-base-warning


# ---------------------------------------
# Module: server
--module=server
##
## Server Threading Configuration
##
# minimum number of threads
threads.min=10
# maximum number of threads
threads.max=200
# thread idle timeout in milliseconds
threads.timeout=60000
# buffer size for output
jetty.output.buffer.size=32768
# request header buffer size
jetty.request.header.size=8192
# response header buffer size
jetty.response.header.size=8192
# should jetty send the server version header?
jetty.send.server.version=true
# should jetty send the date header?
jetty.send.date.header=false
# What host to listen on (leave commented to listen on all interfaces)
#jetty.host=0.0.0.0
# Dump the state of the Jetty server, components, and webapps after startup
jetty.dump.start=false
# Dump the state of the Jetty server, before stop
jetty.dump.stop=false
# Enable delayed dispatch optimisation
jetty.delayDispatchUntilContent=false

# ---------------------------------------
# Module: deploy
--module=deploy
## DeployManager configuration
# Monitored Directory name (relative to jetty.base)
# jetty.deploy.monitoredDirName=webapps


# ---------------------------------------
# Module: websocket
--module=websocket

# ---------------------------------------
# Module: ext
--module=ext

# ---------------------------------------
# Module: resources
--module=resources

# ---------------------------------------
# Module: jsp
--module=jsp
# JSP Configuration

# Select JSP implementation, choices are
#   glassfish : The reference implementation
#               default in jetty <= 9.1
#   apache    : The apache version
#               default jetty >= 9.2
jsp-impl=apache

# To use a non-jdk compiler for JSP compilation when using glassfish uncomment next line
# -Dorg.apache.jasper.compiler.disablejsr199=true

# ---------------------------------------
# Module: jstl
--module=jstl
# JSTL Configuration
# The glassfish jsp-impl includes JSTL by default and this module
# is not required to activate it.
# The apache jsp-impl does not include JSTL by default and this module
# is required to put JSTL on the container classpath


#
# Initialize module logging
#
--module=logging

# ---------------------------------------
# Module: http
--module=http
### HTTP Connector Configuration
-Djava.net.preferIPv4Stack=true

## HTTP port to listen on
jetty.port=8080

## HTTP idle timeout in milliseconds
http.timeout=30000

## HTTP Socket.soLingerTime in seconds. (-1 to disable)
# http.soLingerTime=-1

## Parameters to control the number and priority of acceptors and selectors
# http.selectors=1
# http.acceptors=1
# http.selectorPriorityDelta=0
# http.acceptorPriorityDelta=0

【问题讨论】:

    标签: java amazon-ec2 jetty logback


    【解决方案1】:

    首先(也是最重要的)不要在${jetty.home} 中更改、编辑、修改、添加、删除、删除、破坏、重命名、触摸等任何内容

    那是你的 jetty-distribution 目录,它不打算改变,解压它,然后不管它。

    它甚至会在您启动时告诉您这一点!

    2015-05-06 06:34:58.838:WARN:oejs.HomeBaseWarning:main: This instance of Jetty is not
    running from a separate {jetty.base} directory, this is not recommended.  
    See documentation at http://www.eclipse.org/jetty/documentation/current/startup.html
    2015-05-06 06:34:58.959:INFO:oejs.Server:main: jetty-9.2.10.v20150310
    

    要解决这个问题:

    首先在某处(任何地方)创建一个新目录,这将成为您的${jetty.base}

    ${jetty.base} 目录是您配置正在运行的 Jetty 实例的位置。

    该文档在${jetty.base} 中有关于 3 种不同的 logback 配置的说明

    如果你遵循这些指示,你会得到:

    • ${jetty.base} 为您提供 start.ini
    • ${jetty.base}/lib/logging 中您需要的库
    • 一个被覆盖的${jetty.base}/modules/logging.mod
    • ${jetty.base}/resources/logback.xml中的logback配置
    • ${jetty.base}/resources/jetty-logging.properties 中的码头日志记录配置

    从这里开始,您可以在 ${jetty.base}/start.ini 中设置您需要的其余模块,以及您需要的 web 应用程序和其他零碎(永远不要编辑 ${jetty.home}

    最后,您将编辑 /etc/default/jetty 以添加指向此目录的 JETTY_BASE

    要测试此${jetty.base} 配置是否有意义,您将使用命令行。

    CD 进入你的新${jetty.base} 并运行

    [mybase]$ java -jar /path/to/jetty-dist/start.jar
    

    这会将配置作为正常进程(而不是服务)启动,您可以从中查看配置是否按照您需要的方式运行。

    【讨论】:

    • 谢谢,乔金。根据您的建议,我创建了 /opt/jetty/base 并在 /etc/default/jetty 中定义了 JETTY_BASE=/opt/jetty/base。但是,它仍然无法正常工作。我仍然可以在 /opt/jetty/base/logs 下创建一个新的 start.log。但是,预期的 server.log 仍然不存在。我怀疑我一定在某处做了不正确的配置,但我就是找不到。有什么建议吗?
    • start.log 不是服务器日志基础设施的一部分,它是实际启动码头的引导程序的一部分,在服务器日志初始化之前发生的事情。直接测试配置,作为一个正常的过程,就像答案说的那样。然后将其作为服务进行测试。
    【解决方案2】:

    我按照官方的例子解决了这个问题:http://eclipse.org/jetty/documentation/current/example-logging-logback-centralized.html。实际步骤与 Joakim 在他的回答中提到的非常相似。

    这些基本上是我使用的命令:

    [base]$ mkdir modules
    [base]$ cd modules
    [modules]$ curl -O https://raw.githubusercontent.com/jetty-project/logging-modules/master/capture-all/logging.mod
    [modules]$ curl -O https://raw.githubusercontent.com/jetty-project/logging-modules/master/centralized/webapp-logging.mod
    [modules]$ cd ..
    [base]$ java -jar /opt/jetty/start.jar --add-to-start=logging,webapp-logging
    

    【讨论】:

      【解决方案3】:

      运行以下命令,Jetty 做所有事情:

      [my-base]$ java -jar ../start.jar --add-to-start=logging-logback
      

      更多配置:https://www.eclipse.org/jetty/documentation/jetty-9/index.html#configuring-logging

      【讨论】:

        猜你喜欢
        • 2014-10-25
        • 1970-01-01
        • 1970-01-01
        • 2016-01-14
        • 2011-12-26
        • 1970-01-01
        • 2017-05-06
        • 2021-06-26
        • 1970-01-01
        相关资源
        最近更新 更多