[logback-user] JMXConfigurator OName

Anders Engström epirealer at gmail.com
Wed Aug 13 17:06:59 CEST 2008


Hi!

It would be really nice if the jmxConfigurator action (in logback.xml) could
be configured with an explicit ObjectName. I'm in the same situation as the
original poster (we're running multiple instances of the same
web-application in the same servlet container). 

Something like:


<jmxConfigurator objectNameProperty="webapp=someWebApp" />


This would yield an ObjectName as:
'ch.qos.logback.classic:Name=default,webapp=someWebApp'.

This would allow each web-app to have it's own mbean instance registered
with the server.

My choice is either to read up on the Joran configuration API and create a
custom Action that will do the kind of MBean registration that I need, or to
do some voodoo in our custom Logback loader. At the moment I'm doing some
funky stuff in our loader:


public class ExtensionAwareLogbackConfigurationLoader implements
ServletContextListener {
    
    public static class NamedContext implements Context {
        private Context target;
        private String name;

        public NamedContext(Context target, String name) {
            this.target = target;
            this.name = name;
        }

        public StatusManager getStatusManager() {return
target.getStatusManager();}
        public Object getObject(String s) {return target.getObject(s);}
        public void putObject(String s, Object o) {target.putObject(s, o);}
        public String getProperty(String s) {return target.getProperty(s);}
        public void putProperty(String s, String s1) {target.putProperty(s,
s1);}
        public String getName() {return this.name;}
        public void setName(String s) {target.setName(s);}
    }

    public void contextInitialized(ServletContextEvent event) {
        ApplicationExtension ext = ApplicationExtension.get();
        if(ext != null){
            File configurationFile = ext.getRelativePath("log/logback.xml");
            if(configurationFile.exists()){
                try {
                    event.getServletContext().log("["+
ExtensionAwareLogbackConfigurationLoader.class.getSimpleName()+"]: Using
external logback.xml: "+configurationFile.getAbsolutePath());
                    LoggerContext loggerContext = (LoggerContext)
LoggerFactory.getILoggerFactory();
                    loggerContext.putProperty("output.root",
ext.getRelativePath("log/out").getAbsolutePath());
                    JoranConfigurator configurator = new
JoranConfigurator();
                    configurator.setContext(new NamedContext(loggerContext,
ext.getNamespace()));
                    loggerContext.shutdownAndReset();
                    configurator.doConfigure(configurationFile);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public void contextDestroyed(ServletContextEvent event) {
        LoggerContext loggerContext =
(LoggerContext)LoggerFactory.getILoggerFactory();
        loggerContext.shutdownAndReset();
    }
}

I'm basically setting a custom name for the LoggerContext based on the
current web-application context name.

(The ApplicationExtension is an abstraction of a directory that contains
resources per web-application == customer).

Best Regards //Anders


Ceki Gulcu-2 wrote:
> 
> 
> Hello Ellis,
> 
> The context name cannot be set in logback.xml. It is the job of the
> context 
> selector to set the logging context's name. See 
> http://logback.qos.ch/manual/contextSelector.html for more details.
> 
> Please let us know if you need further information,
> 
> Pritchard, Ellis - Ealing wrote:
>> Hi,
>> 
>> I've currently got multiple logback-enabled servlets running under the
>> same JVM, each in separate web contexts, 
>  > and each with their own logback.xml and slf4j/logback jars in WEB-INF.
>> 
>> Whist this works (in terms of logging), the jmxConfigurator MBean seems
>> to only refer to 
>  > one of the contexts, and has the name 'default'. I can't see the
> loggers 
> listed for
>  > the other contexts.
>> 
>> What I'd expected to see is a different MBean listed for each webapp
>> context.
>> 
>> Looking at the jmxConfigurator code, it seems to use the name of the
>> logging context for the JMX name; 
>  > how does this get set? Can it be set in the logback.xml?
> 
>> Ellis.
> -- 
> Ceki Gülcü
> 
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://qos.ch/mailman/listinfo/logback-user
> 
> 

-- 
View this message in context: http://www.nabble.com/JMXConfigurator-OName-tp18566870p18964987.html
Sent from the Logback User mailing list archive at Nabble.com.



More information about the Logback-user mailing list