0

I have a Client and Server class. There is one Client instance running and eight Server instances running at the same time in the same JVM.

I now wish to (programatically or through properties file) setup log4j so that I'd have one log file per class instance. For the example above I'd like to have 9 separate log files.

4
  • Why do you run them in the same VM?
    – user647772
    Commented Jul 24, 2012 at 8:33
  • You're probably not going to like this, but it seems you are using logging for debugging purposes, which is an antipattern. Better write automated tests. Commented Jul 24, 2012 at 8:41
  • I'm currently trying to pinpoint the location where the problem occurs, before I actually go debugging. The funny thing is that I'm actually debugging a unit test... @ChristianSchlichtherle What I'm doing feels wrong. Can you perchance point me to some resource on this antipattern?
    – Howie
    Commented Jul 24, 2012 at 9:44
  • Well, it was a general comment - no offense intended. But rather than adding logging statements, I would write a unit test which tests for multithreading. There are plenty of resources on the web about writing unit tests, so you sure find some good tutorials. Commented Jul 24, 2012 at 10:58

1 Answer 1

1

If you are using the same Logger for all the instances (v.g., private static final Logger log = Logger.getLogger(MyClass.class), all logs will follow the same process.

You should define the logger as an instance attribute and setup a different behavior for each one in the log4j.properties, v.g.

private final Logger log;

public MyClass(String instanceID) {
  this.log = Logger.getLogger(MyClass.class.toString() + "_" + instanceID);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.