[slf4j-user] slf4j logger -> Servlet container log : How to?
Jacob Kjome
hoju at visi.com
Wed May 12 06:27:48 CEST 2010
See comments inline below....
On 5/11/2010 10:42 AM, Thorbjoern Ravn Andersen wrote:
> Den 11/05/10 18.32, Jacob Kjome skrev:
>> In web.xml...
>>
>> <listener>
>> <listener-class>
>> org.apache.log4j.servlet.ServletContextLogAppenderListener
>> </listener-class>
>> </listener>
>>
>> in log4j.xml...
>>
>> <appender name="ServletContext"
>> class="org.apache.log4j.servlet.ServletContextLogAppender">
>> <param name="servletContextPath" value="/mycontext"/>
>> <param name="Threshold" value="debug"/>
>> <layout class="org.apache.log4j.PatternLayout">
>> <param name="ConversionPattern" value="[%-5p][%-8.8t]: %39.39c %x- %m"/>
>> </layout>
>> </appender>
>>
>> Notice the conversion pattern ends with "%m" instead of "%m%n". This
>> is because when the message goes through the servlet context logging
>> mechanism, the latter adds the newline, so adding it to the conversion
>> pattern above would result in 2 newlines, which is why we leave it out.
> Ok. So if I understand this correctly, your approach uses a servlet
> listener to register all contexts keyed by the last part of their
> context path, and you tell the appender in question which ServletContext
> to retrieve and then forward to that logger.
>
Each webapp that defines the servlet context listener can participate in servlet
context logging. However, how many contexts the the servlet context appender
handles depends on how you set up classloading. If you deploy the
log4j-sandbox.jar, along with log4j.jar, in WEB-INF/lib and use child-first
classloading, then the servlet context appender will only ever deal with a single
context; the current one. On the other hand, if log4j-sandbox.jar and log4j.jar
are shared, e.g., at the container level, then it would handle more context paths.
> Will this also work with multiple web apps using slf4j, or will they go
> to the same appender?
>
It's been a while since I used it or studied it closely, but it should handle as
many contexts as you give it. Note that I've never tried it via SLF4J, only
directly using Log4j.
BTW, the way I've used this in the past is either to place the jars in WEB-INF/lib
and use child-first classloading or use a logger repository selector. I always
manually configure Log4j after letting the servlet context listener register the
context to the appender. Then in Tomcat, for instance, I place log4j.jar in a
shared lib location along with log4j.properties, meant for configuring the server
to use Log4j, which looks somewhat like below. So, the server has its own log4j
configuration while each webapp has its own configuration. And when my app either
logs directly to the servlet context or logs via the servlet context log appender,
I have defined exactly where the output goes; to
"${catalina.base}/logs/localhost_mycontext.log".
Oh, and I had to follow these instructions to get Tomcat to use Log4j...
http://tomcat.apache.org/tomcat-6.0-doc/logging.html#log4j
Jake
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-5p[%-8.8t]: %39.39c %-6r - %m%n
log4j.appender.LOCALHOST=org.apache.log4j.RollingFileAppender
log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.log
log4j.appender.LOCALHOST.MaxFileSize=1000KB
log4j.appender.LOCALHOST.MaxBackupIndex=1
log4j.appender.LOCALHOST.layout=org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern=%-5p[%-8.8t]: %39.39c %-6r - %m%n
log4j.appender.MYCONTEXT=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MYCONTEXT.File=${catalina.base}/logs/localhost_mycontext.log
log4j.appender.MYCONTEXT.DatePattern='.'yyyy-MM-dd
log4j.appender.MYCONTEXT.layout=org.apache.log4j.PatternLayout
log4j.appender.MYCONTEXT.layout.ConversionPattern=%c{1} %-6r - %m%n
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/mycontext]=DEBUG,
MYCONTEXT
log4j.additivity.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/mycontext]=false
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO,
LOCALHOST
log4j.additivity.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=false
log4j.logger.org.apache.coyote=INFO
log4j.logger.org.apache.catalina=INFO
log4j.rootLogger=WARN, A1
More information about the slf4j-user
mailing list