SpringMVC集成LogBack,相关配置


     最近在做项目中需要用到日志,本来选取的是Log4j,最后经过对比之后还是发现LogBack在性能上比Log4j有优势。至于有什么好处,请参考下面这篇文章。

从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

posted @ 2019-02-20 10:03  tangblog  阅读(4519)  评论(0编辑  收藏  举报