我正在尝试设置 log4jDEBUG
级别com.github.flowersinthesand
我的 log4j.properties:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c:%L - %m%n
log4j.logger.com.github.flowersinthesand=DEBUG,stdout
根据如何在 log4j 中启用包级别日志记录,除非我的包名称不正确,否则我正在正确执行此操作。我从 maven 获得了包名,假设它会涵盖所有库。
<dependency>
<groupId>com.github.flowersinthesand</groupId>
<artifactId>portal-core</artifactId>
<version>0.6</version>
</dependency>
<dependency>
<groupId>com.github.flowersinthesand</groupId>
<artifactId>portal-spring</artifactId>
<version>0.6</version>
</dependency>
为什么这个包根本没有被记录?
更新
作为对 Lucas 确认我的 log4j 配置正确的回应。
主.java
package com.production;
public class Main {
protected static final Logger logger = Logger.getLogger(Main.class.getName());
public static void main(String[] args) throws IOException {
logger.info("Starting server...");
final HttpServer server = HttpServer.createSimpleServer(".", 80);
WebappContext ctx = new WebappContext("Socket", "/");
//enable annotation configuration
ctx.addContextInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
ctx.addContextInitParameter("contextConfigLocation", "com");
//allow spring to do all of it's stuff
ctx.addListener("org.springframework.web.context.ContextLoaderListener");
//enable web socket support
final WebSocketAddOn addon = new WebSocketAddOn();
for (NetworkListener listener : server.getListeners()) {
listener.registerAddOn(addon);
//if false, local files (html, etc.) can be modified without restarting the server
listener.getFileCache().setEnabled(false);
}
//add jersey servlet support
ServletRegistration jerseyServletRegistration = ctx.addServlet("JerseyServlet", new SpringServlet());
jerseyServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "com.resource");
jerseyServletRegistration.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.resource.ResponseCorsFilter");
jerseyServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
jerseyServletRegistration.setLoadOnStartup(1);
jerseyServletRegistration.addMapping("/api/*");
//deploy
logger.info("Deploying server...");
ctx.deploy(server);
server.start();
//start the production process
Production.init();
System.in.read();
server.stop();
}
}
配置文件
package com;
@Configuration
@ComponentScan(basePackages = {
"com"
})
@PropertySource(value= {
"classpath:/application.properties",
"classpath:/environment-${MY_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.repository")
@EnableTransactionManagement
public class Config {
@Value("${db.url}")
String PROPERTY_DATABASE_URL;
@Value("${db.user}")
String PROPERTY_DATABASE_USER;
@Value("${db.password}")
String PROPERTY_DATABASE_PASSWORD;
@Value("${persistenceUnit.default}")
String PROPERTY_DEFAULT_PERSISTENCE_UNIT;
@Value("${hibernate.dialect}")
String PROPERTY_HIBERNATE_DIALECT;
@Value("${hibernate.format_sql}")
String PROPERTY_HIBERNATE_FORMAT_SQL;
@Value("${hibernate.show_sql}")
String PROPERTY_HIBERNATE_SHOW_SQL;
@Value("${entitymanager.packages.to.scan}")
String PROPERTY_ENTITYMANAGER_PACKAGES_TO_SCAN;
@Autowired
private BeanFactory beanFactory;
@Bean
public DataSource dataSource() {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUrl(PROPERTY_DATABASE_URL);
dataSource.setUser(PROPERTY_DATABASE_USER);
dataSource.setPassword(PROPERTY_DATABASE_PASSWORD);
return dataSource;
}
@Bean
public JpaTransactionManager transactionManager() throws ClassNotFoundException {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPersistenceUnitName(PROPERTY_DEFAULT_PERSISTENCE_UNIT);
entityManagerFactoryBean.setPackagesToScan(PROPERTY_ENTITYMANAGER_PACKAGES_TO_SCAN);
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", PROPERTY_HIBERNATE_DIALECT);
jpaProperties.put("hibernate.format_sql", PROPERTY_HIBERNATE_FORMAT_SQL);
jpaProperties.put("hibernate.show_sql", PROPERTY_HIBERNATE_SHOW_SQL);
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
@Bean
public ApplicationContextProvider applicationContextProvider() {
return new ApplicationContextProvider();
}
/**
* This bean is required for @Value injection from .properties files
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer () {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public App app() {
return new App(new Options().url("/socket/workstation/approval").packageOf(ApprovalSocketHandler.class), new SpringModule(beanFactory));
}
}