Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4J is Not Writing to Specific Log File in Spring Boot Microservice

Am trying to use Log4j to write to a local log file in my filesystem.

Actually used the exact properties file from a different project and just changed the name of the top level directory to match my app's name. The different project writes to the logs.log file but this doesn't print any content at all. Both projects use the same version of log4j.

pom.xml

<dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.17</version>
</dependency>

On my Unix based macOS, I cd'ed into /var/log/ and did the following:

sudo mkdir myapp
chmod 777 myapp

Have inside myapp, the following setup all over the place:

if (log.isDebugEnabled() {
   log.debug("print something");
}

myapp/src/main/resources/log4j.properties:

log4j.rootCategory=DEBUG, RF

log4j.category.your.category.name=DEBUG

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%-5p [%F]: %m [%d{ISO8601}]%n
log4j.logger.com.amazonaws=DEBUG RF

log4j.appender.RF=org.apache.log4j.RollingFileAppender
log4j.appender.RF.File=/var/log/myapp/logs.log

log4j.appender.RF.MaxFileSize=10MB
log4j.appender.RF.MaxBackupIndex=30
log4j.appender.RF.layout=org.apache.log4j.PatternLayout
log4j.appender.RF.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x(%F:%L) - %m%n
Threshold=DEBUG

Don't understand why inside:

/var/log/myapp/ 

There's no logs.log file present!

Am using this inside a Spring Boot 1.5.4.RELEASE which I build using:

mvn clean install

And then run using either:

java -jar myapp.jar 

or

mvn spring-boot:run

Would appreciate if someone could provide me with either a better properties file or see what I am doing wrong?

Thank you for taking the time to read this.

like image 769
PacificNW_Lover Avatar asked Sep 12 '17 02:09

PacificNW_Lover


1 Answers

@Veeram is right - the logs are printed by logback, which comes along with Spring Boot. You need to replace it by Log4j. However, now it is not an option:

From Spring Boot 1.4.0 Release Notes:

Log4j 1 support has been removed following Apache EOL announcement.

So I may suggest you consider an upgrade to Log4j 2.

P.S. I think you still can use Log4j 1.2, but in this case, you have to define and specify which dependencies should be excluded from spring-boot-starter-logging module, and which should be added instead. I've set up a demo project to reproduce your problem and here is the list of dependencies in build.gradle (I use Gradle, but I think you get a point):

dependencies {
    compile('org.springframework.boot:spring-boot-starter') {
        exclude group: 'ch.qos.logback'
        exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
    }
    compile('commons-logging:commons-logging:1.2')
    compile('log4j:log4j:1.2.17')
    compile('org.slf4j:slf4j-log4j12:1.7.25')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
like image 189
mxf Avatar answered Sep 27 '22 18:09

mxf