SpringMVC集成LogBack,相關配置


     最近在作項目中須要用到日誌,原本選取的是Log4j,最後通過對比以後仍是發現LogBack在性能上比Log4j有優點。至於有什麼好處,請參考下面這篇文章。php

從Log4j遷移到LogBack的理由


    下面廢話很少說了,就看一下,如何來把LogBack集成到咱們的web項目中吧。本人前臺用的是SpringMVC。

    jar包配置
   若是要使用LogBack作爲日誌的插件的話,須要的jar包有以下,直接看一下Maven依賴

   
   
   
   
  1. <span style="font-family:Comic Sans MS;font-size:18px;"> <dependency>
  2. <groupId>org.slf4j </groupId>
  3. <artifactId>slf4j-api </artifactId>
  4. <version>1.7.12 </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>ch.qos.logback </groupId>
  8. <artifactId>logback-classic </artifactId>
  9. <version>1.1.3 </version>
  10. <scope>compile </scope>
  11. <exclusions>
  12. <exclusion>
  13. <artifactId>slf4j-api </artifactId>
  14. <groupId>org.slf4j </groupId>
  15. </exclusion>
  16. </exclusions>
  17. </dependency>
  18. <dependency>
  19. <groupId>ch.qos.logback </groupId>
  20. <artifactId>logback-core </artifactId>
  21. <version>1.1.3 </version>
  22. <exclusions>
  23. <exclusion>
  24. <groupId>org.slf4j </groupId>
  25. <artifactId>slf4j-api </artifactId>
  26. </exclusion>
  27. </exclusions>
  28. <scope>compile </scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>ch.qos.logback </groupId>
  32. <artifactId>logback-access </artifactId>
  33. <version>1.1.3 </version>
  34. <exclusions>
  35. <exclusion>
  36. <groupId>org.slf4j </groupId>
  37. <artifactId>slf4j-api </artifactId>
  38. </exclusion>
  39. </exclusions>
  40. <scope>compile </scope>
  41. </dependency> </span>
   
    Web.xml
      在web項目中須要經過web.xml來加載咱們所須要的LogBack.xml具體以下
   
   
   
   
  1. <span style="font-family:Comic Sans MS;font-size:18px;"> <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <!-- logback-begin -->
  7. <context-param>
  8. <param-name>logbackConfigLocation </param-name>
  9. <param-value> classpath:logback.xml </param-value>
  10. </context-param>
  11. <listener>
  12. <listener-class>com.util.LogbackConfigListener </listener-class>
  13. </listener>
  14. <!-- logback-end -->
  15. <filter>
  16. <filter-name>encodingFilter </filter-name>
  17. <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class>
  18. <init-param>
  19. <param-name>encoding </param-name>
  20. <param-value>UTF-8 </param-value>
  21. </init-param>
  22. <init-param>
  23. <param-name>forceEncoding </param-name>
  24. <param-value>true </param-value>
  25. </init-param>
  26. </filter>
  27. <filter-mapping>
  28. <filter-name>encodingFilter </filter-name>
  29. <url-pattern>/* </url-pattern>
  30. </filter-mapping>
  31. <servlet>
  32. <servlet-name>springMVC </servlet-name>
  33. <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
  34. <init-param>
  35. <param-name>contextConfigLocation </param-name>
  36. <param-value> classpath:springMVC-servlet.xml </param-value>
  37. </init-param>
  38. <load-on-startup>1 </load-on-startup>
  39. </servlet>
  40. <!-- 這裏必定要是/根據Servlet規範來的 -->
  41. <servlet-mapping>
  42. <servlet-name>springMVC </servlet-name>
  43. <url-pattern>/ </url-pattern>
  44. </servlet-mapping>
  45. </web-app> </span>

   上面的XML中用到了自定義的監聽器,分別是三個類,以下所示

   LogbackConfigListener類
   
   
   
   
  1. <span style= "font-family:Comic Sans MS;font-size:18px;"> package com.util;
  2. import javax.servlet.ServletContextEvent;
  3. import javax.servlet.ServletContextListener;
  4. public class LogbackConfigListener implements ServletContextListener {
  5. public void contextInitialized(ServletContextEvent event) {
  6. LogbackWebConfigurer.initLogging(event.getServletContext());
  7. }
  8. public void contextDestroyed(ServletContextEvent event) {
  9. LogbackWebConfigurer.shutdownLogging(event.getServletContext());
  10. }
  11. }
  12. </span>

   LogbackConfigurer類
   
   
   
   
  1. <span style="font-family:Comic Sans MS;font-size:18px;">package com.util;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.net.URL;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.util.ResourceUtils;
  7. import org.springframework.util.SystemPropertyUtils;
  8. import ch.qos.logback.classic.LoggerContext;
  9. import ch.qos.logback.classic.joran.JoranConfigurator;
  10. import ch.qos.logback.core.joran.spi.JoranException;
  11. public abstract class LogbackConfigurer {
  12. /** Pseudo URL prefix for loading from the class path: "classpath:" */
  13. public static final String CLASSPATH_URL_PREFIX = "classpath:";
  14. /** Extension that indicates a logback XML config file: ".xml" */
  15. public static final String XML_FILE_EXTENSION = ".xml";
  16. private static LoggerContext lc = (LoggerContext) LoggerFactory
  17. .getILoggerFactory();
  18. private static JoranConfigurator configurator = new JoranConfigurator();
  19. /**
  20. * Initialize logback from the given file location, with no config file
  21. * refreshing. Assumes an XML file in case of a ".xml" file extension, and a
  22. * properties file otherwise.
  23. *
  24. * @param location
  25. * the location of the config file: either a "classpath:"
  26. * location (e.g. "classpath:mylogback.properties"), an absolute
  27. * file URL (e.g.
  28. * "file:C:/logback.properties), or a plain absolute path in the file system (e.g. "
  29. * C:/logback.properties")
  30. * @throws FileNotFoundException
  31. * if the location specifies an invalid file path
  32. */
  33. public static void initLogging(String location)
  34. throws FileNotFoundException {
  35. String resolvedLocation = SystemPropertyUtils
  36. .resolvePlaceholders(location);
  37. URL url = ResourceUtils.getURL(resolvedLocation);
  38. if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
  39. // DOMConfigurator.configure(url);
  40. configurator.setContext(lc);
  41. lc.reset();
  42. try {
  43. configurator.doConfigure(url);
  44. } catch (JoranException ex) {
  45. throw new FileNotFoundException(url.getPath());
  46. }
  47. lc.start();
  48. }
  49. // else {
  50. // PropertyConfigurator.configure(url);
  51. // }
  52. }
  53. /**
  54. * Shut down logback, properly releasing all file locks.
  55. * <p>
  56. * This isn't strictly necessary, but recommended for shutting down logback
  57. * in a scenario where the host VM stays alive (for example, when shutting
  58. * down an application in a J2EE environment).
  59. */
  60. public static void shutdownLogging() {
  61. lc.stop();
  62. }
  63. /**
  64. * Set the specified system property to the current working directory.
  65. * <p>
  66. * This can be used e.g. for test environments, for applications that
  67. * leverage logbackWebConfigurer's "webAppRootKey" support in a web
  68. * environment.
  69. *
  70. * @param key
  71. * system property key to use, as expected in logback
  72. * configuration (for example: "demo.root", used as
  73. * "${demo.root}/WEB-INF/demo.log")
  74. * @see org.springframework.web.util.logbackWebConfigurer
  75. */
  76. public static void setWorkingDirSystemProperty(String key) {
  77. System.setProperty(key, new File("").getAbsolutePath());
  78. }
  79. }
  80. </span>

  LogbackWebConfigurer類
   
   
   
   
  1. <span style= "font-family:Comic Sans MS;font-size:18px;"> package com.util;
  2. import java.io.FileNotFoundException;
  3. import javax.servlet.ServletContext;
  4. import org.springframework.util.ResourceUtils;
  5. import org.springframework.util.SystemPropertyUtils;
  6. import org.springframework.web.util.WebUtils;
  7. public abstract class LogbackWebConfigurer {
  8. /** Parameter specifying the location of the logback config file */
  9. public static final String CONFIG_LOCATION_PARAM = "logbackConfigLocation";
  10. /**
  11. * Parameter specifying the refresh interval for checking the logback config
  12. * file
  13. */
  14. public static final String REFRESH_INTERVAL_PARAM = "logbackRefreshInterval";
  15. /** Parameter specifying whether to expose the web app root system property */
  16. public static final String EXPOSE_WEB_APP_ROOT_PARAM = "logbackExposeWebAppRoot";
  17. /**
  18. * Initialize logback, including setting the web app root system property.
  19. *
  20. * @param servletContext
  21. * the current ServletContext
  22. * @see WebUtils#setWebAppRootSystemProperty
  23. */
  24. public static void initLogging(ServletContext servletContext) {
  25. // Expose the web app root system property.
  26. if (exposeWebAppRoot(servletContext)) {
  27. WebUtils.setWebAppRootSystemProperty(servletContext);
  28. }
  29. // Only perform custom logback initialization in case of a config file.
  30. String location = servletContext
  31. .getInitParameter(CONFIG_LOCATION_PARAM);
  32. if (location != null) {
  33. // Perform actual logback initialization; else rely on logback's
  34. // default initialization.
  35. try {
  36. // Return a URL (e.g. "classpath:" or "file:") as-is;
  37. // consider a plain file path as relative to the web application
  38. // root directory.
  39. if (!ResourceUtils.isUrl(location)) {
  40. // Resolve system property placeholders before resolving
  41. // real path.
  42. location = SystemPropertyUtils
  43. .resolvePlaceholders(location);
  44. location = WebUtils.getRealPath(servletContext, location);
  45. }
  46. // Write log message to server log.
  47. servletContext.log( "Initializing logback from [" + location
  48. + "]");
  49. // Initialize without refresh check, i.e. without logback's
  50. // watchdog thread.
  51. LogbackConfigurer.initLogging(location);
  52. } catch (FileNotFoundException ex) {
  53. throw new IllegalArgumentException(
  54. "Invalid 'logbackConfigLocation' parameter: "
  55. + ex.getMessage());
  56. }
  57. }
  58. }
  59. /**
  60. * Shut down logback, properly releasing all file locks and resetting the
  61. * web app root system property.
  62. *
  63. * @param servletContext
  64. * the current ServletContext
  65. * @see WebUtils#removeWebAppRootSystemProperty
  66. */
  67. public static void shutdownLogging(ServletContext servletContext) {
  68. servletContext.log( "Shutting down logback");
  69. try {
  70. LogbackConfigurer.shutdownLogging();
  71. } finally {
  72. // Remove the web app root system property.
  73. if (exposeWebAppRoot(servletContext)) {
  74. WebUtils.removeWebAppRootSystemProperty(servletContext);
  75. }
  76. }
  77. }
  78. /**
  79. * Return whether to expose the web app root system property, checking the
  80. * corresponding ServletContext init parameter.
  81. *
  82. * @see #EXPOSE_WEB_APP_ROOT_PARAM
  83. */
  84. private static boolean exposeWebAppRoot(ServletContext servletContext) {
  85. String exposeWebAppRootParam = servletContext
  86. .getInitParameter(EXPOSE_WEB_APP_ROOT_PARAM);
  87. return (exposeWebAppRootParam == null || Boolean
  88. .valueOf(exposeWebAppRootParam));
  89. }
  90. }
  91. </span>


   logback.XML配置
