0

我正在尝试设置 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));
        }
    }
4

3 回答 3

0

看起来这是我的项目中包含的日志记录包的问题。为什么只为休眠记录日志中概述的解决方案?解决了这个问题

于 2013-04-26T11:36:12.537 回答
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
于 2013-05-08T19:11:03.477 回答
0

Grizzly 使用 JUL 日志记录:无法让 grizzly 日志记录工作,没有写入我的 log4j 日志文件。您需要初始化自己的 log4j 实例以写入标准输出,以便将 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的详细信息

于 2013-04-25T19:12:36.923 回答