Tatkal
Tatkal

Reputation: 568

log4j2 not writing into file

log4j2.xml

I have checked several questions on this error but I haven't able to figure why data is not writing into log file.

<?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 seems to be fine and even dependencies have been checked.

<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>

file Spring boot application used in this example.

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

Log file is completely blank when I run this program. Not able to figure out the exact problem.

Upvotes: 3

Views: 7249

Answers (1)

giuliorm
giuliorm

Reputation: 128

Pay attention to the "name" property in the logger configuration:

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

The name is com.example, so, the logger retrieval in the class should be

Logger logger = LoggerFactory.getLogger("com.example");

Also, remove the property immediateFlush="false" on the RollingRandomAccessFile appender. For some reasons, the file remains empty even if the logger names are correct.

And remove the line with the logger context initialization, it's not required to initialize the logger.

LoggerContext loggerContext = Configurator.initialize("hzServer","log4j2.xml");

Hope that helps.

Upvotes: 6

Related Questions