将Log4j与jetty-maven-plugin 9.x一起使用 [英] Use Log4j with jetty-maven-plugin 9.x

查看:103
本文介绍了将Log4j与jetty-maven-plugin 9.x一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何启用 Log4j .eclipse.org/jetty/documentation/current/jetty-maven-plugin.html"rel =" nofollow noreferrer> jetty-maven-plugin 9 ?

How can I enable Log4j for jetty-maven-plugin 9?

我遵循了 Jetty文档用于独立Jetty 9,并添加了JAR和属性文件.

I followed the Jetty documentation for standalone Jetty 9 and added JARs and property file.

码头配置:

<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>

Spring在我的Webb应用程序内部使用Log4j进行日志记录,但是Jetty仍然使用错误的记录器进行日志记录.

Spring inside my webb app logs with Log4j, but Jetty still logs with the wrong logger.

控制台输出:

[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

推荐答案

pom.xml中设置日志属性的位置还不够,它必须是命令行参数,请参见

It is not enough to set the location of the log properties in the pom.xml, it has to be a command line argument, see Setting System Properties:

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

However, sometimes it is not possible to use this feature to set System properties - sometimes the software component using the System property is already initialized by the time that maven runs (in which case you will need to provide the System property on the command line), or by the time that Jetty runs.

更改的命令行:

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

与Maven 3.0兼容.

works with Maven 3.0.

但是它不适用于Maven 3.1,因为Maven 3.1包含SLF4J Maven 3.1.x日志记录:

But it doesn't work with Maven 3.1, because Maven 3.1 contains SLF4J SimpleLogger, see Maven 3.1.x logging:

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

The standard Maven distribution, from Maven 3.1.0 onward, uses the SLF4J API for logging combined with the SLF4J Simple implementation

Jetty将SLF4J Simple实现用于其自己的日志(不适用于Web应用程序日志),而不是Log4j实现.

and Jetty uses SLF4J Simple implementation for its own logs (not for web application logs) instead of Log4j implementation.

这篇关于将Log4j与jetty-maven-plugin 9.x一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