[logback-user] Localized log level names

Dario Campagna campagna at esteco.com
Tue Jun 5 09:20:56 CEST 2012


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