【问题标题】:Use Log4j with jetty-maven-plugin 9.x将 Log4j 与 jetty-maven-plugin 9.x 一起使用
【发布时间】:2016-02-28 07:23:13
【问题描述】:

如何为jetty-maven-plugin 9 启用Log4j?

我遵循了独立 Jetty 9 的 Jetty documentation 并添加了 JAR 和属性文件。

码头配置:

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.0.5.v20130815</version>
    <configuration>
        <webApp>
            <contextPath>/mywebapp</contextPath>
            <jettyEnvXml>jetty-env.xml</jettyEnvXml>
        </webApp>
        <systemProperties>
            <systemProperty>
                <name>log4j.configuration</name>
                <value>log4j-jetty.properties</value>
            </systemProperty>
        </systemProperties>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
    </dependencies>
</plugin>

我的 webb 应用程序中的 Spring 使用 Log4j 记录,但 Jetty 仍然使用错误的记录器进行记录。

控制台输出:

[INFO] --- jetty-maven-plugin:9.0.5.v20130815:run (default-cli) @ mywebapp ---
[INFO] Configuring Jetty for project: mywebapp
[INFO] webAppSourceDirectory not set. Defaulting to D:\projekte\test\workspace\test-parent\mywebapp\src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = D:\projekte\test\workspace\test-parent\mywebapp\target\classes
[INFO] Context path = /mywebapp
[INFO] Tmp directory = D:\projekte\test\workspace\test-parent\mywebapp\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = file:/D:/projekte/test/workspace/test-parent/mywebapp/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = D:\projekte\test\workspace\test-parent\mywebapp\src\main\webapp
[INFO] jetty-9.0.5.v20130815
[INFO] No Transaction manager found - if your webapp requires one, please configure one.
[INFO] No Spring WebApplicationInitializer types detected on classpath
[INFO] Initializing Spring root WebApplicationContext
<14:03:42,507> <INFO > <ContextLoader> (main) - Root WebApplicationContext: initialization started
<14:03:42,562> <INFO > <XmlWebApplicationContext> (main) - Refreshing Root WebApplicationContext: startup date [Wed Nov 25 14:03:42 CET 2015]; root of context hierarchy
<14:03:42,587> <INFO > <XmlBeanDefinitionReader> (main) - Loading XML bean definitions from ServletContext resource [/WEB-INF/beans.xml]
<14:03:42,694> <INFO > <XmlBeanDefinitionReader> (main) - Loading XML bean definitions from ServletContext resource [/WEB-INF/cxf.xml]
<14:03:42,919> <INFO > <AutowiredAnnotationBeanPostProcessor> (main) - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
[INFO] Setting the server's publish address to be /test
<14:03:43,231> <INFO > <ContextLoader> (main) - Root WebApplicationContext: initialization completed in 724 ms
[INFO] Started o.e.j.m.p.JettyWebAppContext@224f70d3{/mywebapp,file:/D:/projekte/test/workspace/test-parent/mywebapp/src/main/webapp/,AVAILABLE}{file:/D:/projekte/test/workspace/test-parent/mywebapp/src/main/webapp/}
[WARNING] !RequestLog
[INFO] Started ServerConnector@5a88a0f2{HTTP/1.1}{0.0.0.0:8080}
[INFO] Started Jetty Server

我的 Log4j 属性:

log4j.rootLogger=info, stdout
log4j.debug=false

# console logger
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=<%d{ABSOLUTE}> <%-5p> <%c{1}> (%t) - %m%n

【问题讨论】:

    标签: java log4j slf4j maven-jetty-plugin jetty-9


    【解决方案1】:

    在pom.xml中设置日志属性的位置是不够的,必须是命令行参数,见Setting System Properties:

    但是,有时无法使用此功能来设置系统属性 - 有时使用系统属性的软件组件在 maven 运行时已经初始化(在这种情况下,您需要在命令行上提供 System 属性),或者在 Jetty 运行时提供。

    更改的命令行:

    mvn -Dlog4j.configuration=file:///D:/log4j-jetty.properties jetty:run
    

    适用于 Maven 3.0。

    但它不适用于 Maven 3.1,因为 Maven 3.1 包含 SLF4J SimpleLogger,请参阅 Maven 3.1.x logging:

    标准 Maven 发行版,从 Maven 3.1.0 开始,使用 SLF4J API 结合 SLF4J 简单实现进行日志记录

    Jetty 使用 SLF4J Simple 实现自己的日志(而不是 Web 应用程序日志)而不是 Log4j 实现。

    【讨论】:

      猜你喜欢
      • 2013-08-19
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      相关资源
      最近更新 更多