Michal Krasny
Michal Krasny

Reputation: 5926

How to release a Log4J logger

I'm creating a program, that will run 24/7 and will continually process multiple tasks. These tasks can run in parallel. Every task will have it's independent log file. So for every task I want to create own logger like this:

Logger logger = Logger.getLogger("taskID");

How can I correctly release the logger so it is no longer in memory, after the task is done?

Upvotes: 0

Views: 473

Answers (2)

kazbeel
kazbeel

Reputation: 1436

This is not the way a Logger should be instantiated. You must always make them static and final. Doing so you don't have to worry about it anymore as it's going to be only and only one instance of Logger per class.

Take a look at the official documentation and some manuals online. This book is also very good to get started.

PS: On the other hand, I would recomend you the use of SLF4j as façade.

Upvotes: 1

Stephen C
Stephen C

Reputation: 719446

There is no way to "release" a Logger object. But that's OK. If it is reachable you can still use it ... and it shouldn't be "released". If it is unreachable, the GC will reclaim it.

By the way, if you are really talking about log4j, then the method you call to get hold of a named logger is Logger.getLogger(String). It is defined to return an existing instance (with the same name) if one exists, so you don't need to worry about creating lots of copies of the same logger.

Upvotes: 2

Related Questions