如何在此Grizzly/Jersey应用程序中启用log4j
我没有使用任何XML配置,而是在运行时执行所有操作 我的应用程序运行,API工作,但我没有看到任何日志正在编写。我希望看到类似于从[classpath:environment-${MY_environment}.properties]初始化log4j的如何在此Grizzly/Jersey应用程序中启用log4j,log4j,jersey,grizzly,Log4j,Jersey,Grizzly,我没有使用任何XML配置,而是在运行时执行所有操作 我的应用程序运行,API工作,但我没有看到任何日志正在编写。我希望看到类似于从[classpath:environment-${MY_environment}.properties]初始化log4j的INFO:[MyApp] 当此应用程序启动以确认它看到log4j配置时 我避免使用log4j.properties,因为我希望根据应用程序的环境进行不同的日志记录配置 如何通过我的日志配置获取此应用程序的写入日志? 我的主要课程是: import
INFO:[MyApp]
当此应用程序启动以确认它看到log4j配置时
我避免使用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();
}
您可以传递
log4j.configuration
system属性来执行此操作
例如:
java -Dlog4j.configuration=environment-production.properties
根据该示例,您没有调用ctx.deploy(HttpServer)。如果您不这样做,那么您定义的ServletContext侦听器将不会被调用(注意:无论您是在服务器启动之前还是之后调用deploy都没有关系)。这不起作用,我仍然看到关于no log4j config的警告
java -Dlog4j.configuration=environment-production.properties