Log4j2不能与tomcat一起使用

时间:2017-09-24 18:11:12

标签: java log4j

我开始使用log4j2进行日志记录。我的项目部署在tomcat上。在tomcat中,我的记录器不起作用,它在catalina.out中写入日志,并且消息的模式完全不同:测试后的消息和catalina.out的消息如下所示。运行测试时记录的工作正常。

测试后

在我的文件中记录消息

2017-09-24 20:21:57,418 [main]  INFO com.service.AccountService - create account started

并在catalina.out

中记录消息
[http-nio-8085-exec-186] INFO com.service.AccountService - create account started

我的log4j2配置文件(我使用程序化配置)

@Plugin(name = "Log4j2Config", category = "ConfigurationFactory")
@Order(50)
public class Log4j2Config extends ConfigurationFactory {

    private static final String NAME_ROLLING_FILE = "com."; 
    private static final String PATTERN_LAYOUT = "%d [%t]  %-5level %logger{36} - %msg%n";
    private static final String LOG_FILE_NAME = "socialNet.log";
    private static final String LOG_FILE_NAME_PATTERN = "socialNet-%d{dd-MM-yyyy}-%i.log";

    @Override
    public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
        return getConfiguration(loggerContext, source.toString(), null);
    }

    @Override
    public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation) {
        return new Log4jConfiguration();
    }


    @Override
    protected String[] getSupportedTypes() {
        return new String[]{"*"};
    }

    private static class Log4jConfiguration extends DefaultConfiguration {

        public Log4jConfiguration() {
            super.doConfigure();

            initAppenderLogger(getRollingFileAppender(NAME_ROLLING_FILE), getRollingFileLogger(NAME_ROLLING_FILE), Level.INFO, NAME_ROLLING_FILE);

            initAppenderLogger(getRootAppender(getRootLogger().getName()), getRootLogger(), Level.ERROR, getRootLogger().getName());

        }

        private void initAppenderLogger(Appender appender, LoggerConfig loggerConfig, Level level, String loggerName) {
            appender.start();
            addAppender(appender);
            addLogger(loggerName, loggerConfig);
            getLogger(loggerName).addAppender(appender, level, null);
        }

        private Appender getRollingFileAppender(String appenderName) {

            final TimeBasedTriggeringPolicy timeBasedTriggeringPolicy = TimeBasedTriggeringPolicy.newBuilder()
                    .withInterval(1)
                    .withModulate(true)
                    .build();

            String oneMB = "1 M";
            final SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy = SizeBasedTriggeringPolicy.createPolicy(oneMB);
            final CompositeTriggeringPolicy policy = CompositeTriggeringPolicy.createPolicy(timeBasedTriggeringPolicy,
                    sizeBasedTriggeringPolicy);
            final DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.newBuilder()
                    .withMax("14")
                    .withMin("1")
                    .withFileIndex("max")
                    .withConfig(this)
                    .withCompressionLevelStr(Deflater.NO_COMPRESSION + "")
                    .build();

            String path = "/Users/mac/Documents/Programming/Java/projects/socialnet/tmp/log/";

            return RollingFileAppender.newBuilder()
                    .withName(appenderName)
                    .withAdvertise(Boolean.parseBoolean(null))
                    .withAdvertiseUri(null)
                    .withAppend(true)
                    .withBufferedIo(true)
                    .withBufferSize(8192)
                    .withLayout(PatternLayout.newBuilder()
                            .withPattern(PATTERN_LAYOUT)
                            .withConfiguration(this)
                            .build())
                    .withPolicy(policy)
                    .withStrategy(rolloverStrategy)
                    .withFilePattern(path + LOG_FILE_NAME_PATTERN)
                    .withFileName(path + LOG_FILE_NAME)
                    .build();
        }

        private LoggerConfig getRollingFileLogger(String loggerName) {
            return LoggerConfig.createLogger(false, Level.INFO,
                    "loggerName", null, new AppenderRef[]{}, null, this, null);        }

        private Appender getRootAppender(String appenderName){
            return ConsoleAppender.newBuilder()
                    .withName(appenderName)
                    .withLayout(
                            PatternLayout.newBuilder()
                                    .withPattern(PATTERN_LAYOUT)
                                    .build())
                    .build();

        }
    }
}

pom.xml中用于记录的依赖项:

 ...
 <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j.version}</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>${slf4j.version}</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
        <version>${log4j.version}</version>
    </dependency>
...

我错过了什么?

0 个答案:

没有答案