Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

tcServer Logging with Logback & SLF4J

DZone's Guide to

tcServer Logging with Logback & SLF4J

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

Logback and SLF4J provide better logging for java applications. To configure VMware vFabric tcServer to take advantage of the speed and flexibility of Logback use the following steps as a starting point.

Download the tcServer Developer Edition free: http://www.springsource.com/developer/tcserver
This comes with Spring Insight: http://www.springsource.org/insight an awesome tool for inspecting application performance, see those expensive sql queries, etc.

1. Add JMX options

File: <appname>/bin/setenv.sh

CATALINA_OPTS="-Dcom.sun.management.jmxremote"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.ssl=false"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"

2. Enable JMX

File: <appname>/conf/server.xml

# In the <service> section:

<Connector port="8050"
  handler.list="mx"
  mx.enabled="true"
  mx.httpHost="localhost"
  mx.httpPort="8082"
  protocol="AJP/1.3" />

3. If using JNDI, add JNDI Configuration support for Logback

File: <appname>/bin/setenv.sh

JAVA_OPTS="$JAVA_OPTS -Dlogback.ContextSelector=JNDI"

4. Add Server Logging using Logback

File: <appname>/conf/server.xml

#In the <Hosts> Section:

<Valve className="ch.qos.logback.access.tomcat.LogbackValve" />

5. Create logback-access.xml file in <appname>/conf/ directory

<configuration>
  <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
 
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>combined</pattern>
    </encoder>
  </appender>
 
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>myApp.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>myApp-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
      <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <maxFileSize>5MB</maxFileSize>
      </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    <encoder>
      <pattern>combined</pattern>
    </encoder>
  </appender>
 
  <logger name="com.MYAPP.MYPKG" level="debug" />
  <logger name="org.springframework.web" level="debug" />
  <logger name="org.springframework.beans" level="debug" />
  <logger name="org.springframework.orm" level="debug" />
  <logger name="org.springframework.web" level="debug" />
  <logger name="net.sf.ehcache" level="error" />
  <logger name="org.hibernate.cache" level="error" />
 
  <root level="info">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

6. In the <tomcatdir>/lib directory, add the following jars

By including JCL-over-SLF4J, we handle Apache Commons Logging through SLF4J (JCL = Jakarta Commons Logging)
By including JUL-to-SLF4J, we handle Java Util Logging through SLF4J

  • jcl-over-slf4j-1.6.1.jar
  • jul-to-slf4j-1.6.1.jar
  • logback-access-0.9.29.jar
  • logback-classic-0.9.29.jar
  • logback-core-0.9.29.jar
  • slf4j-api-1.6.1.jar

7. Configure the application for Logging Separation

See: http://logback.qos.ch/manual/loggingSeparation.html

<env-entry>
  <description>JNDI logging context for this app</description>
  <env-entry-name>logback/context-name</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>yourAppHere</env-entry-value>
</env-entry>
 
<env-entry>
  <description>URL for configuring logback context</description>
  <env-entry-name>logback/configuration-resource</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>logback.xml</env-entry-value>
</env-entry>
 
<listener>
  <listener-class>ch.qos.logback.classic.selector.servlet.ContextDetachingSCL</listener-class>
</listener>
 
<filter>
  <filter-name>LoggerContextFilter</filter-name>
  <filter-class>ch.qos.logback.classic.selector.servlet.LoggerContextFilter</filter-class>
</filter>
 
<filter-mapping>
  <filter-name>LoggerContextFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

8. Optional – To add a servlet to view the logback events

File: <application>/src/main/webapp/WEB-INF/web.xml
See: http://logback.qos.ch/manual/configuration.html
Status Messages will be available: http://yourWebapp/lbClassicStatus

	
<servlet>
  <servlet-name>ViewStatusMessages</servlet-name>
  <servlet-class>ch.qos.logback.classic.ViewStatusMessagesServlet</servlet-class>
</servlet>
 
<servlet-mapping>
  <servlet-name>ViewStatusMessages</servlet-name>
  <url-pattern>/lbClassicStatus</url-pattern>
</servlet-mapping>

Summary

Using tcServer with Insight provides us developers with a valuable resource for code inspection at runtime. Logback and SLF4J provides us with fast, flexible logging configuration and the logging separation for our application instances.

From http://gordondickens.com/wordpress/2011/08/16/tcserver-logging-with-logback-slf4j/

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}