[logback-dev] [JIRA] (LOGBACK-1308) Allow LoggerContext subclasses to update newly-created Loggers

QOS.CH (JIRA) noreply-jira at qos.ch
Fri Jun 2 20:59:00 CEST 2017


Richard Sand created LOGBACK-1308:
-------------------------------------

             Summary: Allow LoggerContext subclasses to update newly-created Loggers
                 Key: LOGBACK-1308
                 URL: https://jira.qos.ch/browse/LOGBACK-1308
             Project: logback
          Issue Type: Improvement
          Components: logback-classic
    Affects Versions: 1.3.0, 1.2.4
         Environment: all
            Reporter: Richard Sand
            Assignee: Logback dev list
         Attachments: LoggerContext.patch

I've a use case where I am adapting Logback with a legacy product's proprietary log mechanism. I did this by creating a wrapper class around a Logger, creating custom appenders which wrap the product's logging API, and directly attaching the appenders necessary according to the product's configuration settings.

Now I am trying to enable 3rd-party plugins, which are written for this product, to be able to directly use Logger.getLogger() to log, and not have to even worry about the product's proprietary (and clunky) logging API. But this proves difficult in Logback, because there is no hook to programmatically touch the Logger instances at creation-time. A custom LoggerContext doesn't help because the getLogger() method is final, and the Logger class itself is also final, allowing no extensibility.

The patch I have supplied provides this hook by simply adding a method in LoggerContext that is invoked directly after Logger.createChildByName(), which by default is a no-op but can be overridden by custom contexts.

 

{{ /**}}
{{  * Allows subclasses to perform post-create modifications on newly-created \{@link Logger} instances.}}
{{  * By default, this method does nothing}}
{{  *  }}
{{  * @param childLogger}}
{{  */}}
{{ protected void customizeNewLogger(final Logger childLogger) \{}}
{{ }}}

Inside the getLogger() method, this is invoked directly after creation:

{{    childLogger = logger.getChildByName(childName);}}
{{    if (childLogger == null) \{}}
{{         childLogger = logger.createChildByName(childName);}}
{{{color:#d04437}         customizeNewLogger(childLogger);{color}}}
{{         loggerCache.put(childName, childLogger);}}
{{         incSize();}}
{{    }}}

Thanks for your consideration of this update!

 



--
This message was sent by Atlassian JIRA
(v7.3.1#73012)


More information about the logback-dev mailing list