Anup Ganatra
Anup Ganatra

Reputation: 366

Log4j printing to console every time

I have placed following log4j.properties file in src folder of my eclipse dynamic web project. But everytime logs are getting printed on console though I am writing code to print the logs in file only. If I am deleting log4j.properties file then also it is getting printed on console.Don't know what went wrong.

###############################################################################

log4j.rootLogger=INFO, file

###############################################################################

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/apache-tomcat-7.0.47/webapps/LogFile.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %-5p %c - %m%n

###############################################################################

I am using following code to print the logs.

import org.apache.log4j.Logger;

Logger log = Logger.getLogger(Client.class.getName());
log.info("First log");

Upvotes: 0

Views: 1256

Answers (2)

Anup Ganatra
Anup Ganatra

Reputation: 366

Thanks Nikhil

I have used follwing code by looking your code and it worked fine.

import org.apache.log4j.PropertyConfigurator;

PropertyConfigurator.configure(getClass().getClassLoader().getResourceAsStream("log4j.properties"));

Upvotes: 1

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

Try to override the log4j configuration in your class like this:

import org.apache.log4j.PropertyConfigurator;;

static{
     PropertyConfigurator.configure("src/main/resources/log4j.properties");
}

This assumes you have a file called log4j.properties under src/main/resources

Upvotes: 2

Related Questions