Howie
Howie

Reputation: 2778

Separate log files for separate instances of same class

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.

Upvotes: 0

Views: 919

Answers (1)

SJuan76
SJuan76

Reputation: 24895

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);
}

Upvotes: 1

Related Questions