log4j2没有写入文件

时间:2018-05-21 10:02:39

标签: java log4j2

log4j2.xml

我已经检查过有关此错误的几个问题,但我无法确定数据未写入日志文件的原因。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>

    <Appenders>

        <RollingRandomAccessFile name="HzServer" 
                                 filename="logs/hzServer.log" immediateFlush="false" append="true"
                                 filepattern="logs/hzServer.log-%d{MM-dd-yyyy}">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}  %highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue} - %msg%throwable%n" />
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingRandomAccessFile>

    </Appenders>
    <Loggers>
        <Logger name="com.example" level="debug" includeLocation="true"
                         additivity="false">
                <AppenderRef ref="HzServer" />

         </Logger>
     </Loggers>
</Configuration>

的pom.xml

Config似乎很好,甚至已经检查了依赖项。

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.8.2</version>
</dependency>

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

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.8.2</version>
</dependency>

文件 此示例中使用的Spring启动应用程序。

@SpringBootApplication
@ComponentScan
public class HzServer implements CommandLineRunner
{
    LoggerContext loggerContext = Configurator.initialize("hzServer","log4j2.xml");
    Logger logger = LoggerFactory.getLogger(HzServer.class);

    @Autowired
    HazelcastInstance hzInstance;

    public static void main(String[] args) 
    {
        SpringApplication.run(HzServer.class);
    }

    @Override
    public void run(String... args) throws Exception 
    {       
        ITopic<String> topic = hzInstance.getTopic("myTopic");

        topic.publish("HelloWorld");

        logger.info("Topic published");
        logger.debug("Topic published");
        logger.error("Topic published");
        logger.trace("Topic published");        
    }
}

运行此程序时,日志文件完全空白。无法弄清楚确切的问题。

1 个答案:

答案 0 :(得分:0)

注意记录器配置中的“name”属性:

if (color.equals("yellow")) {
  factory = new GiraffeFactory();
} else {
  factory = new FlamingoFactory();
}

Animal animal = factory.createAnimal();
animal.breathe();
animal.walk();

名称为((Flamingo) animal).fly(); ,因此,类中的记录器检索应为

<Logger name="com.example" level="debug" includeLocation="true" 
    additivity="false">
    <AppenderRef ref="HzServer" />
</Logger>

此外,删除com.example appender上的属性Logger logger = LoggerFactory.getLogger("com.example"); 。由于某些原因,即使记录器名称正确,文件仍为空。

使用记录器上下文初始化删除行,不需要初始化记录器。

immediateFlush="false"

希望有所帮助。