Redirecting logging with log4j to a file

772 views
Skip to first unread message

Jovan Krička

unread,
Dec 29, 2016, 6:41:49 AM12/29/16
to sparkjava
I have created a simple Spark project, and I included log4j as a dependency and added log4j.properties file to write my logs to a file on the file system.
For some reason this does not work and when I run my application I can only see logs in the console.
The file that I defined for logging is being created, there are even some generic log lines in it, but no log lines that I am logging from my application.

This is dependencies section in my build.gradle file:

dependencies {
	compile 'com.sparkjava:spark-core:2.3'
	compile group: 'org.springframework', name: 'spring-core', version: '4.3.5.RELEASE'
	compile group: 'org.springframework', name: 'spring-beans', version: '4.3.5.RELEASE'
	compile group: 'org.springframework', name: 'spring-context', version: '4.3.5.RELEASE'
	compile group: 'org.springframework', name: 'spring-expression', version: '4.3.5.RELEASE'
	compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.1'
	compile group: 'com.google.code.gson', name: 'gson', version: '1.7.2'
	compile group: 'com.google.guava', name: 'guava', version: '11.0.2'
	compile group: 'log4j', name: 'log4j', version: '1.2.17'
}

This is my log4j.properties file:

# Root logger option
log4j.rootLogger=INFO, stdout, file

# Redirect log messages to 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

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log4j-application.log
log4j.appender.file.MaxFileSize=5MB
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

I have tried modifying log4j.properties to make it work but without success.
I only get my log file created and a couple of lines in it that came from the startup of the application.
Application logs are not there, they are showing only in eclipse console.

What am I doing wrong with this setup?

Jovan Krička

unread,
Jan 4, 2017, 3:10:05 PM1/4/17
to sparkjava

I just wanted simple logging to a file and I added log4j dependency to my project. This was unnecessary since sparkjava already provides slf4j-simple that can be used for this purpose.


I removed log4j dependency from my build.gradle, added simplelogger.properties to my classpath and configured it for writing logs to a file on a disk.

domin...@gmail.com

unread,
Jan 9, 2017, 9:39:06 AM1/9/17
to sparkjava
If you want to use log4j you can. Just add the Log4j binding for SL4J

<dependency>
   
<groupId>org.slf4j</groupId>
   
<artifactId>slf4j-api</artifactId>
   
<version>1.7.22</version>
</dependency>
<dependency>
   
<groupId>org.slf4j</groupId>
   
<artifactId>slf4j-log4j12</artifactId>
   
<version>1.7.22</version>
</dependency>

or in your case with Gradle.

compiile ('org.slf4j:slf4j-api:1.7.22')
runtime
('org.slf4j:slf4j-log4j12:1.7.22')


Reply all
Reply to author
Forward
0 new messages