我没有使用任何 XML 配置,而是在运行时做所有事情。
我的应用程序运行并且我的 API 可以在泽西岛运行,但我没有看到任何正在写入的日志。我希望INFO: [MyApp] Initializing log4j from [classpath:environment-${MY_ENVIRONMENT}.properties]
在这个应用程序启动时看到类似的东西,以确认它看到了 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();
}
在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