下面來看一下這個xml是如何配置的
   
   
   
   
  1. <span style="font-family:Comic Sans MS;font-size:18px;"> <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- ROOT 節點 -->
  3. <!-- 屬性描述 scan:性設置爲true時,配置文件若是發生改變,將會被從新加載,默認值爲true scanPeriod:設置監測配置文件是否有修改的時間間隔,若是沒有給出時間單位,默認單位是毫秒。當scan爲true時,此屬性生效。默認的時間間隔爲1分鐘。
  4. debug:當此屬性設置爲true時,將打印出logback內部日誌信息,實時查看logback運行狀態。默認值爲false。 -->
  5. <configuration scan="true" scanPeriod="60 seconds" debug="false">
  6. <!-- 定義日誌文件 輸入位置,注意此處的/ -->
  7. <property name="log_dir" value="E:/logs" />
  8. <!-- 日誌最大的歷史 60天 -->
  9. <property name="maxHistory" value="60"> </property>
  10. <!-- 控制檯輸出日誌 -->
  11. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  12. <encoder>
  13. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
  14. %msg%n </pattern>
  15. </encoder>
  16. </appender>
  17. <!-- 出錯日誌 appender -->
  18. <appender name="ERROR"
  19. class= "ch.qos.logback.core.rolling.RollingFileAppender">
  20. <!-- 在多數的Log工具中,級別是能夠傳遞,例如若是指定了日誌輸出級別爲DEBUG, 那麼INFO、ERROR級別的log也會出如今日誌文件。這種默認給程序的調試帶來了不少的麻煩
  21. 經過配置Filter 來嚴格控制日誌輸入級別 <filter class="ch.qos.logback.classic.filter.LevelFilter">
  22. <level>ERROR/level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch>
  23. </filter> -->
  24. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  25. <!-- 按天回滾 daily -->
  26. <fileNamePattern>${log_dir}/error-log-%d{yyyy-MM-dd}.log
  27. </fileNamePattern>
  28. <!-- 日誌最大的歷史 60天 -->
  29. <maxHistory>${maxHistory} </maxHistory>
  30. </rollingPolicy>
  31. <encoder>
  32. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
  33. %msg%n </pattern>
  34. </encoder>
  35. </appender>
  36. <!-- INFO 日誌 appender -->
  37. <appender name="INFO"
  38. class= "ch.qos.logback.core.rolling.RollingFileAppender">
  39. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  40. <!-- 按天回滾 daily -->
  41. <fileNamePattern>${log_dir}/info-log-%d{yyyy-MM-dd}.log
  42. </fileNamePattern>
  43. <!-- 日誌最大的歷史 60天 -->
  44. <maxHistory>${maxHistory} </maxHistory>
  45. </rollingPolicy>
  46. <encoder>
  47. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
  48. %msg%n </pattern>
  49. </encoder>
  50. </appender>
  51. <!-- 訪問日誌 appender -->
  52. <appender name="ACCESS"
  53. class= "ch.qos.logback.core.rolling.RollingFileAppender">
  54. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  55. <!-- 按天回滾 daily -->
  56. <fileNamePattern>${log_dir}/access-log-%d{yyyy-MM-dd}.log
  57. </fileNamePattern>
  58. <!-- 日誌最大的歷史 60天 -->
  59. <maxHistory>${maxHistory} </maxHistory>
  60. </rollingPolicy>
  61. <encoder>
  62. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
  63. %msg%n </pattern>
  64. </encoder>
  65. </appender>
  66. <!-- 系統用戶操做日誌 appender -->
  67. <appender name="SYS-USER"
  68. class= "ch.qos.logback.core.rolling.RollingFileAppender">
  69. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  70. <!-- 按天回滾 daily -->
  71. <fileNamePattern>${log_dir}/sys_user-log-%d{yyyy-MM-dd}.log
  72. </fileNamePattern>
  73. <!-- 日誌最大的歷史 60天 -->
  74. <maxHistory>${maxHistory} </maxHistory>
  75. </rollingPolicy>
  76. <encoder>
  77. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
  78. %msg%n </pattern>
  79. </encoder>
  80. </appender>
  81. <!-- 打印SQL輸出 -->
  82. <logger name="java.sql.Connection" level="DEBUG" />
  83. <logger name="java.sql.Statement" level="DEBUG" />
  84. <logger name="java.sql.PreparedStatement" level="DEBUG" />
  85. <!--error錯誤日誌 additivity="false"表示不向上傳遞 -->
  86. <!-- <logger name="com.test" level="error" > -->
  87. <!-- <appender-ref ref="ERROR" /> -->
  88. <!-- </logger> -->
  89. <!--info日誌 -->
  90. <logger name="com.test" level="info" additivity="false">
  91. <appender-ref ref="INFO" />
  92. </logger>
  93. <!--訪問日誌 -->
  94. <!-- <logger name="com.test" level="info" additivity="false"> -->
  95. <!-- <appender-ref ref="ACCESS" /> -->
  96. <!-- </logger> -->
  97. <!--系統用戶操做日誌 -->
  98. <!-- <logger name="com.test" level="info" additivity="false"> -->
  99. <!-- <appender-ref ref="SYS-USER" /> -->
  100. <!-- </logger> -->
  101. <root>
  102. <level value="INFO" />
  103. <appender-ref ref="stdout" />
  104. </root>
  105. </configuration> </span>


關於這個XML文件的詳細講解請參考 http://blog.csdn.net/haidage/article/details/6794509
相關文章
相關標籤/搜索