Quick StartIf you quickly want to configure Tomcat to use Slf4J and Logback, just download latest package available from SourceForge and explode zip file directly into $CATALINA_HOME. Beware! - doing so will replace server.xml with default version and logging valve. The latest version (Tomcat 6.0.44, 7.0.62, or 8.0.24, Slf4j 1.7.12, Logback 1.1.3) may be downloaded from SourceForge. Previous versions are available as well. All future versions will be released on github releases. The latest versions (Tomcat 7.0.65, 8.0.29, 9.0.0.M1, Slf4j 1.7.12 or 1.7.13, Logback 1.1.3) may be downloaded from releases Some rather old versions are not predefined for direct exploding into $CATALINA_HOME. The following directions are for manual setup. After downloading copy (from the archive):
Delete $CATALINA_HOME/conf/logging.properties. This will turn offjava.util.logging completely. conf/logback.xml tries to reflect original Tomcat logging configuration. Feel free to change it. When using your own preconfigured server.xml, the following will need applied. Add: <Valve className="ch.qos.logback.access.tomcat.LogbackValve" quiet="true" filename="${catalina.home}/conf/logback-access-localhost.xml" /> to $CATALINA_HOME/conf/server.xml. Remove: <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> from $CATALINA_HOME/conf/server.xml. Final step: run $CATALINA_HOME/bin/startup.sh (or startup.bat). Voila! Site PageSite page is located here DetailsThis project allows using SLF4J and Logback in Apache Tomcat absolutely without the need for commons- logging, log4j, and java.util.logging. This project's main and only goal is to allow the following:
Using only Mavens pom.xml file, proper source JARs are downloaded from maven repository and unpacked. Then all classes are refactored underorg.apache.juli.logging package/subpackages and then compiled. To allow web applications to use their own slf4j-api and logback-classic, classes used by Tomcat (particularly jcl-over-slf4j) must go into different, non-standard packages. According to Tomcat Documentation web application looks up classes in their WEB-INF/classes directory and WEB-INF/lib/*.jar files before looking them in$CATALINA_HOME/lib, but after looking them in system class loader. So Tomcat needs only to have tomcat-juli replaced with versions of jcl-over-slf4j, slf4j-api,logback-core, and logback-classic refactored into different packages. Finally, in order to keep the classpath clean, I've chosen the method of selecting Logback's configuration file using juli-logback.configurationFile system property. It is renamed in source files during refactoring phase. Leaving standard logback.configurationFileproperty would cause selecting this file in all web applications despite of having dedicated, classpath-based logback.xml configuration files. There are four JARs involved in the process transformed into org.apache.juli.loggingexactly the same way as commons-logging is transformed in Tomcat's build process. It is eventually compiled into tomcat-juli.jar
Prior builds of this project contained 4 separate jars where tomcat-juli noted these in the manifest in order to avoid further touching of tomcat configuration files for security purposes. Current build results in a single tomcat-juli file and thus no longer requires this. InstallationLaunching the build requires Maven install - everything will be downloaded upon build. Type: mvn clean install If you want to do a build for different Tomcat versions (Tomcat 7 and above), just append-Dtomcat.version=x.x.x option. If you want to build for Tomcat 6 (only Logback versions below 1.0.0!), type: mvn clean install -Ptomcat6 -P-tomcat7+ And move tomcat-juli JAR from target directory to $CATALINA_HOME/bin directory. More detailed instruction:
After changing versions (e.g. for Tomcat), run mvn clean install. Running Tomcat now will use default (very verbose) configuration of Logback. To change Logback's configuration, run Tomcat with the following system variable (using your favorite method of setting such variables - in catalina.sh, setenv.sh or other): -Djuli-logback.configurationFile=file:<logback.xml location> ConfigurationNow you can configure whatever logging technology you want for your web applications. I recommend SLF4J and Logback because from now on, it will not collide with Tomcat's logging configuration. While configuring Tomcat's logging, keep in mind that you have to use renamed packages in logback.xml config file, e.g.: <configuration> <appender name="CONSOLE" class="org.apache.juli.logging.ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} %-5level {%thread} [%logger{20}] : %msg%n</pattern> </encoder> </appender> <logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]" level="INFO" additivity="false"> <appender-ref ref="FILE-LOCALHOST" /> </logger> <root level="INFO"> <appender-ref ref="CONSOLE" /> </root> </configuration> Configuration of logback-access doesn't require renamed packages, as the required JARs are loaded from common class loader. Sample logback.xml reflecting the configuration from standard$CATALINA_HOME/conf/logging.properties can be found in conf/logback.xml fromarchive. Tomcat CustomizationTomcat 6.0.x 7.0.x 8.0.xAfter unpacking apache-tomcat-6.0.x.tgz, apache-tomcat-7.0.x.tgz, or apache-tomcat-8.0.x.tgz, one can run Tomcat by executing $CATALINA_HOME/bin/startup.sh. This will cause running Tomcat with standard java.util.logging enabled. The standard commandline is: "java" \ -Djava.util.logging.config.file="$CATALINA_HOME/conf/logging.properties" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs="$CATALINA_HOME/endorsed" -classpath "$CATALINA_HOME\bin\bootstrap.jar" -Dcatalina.base="$CATALINA_HOME" -Dcatalina.home="$CATALINA_HOME" -Djava.io.tmpdir="$CATALINA_HOME" org.apache.catalina.startup.Bootstrap start Deleting $CATALINA_HOME/conf/logging.properties will replace -Djava.util.logging.config.file with -Dnop. Finally we must configure our tomcat-slf4j-logback integration:
Now Tomcat's internal logging goes through org.apache.juli.logging.org.slf4j andorg.apache.juli.logging.ch.qos.logback to appenders configured in$CATALINA_HOME/conf/logback.xml (or whatever file you set juli-logback.configurationFile variable to). The final step is to configure logback-access. Now we don't have to deal with package manipulation. Just add: <Valve className="ch.qos.logback.access.tomcat.LogbackValve" quiet="true" filename="${catalina.home}/conf/logback-access-localhost.xml" /> to $CATALINA_HOME/conf/server.xml, place properly configured logback-access-localhost.xml on $CATALINA_HOME/conf and place logback-core and logback-access JARs into $CATALINA_HOME/lib. This won't cause problems with individual WARs' slf4j+logback configuration, because logback.xml is read by logback-classicwhich is recommended to reside in WEB-INF/lib. The only additional benefit is that WARs will see logback-core through common class loader. Using Tomcat in Eclipse
That's all. While creating server runtime instance, eclipse generates VM arguments using absolute paths (no variables), so just copy the Tomcat home path and add -Djuli-logback.configurationFile argument. Remember - Tomcat installation must be configured according to Quick Start. (责任编辑:IT) |