aspose file tools*
The moose likes Other Open Source Projects and the fly likes Internationalized logging - log4j 1.2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Internationalized logging - log4j 1.2" Watch "Internationalized logging - log4j 1.2" New topic
Author

Internationalized logging - log4j 1.2

Dave Mark
Greenhorn

Joined: Feb 21, 2004
Posts: 19
Does anyone know of a way to internationalize the levels : DEBUG, INFO, WARN, ERROR, FATAL

So that ERROR --> ERREUR

The messages themselves will be i18ned via resource bundles but I can't find anything in the log4j documentation to internationalize the level names. Anybody any ideas?
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
well normally you do logger.log("a string"); so instead of putting a string make a singleton that has a reference to a group of resource bundles that contains the different messages to log, and depending on the locale will put the correct log string. and your logging now would be like this:

logger.log(I18NLoggerSingleton.getString("key.in.properties.file", ... <a list of parameters if needed>);


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Dave Mark
Greenhorn

Joined: Feb 21, 2004
Posts: 19
Thanks for the quick reply Omar but it's the level I'm trying to internationalize, we already use i18ned messages. I currently have this...

17:42:17,218 DEBUG AccueilAction:? - userId: user0000
17:42:17,218 ERROR AccueilAction:? - Test erreur

after going through loads of examples I came across this one so I tried it out (note line 08.)...It works! ...but now I'm getting two messages for only one log.error...

17:42:17,218 DEBUG AccueilAction:? - userId: user0000
17:42:17,218 ERROR AccueilAction:? - Test erreur
17:42:17,218 ERREUR AccueilAction:? - Test erreur
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Dave Mark wrote:but I can't find anything in the log4j documentation to internationalize the level names. Anybody any ideas?


That's most likely because there isn't anything.

If you're looking for ideas, how about this one? Log4J is open source. You can change it if you like.
Dave Mark
Greenhorn

Joined: Feb 21, 2004
Posts: 19
Paul Clapham wrote:Log4J is open source. You can change it if you like.


Thanks for the reply Paul, but unfortunately the project has certain "recommendations" which they've imposed on us and they could change their mind anytime (which they've already done for something else)

I think I'll have to read up on extending ConsoleApender and DailyRollingFileApender to see if I can just do something like String.replace("ERROR", ERREUR).

Wish me luck. I'll post back what I come up with.

Cheers,

Dave
Dave Mark
Greenhorn

Joined: Feb 21, 2004
Posts: 19
Got it

Thanks for the nudge in the right direction Paul

Thanks to Chapter 4 of Apress Pro Apache Log4j and the "Layout" section of this Czech web page here's what I came up with...

I18nedPatternLayout.java (line 13.)log4j.properties (line 05.)and I get...If they ask, I'll see if I can push the values into the log4j.properties file so I can do something like this...
Dave Mark
Greenhorn

Joined: Feb 21, 2004
Posts: 19
Here's an updated version that searches for the level names in the log4j.properties file...and here's the output...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Glad that helped. Yes, subclassing is the way to go, I've done that myself.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Internationalized logging - log4j 1.2