This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Customize Logger Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Customize Logger Problem" Watch "Customize Logger Problem" New topic
Author

Customize Logger Problem

Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
Hi, I'm working on a project which requires a customize logger where there are a predefined log files to log the output to depending on different business functionalities. For eg. all logging (including different level, debug, error, ...) for classes implementing business function A will go to log file A.

For eg. if I've a StringUtility.isEmpty() and a debug log is implemented there and as different classes implementing different business functionalities can invoke this method, any advise on how do I implement my customize logger such that it'll know which log file to use. For eg. classes implementing business function A invoke StringUtility.isEmpty(), logging should go to log file A. If the class implementing business function B, call this StringUtility.isEmpty(), logging should output to log file B.

I'm following the standard instantiation of the logger object is as follows:



Please advise, thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Too difficult a question for us beginners. Moving.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Can you really get "this" to work after the static keyword?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Apart from that... if you have the requirement to log to two different places, then you will need two different loggers.
Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
Paul Clapham wrote:Apart from that... if you have the requirement to log to two different places, then you will need two different loggers.


Yes, I will need different loggers but the problem is when there are logging in the the common classes, how can the logger know which log to output to?
Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
Campbell Ritchie wrote:Can you really get "this" to work after the static keyword?


Sorry, typed out from my memory. Have updated the codes.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

ABu NeNe wrote:Yes, I will need different loggers but the problem is when there are logging in the the common classes, how can the logger know which log to output to?


You do that in the configuration. But since you need two loggers, then think about it. You can't just use the class name to identify the logger, because if you do that way then you can't have two loggers.
Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
I've the following idea, please take a look:

1. implement the logger class as a singleton
2. create and cache the different logger objects (different logger with different log files) using hashtable when the logger has been instantiated by different classes implementing the business functionalities
3. when the common classes have been invoked by the different classes implementing the business functionalities, logger will use the new Throwable().fillInStackTrace().getStackTrace() to find out the class invoking and lookup in the cache to retrieve the relevant logger.

The concern I've is performance as the operation is quite expensive. Please advise if there are any concern using this approach. Any better suggestion is always welcome.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

My advice is to not do any of that. Either use the built-in classes in the java.util.logging package, or use log4j to do your logging.

Of course if it's really too expensive, then you would just choose not to do it. But right now you have no idea of the costs and haven't measured them at all. So don't try to optimize it because it really isn't too expensive. Just use an existing logging solution and get on with the more important parts of the project.
 
GeeCON Prague 2014
 
subject: Customize Logger Problem