如何为此程序包启用log4j

时间:2013-04-25 18:31:11

标签: java log4j portal-java

我正在尝试为DEBUG

设置log4j 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

根据how to enable package level logging in 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配置正确。

Main.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();
    }
}

Config.java

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));
        }
    }

3 个答案:

答案 0 :(得分:0)

Grizzly使用JUL日志记录:can not get grizzly logging to work, nothing written to my log4j log file。您需要初始化自己的log4j实例以写入stdout,以便将log4j添加到Grizzly日志中。我用:

  <bean id="log4jConfigurer"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
    <property name="targetMethod" value="initLogging" />
    <property name="arguments">
      <list>
        <value>classpath:log4j.properties</value>
      </list>
    </property>
  </bean>

但是,由于您使用的是Spring而不是xml配置的代码配置,因此您可能需要查看Log4jConfigurer的详细信息

答案 1 :(得分:0)

看起来这是我的项目中包含日志包的问题。 Why does this only log for hibernate?中列出的解决方案解决了此问题

答案 2 :(得分:0)

而不是log4j.logger我需要log4j.category

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.category.com.github.flowersinthesand=TRACE
log4j.category.org.atmosphere=TRACE