Log4j2 file created but not log written
Asked Answered
P

6

5

I have used log4j2 with springboot, log file has been created but logs are not written in file.

log4j2.properties

name=PropertiesConfig
property.filename = /export/home/apps/logs
appenders = console, file

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/app-frontend.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

loggers=file
logger.file.name=guru.springframework.blog.log4j2properties
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    </dependencies>

method using logger

private static Logger logger = LogManager.getLogger();

    @RequestMapping(value="/check", method = RequestMethod.GET)
    public String healthCheck() {

        logger.debug("Health-check invoked");

        return "Hey, I am fine";
    }

Above I have mention the codes I have used. Still can not find a way to solve. Logs are even not appearing in the console.

Perceivable answered 9/9, 2016 at 5:32 Comment(1)
It seems your log4j2 configuration is not correct. Why dont you try xml (or yaml/json) based configuration instead? ".properties" cannot leverage the full features of log4j2.Underlet
U
4

Usage of "log4j2.properties" for configuration in spring-boot has certain limitations. Log4J 2 did not support configuration through the properties file when it was initially released. It was from Log4J 2.4 that support for the properties file was again added, but with a completely different syntax.As mentioned in the documentation

As of version 2.4, Log4j now supports configuration via properties files. Note that the property syntax is NOT the same as the syntax used in Log4j 1.

As of version 2.6, this list of identifiers is no longer required as names are inferred upon first usage, however if you wish to use more complex identifies you must still use the list. If the list is present it will be used.

As of spring boot release 1.4.0, the log4j2 api version used is 2.6.2. Note that spring-boot uses the slf4j api to support multiple underlying Logging Framework. There seems to be an issue when using properties based configuration for log4j2 without juggling with classpath dependencies for slf4j bindings.

It would make sense to use the XML (or yaml/json) based configuration to achieve the same and to enable the usage of all the features log4j2 is capable of.

Here is an xml based configuration for the same properties.

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
<Properties>
    <Property name="filename">export/home/apps/logs</Property>
</Properties>
<Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
    </Console>
    <File name="LOGFILE"
        fileName="${filename}/app-frontend.log">
        <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
    </File>
</Appenders>
<Loggers>

    <Logger name="guru.springframework.blog.log4j2properties" level="debug">
        <AppenderRef ref="LOGFILE"
            level="debug" />
    </Logger>
    <Root level="debug">
        <AppenderRef ref="STDOUT"/>
    </Root>
</Loggers>
Underlet answered 9/9, 2016 at 6:47 Comment(3)
your answer is well descriptive. Thanks and thumbs up for that..:-) unfortunately, system allow me to give only one up-vote. :-)Perceivable
Glad it helped :)Underlet
So I was using properties config as well. I switched to using the xml config and now logging is working. I think this is the most important thing to mention and I'm on Spring Boot 5x.Polymerous
R
3

Please rename your logger.file.name with your own property.

Rationalize answered 26/3, 2017 at 23:32 Comment(0)
H
2
Logger logger = LogManager.getLogger("My_logger");

In log4j2.xml we should say the name of getLogger("String") to listen and write to file.

<Logger name ="My_logger" level="debug"> 
Hermit answered 13/4, 2017 at 2:4 Comment(0)
S
1

I tried in multiple ways, but log4j2.properties is not working. I tried with so many versions of spring-boot(upto 1.4.3) & log4j2 combinations and came to conclusion

  • Log4j2 - 2.7 is working properly, but spring-boot didn't come with log4j2-2.7 till version (1.4.x).
  • Don't go log4j2.properties for logging with springboot combination
  • Better use log4j2.yml with this combination as geeks are not interested in using xml these days.

Here is the sample how I achieved.

build.gradle

dependencies {

[
    "org.springframework:spring-context",
    "org.springframework.boot:spring-boot-starter-web",
    "org.springframework.boot:spring-boot-starter-data-jpa",
    "org.springframework.boot:spring-boot-starter-test",
    "org.springframework:spring-context-support:4.1.6.RELEASE"
].each { dep -> compile(dep) {
        exclude module: "spring-boot-starter-logging"
    }
}
compile("org.springframework.boot:spring-boot-starter-log4j2")
compile ('org.apache.logging.log4j:log4j-api:2.6.2')
compile ('org.apache.logging.log4j:log4j-core:2.6.2')   
compile ('org.apache.logging.log4j:log4j-slf4j-impl:2.6')
compile ('org.hibernate:hibernate-entitymanager:5.2.2.Final')
compile ('org.hibernate:hibernate-core:5.2.2.Final')
compile ('org.hibernate:hibernate-java8:5.2.2.Final')
compile 'mysql:mysql-connector-java:5.1.31'
compile 'org.apache.commons:commons-lang3:3.0'
compile 'org.freemarker:freemarker:2.3.20'
//compile 'org.apache.velocity:velocity:1.7'
compile 'commons-dbcp:commons-dbcp:1.4'
compile 'javax.servlet:jstl:1.2'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.5'
testCompile 'junit:junit:4.12'
providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper'

}

log4j2.yml

place this file inside src->main->resources

configuration:
  name: Default
  properties:
    property:
    - name: log-path
      value: c:\\logs
    - name: archive
      value: ${log-path}/archive
  appenders:
    Console:
      PatternLayout:
        pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
      name: Console-Appender
      target: SYSTEM_OUT
    File:
      PatternLayout:
        pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
      fileName: ${log-path}/amc.log
      name: File-Appender
    RollingFile:
      DefaultRolloverStrategy:
        max: '30'
      PatternLayout:
        pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB
      fileName: ${log-path}/rollingfile.log
      filePattern: ${archive}/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz
      name: RollingFile-Appender
  loggers:
    logger:
      additivity: 'false'
      appender-ref:
      - level: info
        ref: Console-Appender
      - level: info
        ref: File-Appender
      - level: info
        ref: RollingFile-Appender
      level: debug
      name: <your package>
    root:
      appender-ref:
        ref: File-Appender
      level: info
Saideman answered 20/1, 2017 at 9:33 Comment(0)
P
1

Finally got the solution: Make your logger.file.appenderRefs as root logger

rootLogger.appenderRefs = file, stdout
rootLogger.appenderRef.file.ref = LOGFILE


rootLogger.level = debug
rootLogger.appenderRef.stdout.ref = STDOUT

This will definitely work!! But I'm not able to limit the file size. Even the limit crossed from default file max size(10M) in log4j2.

Ply answered 10/7, 2020 at 7:38 Comment(0)
T
0

In my case, I had to add the next line:

rootLogger.appenderRef.file.ref = LOGFILE

Also review exist: appender.file.fileName

//////////////////////////////////////////////// CONFIGURATION Log4j2.xml ////////////////////////////////////////////////

property.filename = logs
appenders = console, file

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName = ${filename}/application.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} -%msg%n

loggers = file
logger.file.name = guru.springframework.blog.log4j2properties
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

rootLogger.appenderRef.file.ref = LOGFILE

//////////////////////////////////////////////// CONFIGURATION Log4j2.xml (END) ////////////////////////////////////////////////

It works for me.

Sorry for my english. Greetings

Thingumajig answered 19/8, 2020 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.