Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Internationalized logging - log4j 1.2

 
Dave Mark
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>);

 
Dave Mark
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an updated version that searches for the level names in the log4j.properties file...and here's the output...
 
Paul Clapham
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad that helped. Yes, subclassing is the way to go, I've done that myself.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic