Logback with Tomcat and multiple web applications

I've been struggling with this in Log4J, and am wondering if it's possible in Logback. If I have tomcat, with several web applications, can I have one instance of the logback jars in Tomcat/lib, and one configuration file, but distinguish between applications via variable substitution and the pattern? We are logging to SYSLOG, so I would like the pattern to include the application name. I looked through the manual, and see you can use variable substitution for specifying different files, but does substitution work in the pattern field? Can the ContextJNDISelector use a configuration file outside of the war? Ideally I would have the logging configuration file in tomcat/conf or somewhere, and have all applications use the same one. Thanks for any help!

If I understand correctly, you would like to have the syslog output somehow include the current application name. Is that all? More below. Ryan Cornia wrote:
I've been struggling with this in Log4J, and am wondering if it's possible in Logback.
If I have tomcat, with several web applications, can I have one instance of the logback jars in Tomcat/lib, and one configuration file, but distinguish between applications via variable substitution and the pattern?
We are logging to SYSLOG, so I would like the pattern to include the application name.
I looked through the manual, and see you can use variable substitution for specifying different files, but does substitution work in the pattern field?
Substitution variables work within the pattern field. However, I don't think that's what you want. You probably want to specify a conversion word whose value dynamically changes to the name of your application. Assuming you set the MDC key "applicationName" to the name of the application, then the following config file might be what you are looking for. <configuration> <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender"> <SyslogHost>YOUR HOST HERE</SyslogHost> <Facility>USER</Facility> <SuffixPattern>%mdc{applicationName} %msg</SuffixPattern> </appender> <root level="debug"> <appender-ref ref="SYSLOG" /> </root> </configuration> You might also be interested in SiftingAppender which is a whole world on its own. See http://logback.qos.ch/manual/appenders.html#SiftingAppender
Can the ContextJNDISelector use a configuration file outside of the war?
Sure.
Ideally I would have the logging configuration file in tomcat/conf or somewhere, and have all applications use the same one.
Thanks for any help!
-- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch

Thanks for the suggestions, this is exactly what I want to do. As long as I can point ContextJNDISelector to an external config file, it sounds perfect. I assume I just put in the full path to get at a resource outside of the war? I have tried setting MDC with log4j in the first Listener for a web app. This, unfortunately, has led to unpredictable results. Sometimes it comes through, sometimes not. We are running on a Tomcat 6 cluster. I'll give Logback a try, and hope the dropping MDC is a log4j issue. I had suspected maybe it was the way tomcat created threads that the parent MDC was sometimes not propagating. I assume setting the MDC in the first <listener> in web.xml should make it globally available, and applied to all logging statements? Thanks! On 2/11/09 10:31 AM, "Ceki Gulcu" <ceki@qos.ch> wrote:
If I understand correctly, you would like to have the syslog output somehow include the current application name. Is that all?
More below.
Ryan Cornia wrote:
I've been struggling with this in Log4J, and am wondering if it's possible in Logback.
If I have tomcat, with several web applications, can I have one instance of the logback jars in Tomcat/lib, and one configuration file, but distinguish between applications via variable substitution and the pattern?
We are logging to SYSLOG, so I would like the pattern to include the application name.
I looked through the manual, and see you can use variable substitution for specifying different files, but does substitution work in the pattern field?
Substitution variables work within the pattern field. However, I don't think that's what you want. You probably want to specify a conversion word whose value dynamically changes to the name of your application. Assuming you set the MDC key "applicationName" to the name of the application, then the following config file might be what you are looking for.
<configuration> <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender"> <SyslogHost>YOUR HOST HERE</SyslogHost> <Facility>USER</Facility> <SuffixPattern>%mdc{applicationName} %msg</SuffixPattern> </appender>
<root level="debug"> <appender-ref ref="SYSLOG" /> </root> </configuration>
You might also be interested in SiftingAppender which is a whole world on its own. See http://logback.qos.ch/manual/appenders.html#SiftingAppender
Can the ContextJNDISelector use a configuration file outside of the war?
Sure.
Ideally I would have the logging configuration file in tomcat/conf or somewhere, and have all applications use the same one.
Thanks for any help!

Hi Ryan, I didn't quite understand why you needed ContextJNDISelector. Anyway, here is documentation on MDC http://logback.qos.ch/manual/mdc.html Here is a salient paragraph from that page: The MDC class contains only static methods. It lets the developer place information in a diagnostic context that can be subsequently retrieved by certain logback components. The MDC manages contextual information on a per thread basis. *A child thread automatically inherits a copy of the mapped diagnostic context of its parent* HTH, Ryan Cornia wrote:
Thanks for the suggestions, this is exactly what I want to do. As long as I can point ContextJNDISelector to an external config file, it sounds perfect. I assume I just put in the full path to get at a resource outside of the war?
I have tried setting MDC with log4j in the first Listener for a web app. This, unfortunately, has led to unpredictable results. Sometimes it comes through, sometimes not. We are running on a Tomcat 6 cluster. I'll give Logback a try, and hope the dropping MDC is a log4j issue. I had suspected maybe it was the way tomcat created threads that the parent MDC was sometimes not propagating.
I assume setting the MDC in the first <listener> in web.xml should make it globally available, and applied to all logging statements?
Thanks!
-- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch

I didn't quite understand why you needed ContextJNDISelector.
I would like to have all web apps use the same configuration file that is kept outside of the war. Maybe ContextJNDISelector isn't what is needed? Would I use this technique (http://logback.qos.ch/faq.html#sharedConfiguration) in the listener for the web application? Instead of : context.putProperty("application-name", NAME_OF_CURRENT_APPLICATION); I would just set the MDC?

Ryan Cornia skrev:
I didn't quite understand why you needed ContextJNDISelector.
I would like to have all web apps use the same configuration file that is kept outside of the war. Maybe ContextJNDISelector isn't what is needed?
Could you use the include functionality to get a file in the filesystem? http://logback.qos.ch/manual/joran.html#fileInclusion I cannot see right now, if the filename string can contain ${foo.bar} replacements to put in variable information? -- Thorbjørn Ravn Andersen "...plus... Tubular Bells!"

Within the include element, the filename attribute can contain variables. See line 149, 154, and 159 in getInputStream(InterpretationContext, Attributes) method of in IncludeAction class: http://logback.qos.ch/xref/ch/qos/logback/core/joran/action/IncludeAction.ht... Thorbjørn Ravn Andersen wrote:
Ryan Cornia skrev:
I didn't quite understand why you needed ContextJNDISelector.
I would like to have all web apps use the same configuration file that is kept outside of the war. Maybe ContextJNDISelector isn't what is needed?
Could you use the include functionality to get a file in the filesystem?
http://logback.qos.ch/manual/joran.html#fileInclusion
I cannot see right now, if the filename string can contain ${foo.bar} replacements to put in variable information?
-- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch
participants (3)
-
Ceki Gulcu
-
Ryan Cornia
-
Thorbjørn Ravn Andersen