'log4j2 Does Not Write to Log File from Spring Boot Controller in MVC

My apologies if this is a duplicate of another question, but I haven't been able to find a fix in any of the other answers I've found on here yet. The code excerpts below are not all-inclusive of all the code in the files but it has all of the relevant pieces included. I'm unable to completely copy/paste my entire files as this is for a corporate project.

I'm fairly new to spring boot (maybe 10 weeks into using it) and this is the first time I've ever tried using log4j2 (have to use this within our corporate environment.)

build.gradle excerpt:

dependencies {
        ....
        // log4j2
        compile("org.springframework.boot:spring-boot-starter-log4j2")
        compile("org.apache.logging.log4j:log4j-api")
        compile("org.apache.logging.log4j:log4j-core")
        compile("org.apache.logging.log4j:log4j-web")
        ....
}

ApplicationController.java:

package com.test.controllers;

import ...
....
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Controller
public class ApplicationController {

    public static final Logger logger = LogManager.getLogger(Application.class);
}

IndexController.java:

package com.test.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController extends ApplicationController {

  public IndexController() {
  }

  @RequestMapping("/")
  public String index(Model model) {
    // just a quick example of logging
    logger.debug("--INDEX PAGE--");
    return "index/index";
  }
}

Application.java (my log file has 1 entry and it's from this class - the "Starting application..." line):

package com.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static final Logger logger = LogManager.getLogger(Application.class);

    public static void main(String[] args) {
        logger.debug("Starting application...");
        SpringApplication.run(Application.class, args);
        logger.debug("ending application...");
    }

}

log4j2.properties:

status = debug
name = PropertiesConfig

property.filename = /var/tmp/logs/test.log

filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appenders = rolling

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20

loggers = rolling

logger.rolling.name = com.test
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

logger.rolling.name = com.test.controllers
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

rootLogger.level = debug
rootLogger.appenderRef.rolling.ref = RollingFile

With the above code, my application creates the /var/tmp/logs/test.log file and writes a single entry to it:

2017-09-28 13:54:56,141 DEBUG c.t.Application [main] Starting application...

However, I also need to access the logger from within my Controllers/etc to allow my entire app to write to this log file. I've attempted to call a logger in my ApplicationController.java and then use it in my IndexController.java. I see the entry from IndexController in my console but it does not write to the log file.

What am I doing wrong? Any help here would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source