[logback-user] Localized log level names

ceki ceki at qos.ch
Wed May 30 16:27:38 CEST 2012


Hi Dario,

Have you considered customizing LevelConverter [1] ? Here is a untested 
implementation called CAL10NLevelConverter:

public class CAL10NLevelConverter extends ClassicConverter {

   public String convert(ILoggingEvent le) {
       Level level = le.getLevel();
       IMessageConveyor mc = new MessageConveyor(Locale.getDefault());
       return mc.getMessage(level.toString();
   }
}


You can register a conversion word for CAL10NLevelConverter in the 
configuration file itself. Here is an example:

<configuration>
   <conversionRule conversionWord="cal10nLevel"
                   converterClass="some.package.CAL10NLevelConverter" />
   ....
</configuration>


Once the cal10NLevel is registered, it can be used in any pattern. Example:

<configuration>

   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <encoder>
       <pattern>%d [%thread] %cal10nLevel- %msg%n</pattern>
     </encoder>
   </appender>
   ...
</configuration>

Custom converters are documented at [2].

CAL10NLevelConverter allows you to obtain the desired behavior without 
needing to modify the Level class.

HTH,

-- 
Ceki
http://twitter.com/#!/ceki

[1] 
http://logback.qos.ch/xref/ch/qos/logback/classic/pattern/LevelConverter.html
[2] http://logback.qos.ch/manual/layouts.html#customConversionSpecifier

On 30.05.2012 16:13, Dario Campagna wrote:
> Hi all,
>
>     Few days ago I started experimenting with the org.slf4j.cal10n
> package for generating localized logs using logback.
> That package easily allow one to obtain localized log messages.
>
> Since SLF4J does not support localization of logger names and I need
> them to be localized too, I made some modification to logback-classic
> and used CAL10N to internationalize level names.
>
> More precisely, I defined an enum type named Levels with the keys
> TRACE, DEBUG, INFO, WARN and ERROR.
> I created resource bundles for some locales, e.g. levels_ja.properties
> for the Japanese locale. I added to the Level class the constructor
>
>   private Level(int levelInt, Enum<?>  key) {
> 	this.levelInt = levelInt;
> 	
> 	IMessageConveyor mc = new MessageConveyor(Locale.getDefault());
> 	this.levelStr = mc.getMessage(key);
>    }
>
> Finally, I defined the TRACE, DEBUG, INFO, WARN and ERROR using the
> above constructor, e.g, for level TRACE
>
> public static final Level TRACE = new Level(TRACE_INT, Levels.TRACE);
>
> Thanks to this modification I am know able to obtain localized level
> names in log files. I am wondering if this changes may be useful for
> other logback users. If you have any suggestion/comment please do not
> hesitate to respond to this email.
>
>
> Cheers,
> Dario


More information about the Logback-user mailing list