0

I have a main class say class A where :

public class A {
    someMethod(){
        log.debug("inside some method in class A");
    }
}

And then class B where :

public class B {
    someOtherMethod(){
        log.debug("inside some other method in class B");
    }
}

Now how do I use loggers here, i.e., to log from both these classes and log into a common log file? What should be configuration of the properties file?

Aravind S
  • 463
  • 2
  • 9
  • 25
  • What good would it bring to have the same logger in the two different and unrelated classes? – Tom Dec 03 '18 at 09:23
  • Sorry my bad, corrected the question now. – Aravind S Dec 03 '18 at 09:25
  • Possible duplicate of [Log4j: How to configure simplest possible file logging?](https://stackoverflow.com/questions/6358836/log4j-how-to-configure-simplest-possible-file-logging) – Tom Dec 03 '18 at 09:27

1 Answers1

0

if you are using log4j, you can add a config.properties file to your resources:

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

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
# this is the ouput file path and name
log4j.appender.file.File=filename.log
log4j.appender.file.MaxFileSize=10MB
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

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

Then you can use like this:

import org.apache.log4j.Logger;

public class A {
    Logger log= Logger.getLogger(A.class);
    someMethod(){
       log.debug("inside some method in class A");
    }

public class B {
    Logger log= Logger.getLogger(B.class);
    someOtherMethod(){
        log.debug("inside some other method in class B");
    }
}
elbraulio
  • 994
  • 6
  • 15