最近在作項目中須要用到日誌,原本選取的是Log4j,最後通過對比以後仍是發現LogBack在性能上比Log4j有優點。至於有什麼好處,請參考下面這篇文章。php
-
<span style="font-family:Comic Sans MS;font-size:18px;">
<dependency>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-api
</artifactId>
-
<version>1.7.12
</version>
-
</dependency>
-
<dependency>
-
<groupId>ch.qos.logback
</groupId>
-
<artifactId>logback-classic
</artifactId>
-
<version>1.1.3
</version>
-
<scope>compile
</scope>
-
<exclusions>
-
<exclusion>
-
<artifactId>slf4j-api
</artifactId>
-
<groupId>org.slf4j
</groupId>
-
</exclusion>
-
</exclusions>
-
</dependency>
-
-
<dependency>
-
<groupId>ch.qos.logback
</groupId>
-
<artifactId>logback-core
</artifactId>
-
<version>1.1.3
</version>
-
<exclusions>
-
<exclusion>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-api
</artifactId>
-
</exclusion>
-
</exclusions>
-
<scope>compile
</scope>
-
</dependency>
-
-
<dependency>
-
<groupId>ch.qos.logback
</groupId>
-
<artifactId>logback-access
</artifactId>
-
<version>1.1.3
</version>
-
<exclusions>
-
<exclusion>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-api
</artifactId>
-
</exclusion>
-
</exclusions>
-
<scope>compile
</scope>
-
</dependency>
</span>
-
<span style="font-family:Comic Sans MS;font-size:18px;">
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation=
"http://java.sun.com/xml/ns/javaee
-
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-
-
-
-
-
<!-- logback-begin -->
-
<context-param>
-
<param-name>logbackConfigLocation
</param-name>
-
<param-value> classpath:logback.xml
</param-value>
-
</context-param>
-
<listener>
-
<listener-class>com.util.LogbackConfigListener
</listener-class>
-
</listener>
-
<!-- logback-end -->
-
-
-
<filter>
-
<filter-name>encodingFilter
</filter-name>
-
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
-
<init-param>
-
<param-name>encoding
</param-name>
-
<param-value>UTF-8
</param-value>
-
</init-param>
-
<init-param>
-
<param-name>forceEncoding
</param-name>
-
<param-value>true
</param-value>
-
</init-param>
-
</filter>
-
<filter-mapping>
-
<filter-name>encodingFilter
</filter-name>
-
<url-pattern>/*
</url-pattern>
-
</filter-mapping>
-
-
<servlet>
-
<servlet-name>springMVC
</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation
</param-name>
-
<param-value> classpath:springMVC-servlet.xml
</param-value>
-
</init-param>
-
<load-on-startup>1
</load-on-startup>
-
</servlet>
-
<!-- 這裏必定要是/根據Servlet規範來的 -->
-
<servlet-mapping>
-
<servlet-name>springMVC
</servlet-name>
-
<url-pattern>/
</url-pattern>
-
</servlet-mapping>
-
-
</web-app>
</span>
-
<span style=
"font-family:Comic Sans MS;font-size:18px;">
package com.util;
-
-
import javax.servlet.ServletContextEvent;
-
import javax.servlet.ServletContextListener;
-
-
public
class LogbackConfigListener implements ServletContextListener {
-
-
public void contextInitialized(ServletContextEvent event) {
-
LogbackWebConfigurer.initLogging(event.getServletContext());
-
}
-
-
public void contextDestroyed(ServletContextEvent event) {
-
LogbackWebConfigurer.shutdownLogging(event.getServletContext());
-
}
-
}
-
</span>
-
<span style="font-family:Comic Sans MS;font-size:18px;">package com.util;
-
-
import java.io.File;
-
import java.io.FileNotFoundException;
-
import java.net.URL;
-
-
import org.slf4j.LoggerFactory;
-
import org.springframework.util.ResourceUtils;
-
import org.springframework.util.SystemPropertyUtils;
-
-
import ch.qos.logback.classic.LoggerContext;
-
import ch.qos.logback.classic.joran.JoranConfigurator;
-
import ch.qos.logback.core.joran.spi.JoranException;
-
-
public abstract class LogbackConfigurer {
-
-
/** Pseudo URL prefix for loading from the class path: "classpath:" */
-
public static final String CLASSPATH_URL_PREFIX = "classpath:";
-
-
/** Extension that indicates a logback XML config file: ".xml" */
-
public static final String XML_FILE_EXTENSION = ".xml";
-
-
private static LoggerContext lc = (LoggerContext) LoggerFactory
-
.getILoggerFactory();
-
private static JoranConfigurator configurator = new JoranConfigurator();
-
-
/**
-
* Initialize logback from the given file location, with no config file
-
* refreshing. Assumes an XML file in case of a ".xml" file extension, and a
-
* properties file otherwise.
-
*
-
* @param location
-
* the location of the config file: either a "classpath:"
-
* location (e.g. "classpath:mylogback.properties"), an absolute
-
* file URL (e.g.
-
* "file:C:/logback.properties), or a plain absolute path in the file system (e.g. "
-
* C:/logback.properties")
-
* @throws FileNotFoundException
-
* if the location specifies an invalid file path
-
*/
-
public static void initLogging(String location)
-
throws FileNotFoundException {
-
String resolvedLocation = SystemPropertyUtils
-
.resolvePlaceholders(location);
-
URL url = ResourceUtils.getURL(resolvedLocation);
-
if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
-
// DOMConfigurator.configure(url);
-
configurator.setContext(lc);
-
lc.reset();
-
try {
-
configurator.doConfigure(url);
-
} catch (JoranException ex) {
-
throw new FileNotFoundException(url.getPath());
-
}
-
lc.start();
-
}
-
// else {
-
// PropertyConfigurator.configure(url);
-
// }
-
}
-
-
/**
-
* Shut down logback, properly releasing all file locks.
-
*
<p>
-
* This isn't strictly necessary, but recommended for shutting down logback
-
* in a scenario where the host VM stays alive (for example, when shutting
-
* down an application in a J2EE environment).
-
*/
-
public static void shutdownLogging() {
-
lc.stop();
-
}
-
-
/**
-
* Set the specified system property to the current working directory.
-
*
<p>
-
* This can be used e.g. for test environments, for applications that
-
* leverage logbackWebConfigurer's "webAppRootKey" support in a web
-
* environment.
-
*
-
* @param key
-
* system property key to use, as expected in logback
-
* configuration (for example: "demo.root", used as
-
* "${demo.root}/WEB-INF/demo.log")
-
* @see org.springframework.web.util.logbackWebConfigurer
-
*/
-
public static void setWorkingDirSystemProperty(String key) {
-
System.setProperty(key, new File("").getAbsolutePath());
-
}
-
-
}
-
</span>
-
<span style=
"font-family:Comic Sans MS;font-size:18px;">
package com.util;
-
-
import java.io.FileNotFoundException;
-
-
import javax.servlet.ServletContext;
-
-
import org.springframework.util.ResourceUtils;
-
import org.springframework.util.SystemPropertyUtils;
-
import org.springframework.web.util.WebUtils;
-
-
public
abstract
class LogbackWebConfigurer {
-
-
/** Parameter specifying the location of the logback config file */
-
public
static
final String CONFIG_LOCATION_PARAM =
"logbackConfigLocation";
-
-
/**
-
* Parameter specifying the refresh interval for checking the logback config
-
* file
-
*/
-
public
static
final String REFRESH_INTERVAL_PARAM =
"logbackRefreshInterval";
-
-
/** Parameter specifying whether to expose the web app root system property */
-
public
static
final String EXPOSE_WEB_APP_ROOT_PARAM =
"logbackExposeWebAppRoot";
-
-
/**
-
* Initialize logback, including setting the web app root system property.
-
*
-
* @param servletContext
-
* the current ServletContext
-
* @see WebUtils#setWebAppRootSystemProperty
-
*/
-
public static void initLogging(ServletContext servletContext) {
-
// Expose the web app root system property.
-
if (exposeWebAppRoot(servletContext)) {
-
WebUtils.setWebAppRootSystemProperty(servletContext);
-
}
-
-
// Only perform custom logback initialization in case of a config file.
-
String location = servletContext
-
.getInitParameter(CONFIG_LOCATION_PARAM);
-
if (location !=
null) {
-
// Perform actual logback initialization; else rely on logback's
-
// default initialization.
-
try {
-
// Return a URL (e.g. "classpath:" or "file:") as-is;
-
// consider a plain file path as relative to the web application
-
// root directory.
-
if (!ResourceUtils.isUrl(location)) {
-
// Resolve system property placeholders before resolving
-
// real path.
-
location = SystemPropertyUtils
-
.resolvePlaceholders(location);
-
location = WebUtils.getRealPath(servletContext, location);
-
}
-
-
// Write log message to server log.
-
servletContext.log(
"Initializing logback from [" + location
-
+
"]");
-
-
// Initialize without refresh check, i.e. without logback's
-
// watchdog thread.
-
LogbackConfigurer.initLogging(location);
-
-
}
catch (FileNotFoundException ex) {
-
throw
new IllegalArgumentException(
-
"Invalid 'logbackConfigLocation' parameter: "
-
+ ex.getMessage());
-
}
-
}
-
}
-
-
/**
-
* Shut down logback, properly releasing all file locks and resetting the
-
* web app root system property.
-
*
-
* @param servletContext
-
* the current ServletContext
-
* @see WebUtils#removeWebAppRootSystemProperty
-
*/
-
public static void shutdownLogging(ServletContext servletContext) {
-
servletContext.log(
"Shutting down logback");
-
try {
-
LogbackConfigurer.shutdownLogging();
-
}
finally {
-
// Remove the web app root system property.
-
if (exposeWebAppRoot(servletContext)) {
-
WebUtils.removeWebAppRootSystemProperty(servletContext);
-
}
-
}
-
}
-
-
/**
-
* Return whether to expose the web app root system property, checking the
-
* corresponding ServletContext init parameter.
-
*
-
* @see #EXPOSE_WEB_APP_ROOT_PARAM
-
*/
-
private static boolean exposeWebAppRoot(ServletContext servletContext) {
-
String exposeWebAppRootParam = servletContext
-
.getInitParameter(EXPOSE_WEB_APP_ROOT_PARAM);
-
return (exposeWebAppRootParam ==
null || Boolean
-
.valueOf(exposeWebAppRootParam));
-
}
-
-
}
-
</span>
-
<span style="font-family:Comic Sans MS;font-size:18px;">
<?xml version="1.0" encoding="UTF-8"?>
-
<!-- ROOT 節點 -->
-
<!-- 屬性描述 scan:性設置爲true時,配置文件若是發生改變,將會被從新加載,默認值爲true scanPeriod:設置監測配置文件是否有修改的時間間隔,若是沒有給出時間單位,默認單位是毫秒。當scan爲true時,此屬性生效。默認的時間間隔爲1分鐘。
-
debug:當此屬性設置爲true時,將打印出logback內部日誌信息,實時查看logback運行狀態。默認值爲false。 -->
-
<configuration scan="true" scanPeriod="60 seconds" debug="false">
-
<!-- 定義日誌文件 輸入位置,注意此處的/ -->
-
<property name="log_dir" value="E:/logs" />
-
<!-- 日誌最大的歷史 60天 -->
-
<property name="maxHistory" value="60">
</property>
-
-
-
<!-- 控制檯輸出日誌 -->
-
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
-
%msg%n
</pattern>
-
</encoder>
-
-
</appender>
-
-
-
<!-- 出錯日誌 appender -->
-
<appender name="ERROR"
-
class=
"ch.qos.logback.core.rolling.RollingFileAppender">
-
<!-- 在多數的Log工具中,級別是能夠傳遞,例如若是指定了日誌輸出級別爲DEBUG, 那麼INFO、ERROR級別的log也會出如今日誌文件。這種默認給程序的調試帶來了不少的麻煩
-
經過配置Filter 來嚴格控制日誌輸入級別 <filter class="ch.qos.logback.classic.filter.LevelFilter">
-
<level>ERROR/level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch>
-
</filter> -->
-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
<!-- 按天回滾 daily -->
-
<fileNamePattern>${log_dir}/error-log-%d{yyyy-MM-dd}.log
-
</fileNamePattern>
-
<!-- 日誌最大的歷史 60天 -->
-
<maxHistory>${maxHistory}
</maxHistory>
-
</rollingPolicy>
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
-
%msg%n
</pattern>
-
</encoder>
-
</appender>
-
-
<!-- INFO 日誌 appender -->
-
<appender name="INFO"
-
class=
"ch.qos.logback.core.rolling.RollingFileAppender">
-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
<!-- 按天回滾 daily -->
-
<fileNamePattern>${log_dir}/info-log-%d{yyyy-MM-dd}.log
-
</fileNamePattern>
-
<!-- 日誌最大的歷史 60天 -->
-
<maxHistory>${maxHistory}
</maxHistory>
-
</rollingPolicy>
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
-
%msg%n
</pattern>
-
</encoder>
-
</appender>
-
-
-
<!-- 訪問日誌 appender -->
-
<appender name="ACCESS"
-
class=
"ch.qos.logback.core.rolling.RollingFileAppender">
-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
<!-- 按天回滾 daily -->
-
<fileNamePattern>${log_dir}/access-log-%d{yyyy-MM-dd}.log
-
</fileNamePattern>
-
<!-- 日誌最大的歷史 60天 -->
-
<maxHistory>${maxHistory}
</maxHistory>
-
</rollingPolicy>
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
-
%msg%n
</pattern>
-
</encoder>
-
</appender>
-
-
<!-- 系統用戶操做日誌 appender -->
-
<appender name="SYS-USER"
-
class=
"ch.qos.logback.core.rolling.RollingFileAppender">
-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
<!-- 按天回滾 daily -->
-
<fileNamePattern>${log_dir}/sys_user-log-%d{yyyy-MM-dd}.log
-
</fileNamePattern>
-
<!-- 日誌最大的歷史 60天 -->
-
<maxHistory>${maxHistory}
</maxHistory>
-
</rollingPolicy>
-
<encoder>
-
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
-
%msg%n
</pattern>
-
</encoder>
-
</appender>
-
-
-
<!-- 打印SQL輸出 -->
-
<logger name="java.sql.Connection" level="DEBUG" />
-
<logger name="java.sql.Statement" level="DEBUG" />
-
<logger name="java.sql.PreparedStatement" level="DEBUG" />
-
-
-
-
<!--error錯誤日誌 additivity="false"表示不向上傳遞 -->
-
<!-- <logger name="com.test" level="error" > -->
-
<!-- <appender-ref ref="ERROR" /> -->
-
<!-- </logger> -->
-
<!--info日誌 -->
-
<logger name="com.test" level="info" additivity="false">
-
<appender-ref ref="INFO" />
-
</logger>
-
<!--訪問日誌 -->
-
<!-- <logger name="com.test" level="info" additivity="false"> -->
-
<!-- <appender-ref ref="ACCESS" /> -->
-
<!-- </logger> -->
-
<!--系統用戶操做日誌 -->
-
<!-- <logger name="com.test" level="info" additivity="false"> -->
-
<!-- <appender-ref ref="SYS-USER" /> -->
-
<!-- </logger> -->
-
-
<root>
-
<level value="INFO" />
-
<appender-ref ref="stdout" />
-
</root>
-
</configuration>
</span>