[logback-user] Localized log level names

ceki ceki at qos.ch
Tue Jun 5 10:07:49 CEST 2012


Hi Dario,

Thanks for sharing this code. It's probably a minor detail but instead
of switching on a String which requires JDK 7, you could switch on the
integer corresponding to a Level. The Level.toInt() method [1] returns
this integer. The integer constant for DEBUG is given by
Level.DEBUG_INT [2], for INFO by Level.INFO_INT and so forth.

BTW, the code for the Levels enum is missing. For future reference, it
would be very nice if you could create a github project for this code.

Best regards,

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

[1] 
http://logback.qos.ch/apidocs/ch/qos/logback/classic/Level.html#toInt%28%29
[2] 
http://logback.qos.ch/apidocs/ch/qos/logback/classic/Level.html#DEBUG_INT

On 05.06.2012 09:20, Dario Campagna wrote:
> Hi all,
>
>     Another update regarding the localization of log level names.
>
> I made some modifications to the custom converter for level names
> localization I implemented following Ceki's suggestion (i.e.,
> CAL10NLevelConverter). The following is the converter I obtained.
>
> public class CAL10NLevelConverter extends ClassicConverter {
>
>      private static String LOC_TRACE = locLevelName("TRACE");
>      private static String LOC_DEBUG = locLevelName("DEBUG");
>      private static String LOC_INFO  = locLevelName("INFO");
>      private static String LOC_WARN  = locLevelName("WARN");
>      private static String LOC_ERROR = locLevelName("ERROR");
>
>      @Override
>      public String convert(ILoggingEvent event) {
>          Level level = event.getLevel();
>
>          String locLevelName;
>
>          switch (level.toString()) {
>              case "TRACE":
>                  locLevelName = LOC_TRACE;
>                  break;
>              case "DEBUG":
>                  locLevelName = LOC_DEBUG;
>                  break;
>              case "INFO":
>                  locLevelName = LOC_INFO;
>                  break;
>              case "WARN":
>                  locLevelName = LOC_WARN;
>                  break;
>              case "ERROR":
>                  locLevelName = LOC_ERROR;
>                  break;
>              default:
>                  locLevelName = LOC_DEBUG;
>          }
>
>          return locLevelName;
>      }
>
>      private static String locLevelName(String levelName) {
>          IMessageConveyor mc = new MessageConveyor(Locale.getDefault());
>
>          Enum<Levels>  key;
>
>          switch (levelName) {
>              case "TRACE":
>                  key = Levels.TRACE;
>                  break;
>              case "DEBUG":
>                  key = Levels.DEBUG;
>                  break;
>              case "INFO":
>                  key = Levels.INFO;
>                  break;
>              case "WARN":
>                  key = Levels.WARN;
>                  break;
>              case "ERROR":
>                  key = Levels.ERROR;
>                  break;
>              default:
>                  key = Levels.DEBUG;
>          }
>
>          return mc.getMessage(key);
>      }
>
> }
>
> Thanks to the static attributes I manged to reduce the number of
> invocations to the method MakePropertyResourceBoundle of CAL10N when
> using the custom converters for localizing level names.
> I also used this idea to implement a custom converter that localize
> the string "Caller+".
>
> Running my application using this new converter I obtain the same
> performances in term of execution time I get using logback with
> modified Level and CallerDataConverter classes.
>
>
> Cheers,
> Dario




More information about the Logback-user mailing list