无法使用jetty使用logback进行日志记录

时间:2015-05-06 05:04:51

标签: java amazon-ec2 jetty logback

我跟着this page在我的Amazon EC2实例(Ubuntu 14.04.2 LTS)上安装了jetty。 Jetty正在运行。但是,我无法使用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 /默认/码头

$ 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

3 个答案:

答案 0 :(得分: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种不同配置的说明

如果你按照这些指示行事,你最终会接受:

  • ${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中设置您需要的其余模块以及您的网络应用程序以及您需要的其他位置(从未编辑过${jetty.home})< / p>

最后,您要修改/etc/default/jetty以添加指向此目录的JETTY_BASE

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

将CD放入新的${jetty.base}并运行

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

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

答案 1 :(得分:0)

我按照官方示例解决了这个问题: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

答案 2 :(得分:0)

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

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

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

相关问题