mogoli
mogoli

Reputation: 2375

Log4j to write to one file per instance

I'm writing an application that will on a tomcat server. The application is instantiated multiple times for each client we serve.

I'm introducing Log4j to the application. A client file will be created and log outputs will be written to it.

The problem Im facing is that every time a new instance is created, log outputs from other clients are written to the new client log file.

Within the app, I instantiate the logger for each log like this:

private static final Logger logger = Logger.getLogger(name_of_class.class);

How can I make sure that previous instances of the application will only log to its own file and not spill over to other clients?

Upvotes: 2

Views: 643

Answers (1)

mogoli
mogoli

Reputation: 2375

So I Managed to solve it.

The problem lied in naming the logger after class it resided in. What I did was move the logger declaration to a method that is called by the other classes.

When the method is called, it instantiates a logger named after the client i.e.

Logger.getLogger("ClientName");

This way the outputs are always written to the client file only, even if the classes are instantiated the client logger is called.

Thanks for helping me through the thought process.

Upvotes: 1

Related Questions