Ibolit
Ibolit

Reputation: 9720

Log4j, dynamic file name based on .getLogger("") parameter

My application controls an arbitrary number of devices. I want to have log files for each of the devices. Basically, i want to be able to call LogManager.getLogger(deviceId) and log the corresponding thing.

I looked through a number of topics here and on other sites, still i am not quite sure if it is possible or not. I am not quite sure whether i should extend an appender or a logger.

How does one go about solving this task?

Upvotes: 1

Views: 539

Answers (1)

Nils De Winter
Nils De Winter

Reputation: 1490

One solution, though it's not optimal, would be to add a fileappender in the constructor of your device object :

String device = "thing"
Logger log1 = Logger.getLogger("org.path."+device);
log1.setAdditivity(false);
log1.addAppender(new FileAppender(new SimpleLayout(), "org.path."+device ));

Upvotes: 1

Related Questions