【问题标题】:How to enable log4j in this Grizzly/Jersey application如何在这个 Grizzly/Jersey 应用程序中启用 log4j
【发布时间】:2013-10-18 19:17:16
【问题描述】:

我没有使用任何 XML 配置,而是在运行时做所有事情。

我的应用程序运行并且我的 API 在泽西岛运行,但我没有看到任何正在写入的日志。当此应用程序启动以确认它看到 log4j 配置时,我希望看到类似 INFO: [MyApp] Initializing log4j from [classpath:environment-${MY_ENVIRONMENT}.properties] 的内容。

我已经避免使用 log4j.properties,因为我希望每个应用程序环境都有不同的日志记录配置。

如何让这个应用程序通过我的日志配置写入日志?

我的主要课程是:

import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.WebappContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.context.ContextLoaderListener;

...

protected static HttpServer startServer() throws IOException {
    ResourceConfig rc = new PackagesResourceConfig("com.company.product.api.resources");
    Map<String,Boolean> features = rc.getFeatures();
    features.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

    return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
}

public static void main(String[] args) throws IOException {
    //Without this, ApplicationContextProvider has no context
    AnnotationConfigApplicationContext annotationCtx = new AnnotationConfigApplicationContext(Config.class);

    //The only reason this is here is because I think I need it for log4j config
    WebappContext ctx = new WebappContext("API", "/");

    //enable log4j configuration
    ctx.addListener("org.springframework.web.util.Log4jConfigListener");
    ctx.addContextInitParameter("log4jConfigLocation", "classpath:environment-${MY_ENVIRONMENT}.properties");

    //enable annotation configuration so we can avoid XML
    ctx.addContextInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
    ctx.addContextInitParameter("contextConfigLocation", "com.company.product.api");

    //allow spring to do all of it's stuff
    ctx.addListener(ContextLoaderListener.class);

    HttpServer httpServer = startServer();

    System.in.read();
    httpServer.stop();
}

environment-production.properties,除了log4j之外的所有配置都被正确使用:

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
# Set the name of the file
log4j.appender.FILE.File=/var/log/productionApi/productionApi.log

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true

# Set the DatePattern
log4j.appender.FILE.DatePattern='.' yyyy-MM-dd-a

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

【问题讨论】:

    标签: log4j jersey grizzly


    【解决方案1】:

    根据示例,您没有调用 ctx.deploy(HttpServer)。如果您不这样做,那么您定义的 ServletContext 侦听器将不会被调用(注意:无论您在服务器启动之前还是之后调用 deploy 都没有关系)。

    【讨论】:

      【解决方案2】:

      您可以传递log4j.configuration 系统属性来解决问题。

      例如:

          java -Dlog4j.configuration=environment-production.properties
      

      【讨论】:

      • 这不起作用,我仍然看到关于没有 log4j 配置的警告
      猜你喜欢
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多