如何在这个Grizzly / Jersey应用程序中启用log4j

时间:2013-10-18 19:17:17

标签: log4j jersey grizzly

我没有使用任何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

2 个答案:

答案 0 :(得分:0)

您可以通过log4j.configuration系统属性来执行此操作。

例如:

    java -Dlog4j.configuration=environment-production.properties

答案 1 :(得分:0)

根据示例,您没有调用ctx.deploy(HttpServer)。如果不这样做,则不会调用您定义的ServletContext侦听器(注意:如果在服务器启动之前或之后调用deploy,则无关紧要。)