0

I am tryin to log info with Log4j. I have created log4j.properties:

# Root logger option
log4j.rootLogger=INFO, file, stdout
# configuration to print into file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log\\logging.log
log4j.appender.file.MaxFileSize=12MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# configuration to print on console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

included logger:

private static final Logger logger = LogManager.getLogger(UserController.class);

and tried to log:

   logger.debug("Debugging log");
    logger.info("Info log");
    logger.warn("Hey, This is a warning!");
    logger.error("Oops! We have an Error. OK");
    logger.fatal("Damn! Fatal error. Please fix me.");

Yet everything is logged into console ( and debug is ignored ).

My properties file is in \src\main\resources folder, i have tried using xml based configuration but result was the same, no luck with printing it into a file.

Did i make any mistake in configuration? Or where is the root of the problem?

Thanks for help

edit:

I am using maven and spring-boot, i have this depenceny:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
jingx
  • 3,698
  • 3
  • 24
  • 40
Darlyn
  • 4,715
  • 12
  • 40
  • 90
  • 1
    Your `log4j.properties` has a `stdout` appender, and is set to `INFO` level. So it _is_ expected to log to console and ignore debug messages. – jingx Apr 01 '19 at 18:48
  • shouldnt it do both? file and console – Darlyn Apr 01 '19 at 18:56
  • even after removing stdout part, it still prints it into console – Darlyn Apr 01 '19 at 19:05
  • In that case it seems like the file isn't getting picked up. I just saw your dependency part and not sure if you are supposed to directly exclude `starter-logging` from `starter-web`. See the doc https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html – jingx Apr 01 '19 at 19:32

1 Answers1

0

Please read this documentation for customization file naming https://docs.spring.io/spring-boot/docs/1.2.1.RELEASE/reference/htmlsingle/#boot-features-custom-log-levels

Just to be sure, I am assuming you are using Log4j not Log4j2.

At first, the root logging level INFO is not correct. It should be DEBUG in this context. There is a nice SO Post regarding level hierarchy here log4j logging hierarchy order

So, the root logger should be as following

log4j.rootLogger=DEBUG, file, stdout

Finally, it seems the exclude XML is not correct. You will find the configure Log4j for logging doc here https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html Please note that, in this doc the reference is to Log4j2. For our case we need to add Log4J dependency. Here is my XML:

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

    <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>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

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