Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to put log4j.properties in java gradle web application on eclipse

I am making a simple web application using Gradle. I am using slf4j and log4j as loggers, I added 3 jars using gradle.build file to work with slf4j. I went through the some gradle and slf4j tutorials and still I could not find and answer that where to put log4j.properties in gradle project. Can any body help me for that ?

like image 297
Kasun Kariyawasam Avatar asked Dec 19 '13 05:12

Kasun Kariyawasam


2 Answers

enter image description here

 //import org.slf4j.Logger;

//import org.slf4j.LoggerFactory;

import org.apache.log4j.Logger;

public class Log4jTest {

//Slf4J
//final static Logger logger = LoggerFactory.getLogger(Log4jTest.class);


//Log4J
final static Logger logger = Logger.getLogger(Log4jTest.class);

public static void main(String[] args) {
    logger.info("Logging with Log 4J starts");

    Log4jTest obj = new Log4jTest();

    try{
        obj.divide();
    }catch(ArithmeticException ex){
        logger.error("Sorry, something wrong!", ex);
    }


}

private void divide(){

    int i = 10 /0;

}

}

** Log4J property file**

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

# 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

# Rirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=E:\\Logging\\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

** Dependency for build.gradle**

dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
compile 'log4j:log4j:1.2.17'
compile 'org.slf4j:slf4j-log4j12:1.7.18'
}
like image 85
Deepak Avatar answered Oct 16 '22 10:10

Deepak


log4j.properties is typically read from the web app's class path, and as such you would put it into src/main/resources.

like image 11
Peter Niederwieser Avatar answered Oct 16 '22 10:10

Peter Niederwieser