Based on the example, you didn't call ctx.deploy(HttpServer). If you don't do that, then the ServletContext listener you've defined won't be invoked (note: it doesn't matter if you call deploy before or after the server has started).
How to enable log4j in this Grizzly/Jersey application
题
I'm not using any XML configuration and am doing everything at run time.
My application runs and Jersey my API works, but I'm not seeing any logs being written. I expect to see something like INFO: [MyApp] Initializing log4j from [classpath:environment-${MY_ENVIRONMENT}.properties]
when this application boots up to confirm it's seeing the log4j configuration.
I've avoided using log4j.properties
since I want different logging configuration per my application's environment.
How can I get this application writing logs via my log configuration?
My main class is:
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();
}
In environment-production.properties
, all configuration is properly being used, except for 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
解决方案
其他提示
You may pass log4j.configuration
system property to do the trick.
For example:
java -Dlog4j.configuration=environment-production.properties