前言

log4j2配置文件有不少配置,下面罗列一些,方便快速查看。

简单的配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="INFO">
  3. <Appenders>
  4. <Console name="Console" target="SYSTEM_OUT">
  5. <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
  6. </Console>
  7. <File name="MyFile" fileName="all.log" immediateFlush="false" append="false">
  8. <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  9. </File>
  10. </Appenders>
  11. <Loggers>
  12. <Root level="debug">
  13. <AppenderRef ref="Console" />
  14. <AppenderRef ref="MyFile"/>
  15. </Root>
  16. </Loggers>
  17. </Configuration>

严格模式的配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration monitorinterval="30" status="info" strict="true">
  3. <Properties>
  4. <Property name="filename">log/CelsiusConverter.log</Property>
  5. </Properties>
  6. <Appenders>
  7. <Appender type="Console" name="Console">
  8. <Layout type="PatternLayout" pattern="%d %p [%t] %m%n" />
  9. </Appender>
  10. <Appender type="Console" name="FLOW">
  11. <Layout type="PatternLayout" pattern="%C{1}.%M %m %ex%n" />
  12. </Appender>
  13. <Appender type="File" name="File" fileName="${filename}">
  14. <Layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n" />
  15. </Appender>
  16. </Appenders>
  17. <Loggers>
  18. <Root level="debug">
  19. <AppenderRef ref="File" />
  20. <AppenderRef ref="Console" />
  21. <!-- Use FLOW to trace down exact method sending the msg -->
  22. <!-- <AppenderRef ref="FLOW" /> -->
  23. </Root>
  24. </Loggers>
  25. </Configuration>

完整配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置-->
  3. <!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数-->
  4. <configuration status="WARN" monitorInterval="30">
  5. <!--先定义所有的appender-->
  6. <appenders>
  7. <!--输出到控制台-->
  8. <console name="Console" target="SYSTEM_OUT">
  9. <!--输出日志的格式-->
  10. <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
  11. </console>
  12. <!--打印到文件中,这个log每次运行程序会自动清空,由append属性决定-->
  13. <File name="log" fileName="log/test.log" append="false">
  14. <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
  15. </File>
  16. <!--文件滚动, 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
  17. <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log"
  18. filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
  19. <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
  20. <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
  21. <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
  22. <Policies>
  23. <DefaultRolloverStrategy max="20"/> <!--文件夹下最多的文件个数-->
  24. <TimeBasedTriggeringPolicy interval="2 hour"/> <!--多长时间滚动一次-->
  25. <SizeBasedTriggeringPolicy size="100 MB"/> <!-- 一个日志文件的最大大小 -->
  26. </Policies>
  27. </RollingFile>
  28. </appenders>
  29. <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
  30. <loggers>
  31. <!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
  32. <logger name="org.springframework" level="INFO"></logger>
  33. <logger name="org.mybatis" level="INFO"></logger>
  34. <root level="all">
  35. <appender-ref ref="Console"/>
  36. <appender-ref ref="RollingFileInfo"/>
  37. <appender-ref ref="RollingFileWarn"/>
  38. <appender-ref ref="RollingFileError"/>
  39. </root>
  40. </loggers>
  41. </configuration>

tomcat web 配置

  1. <context-param>
  2. <param-name>log4jConfiguration</param-name>
  3. <param-value>/WEB-INF/conf/log4j2.xml</param-value>
  4. </context-param>
  5. <listener>
  6. <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
  7. </listener>

项目中的配置

dev环境

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <Configuration status="warn">
  3. <Appenders>
  4. <Console name="STOUT" target="SYSTEM_OUT">
  5. <PatternLayout
  6. pattern="%highlight{%d [%t] %-5level: %msg%n%throwable}{FATAL=white, ERROR=red, WARN=blue, INFO=black, DEBUG=green, TRACE=blue}"/>
  7. </Console>
  8. </Appenders>
  9. <Loggers>
  10. <Root level="all">
  11. <AppenderRef ref="STOUT"/>
  12. </Root>
  13. </Loggers>
  14. </Configuration>

test环境

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <Configuration status="warn">
  3. <properties>
  4. <property name="logPath">
  5. /project/backend/code/
  6. </property>
  7. </properties>
  8. <customLevels>
  9. <customLevel name="BIZ" intLevel="350"/>
  10. </customLevels>
  11. <Appenders>
  12. <RollingRandomAccessFile name="ERROR_LOGGER" fileName="${logPath}/logs2/error/error.log"
  13. filePattern="${logPath}/logs2/error/error.log.%d{yyyy-MM-dd-HH}">
  14. <PatternLayout>
  15. <Pattern>[date:%d{DEFAULT}] [%t] %5level %c{1}: %msg%n</Pattern>
  16. </PatternLayout>
  17. <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
  18. <Policies>
  19. <TimeBasedTriggeringPolicy/>
  20. <SizeBasedTriggeringPolicy size="400 MB"/>
  21. </Policies>
  22. <DefaultRolloverStrategy max="20"/>
  23. </RollingRandomAccessFile>
  24. <RollingRandomAccessFile name="BIZ_LOGGER" fileName="${logPath}/logs2/biz/biz.log"
  25. filePattern="${logPath}/logs2/biz/biz.log.%d{yyyy-MM-dd-HH}">
  26. <PatternLayout>
  27. <Pattern>[date:%d{DEFAULT}] [%t] %5level %c{1}: %msg%n</Pattern>
  28. </PatternLayout>
  29. <filters>
  30. <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
  31. <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
  32. <ThresholdFilter level="biz" onMatch="ACCEPT" onMismatch="DENY"/>
  33. </filters>
  34. <Policies>
  35. <TimeBasedTriggeringPolicy/>
  36. <SizeBasedTriggeringPolicy size="400 MB"/>
  37. </Policies>
  38. <DefaultRolloverStrategy max="20"/>
  39. </RollingRandomAccessFile>
  40. <RollingRandomAccessFile name="INFO_LOGGER" fileName="${logPath}/logs2/info/info.log"
  41. filePattern="${logPath}/logs2/info/info.log.%d{yyyy-MM-dd-HH}">
  42. <PatternLayout>
  43. <Pattern>[date:%d{DEFAULT}] [%t] %5level %c{1}: %msg%n</Pattern>
  44. </PatternLayout>
  45. <filters>
  46. <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
  47. <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
  48. <ThresholdFilter level="biz" onMatch="DENY" onMismatch="NEUTRAL"/>
  49. <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
  50. </filters>
  51. <Policies>
  52. <TimeBasedTriggeringPolicy/>
  53. <SizeBasedTriggeringPolicy size="400 MB"/>
  54. </Policies>
  55. <DefaultRolloverStrategy max="20"/>
  56. </RollingRandomAccessFile>
  57. <!--###################################-->
  58. <RollingRandomAccessFile name="TRACE_ERROR_LOGGER" fileName="${logPath}/logs2/trace/error.log"
  59. filePattern="${logPath}/logs2/trace/trace.log.%d{yyyy-MM-dd-HH}">
  60. <PatternLayout>
  61. <Pattern>[date:%d{DEFAULT}] [%t] %5level %c{1}: %msg%n</Pattern>
  62. </PatternLayout>
  63. <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
  64. <Policies>
  65. <TimeBasedTriggeringPolicy/>
  66. <SizeBasedTriggeringPolicy size="400 MB"/>
  67. </Policies>
  68. <DefaultRolloverStrategy max="20"/>
  69. </RollingRandomAccessFile>
  70. <RollingRandomAccessFile name="TRACE_SQL_LOGGER" fileName="${logPath}/logs2/trace/sql.log"
  71. filePattern="${logPath}/logs2/sql/sql.log.%d{yyyy-MM-dd-HH}">
  72. <PatternLayout>
  73. <Pattern>[date:%d{DEFAULT}] [%t] %5level %c{1}: %msg%n</Pattern>
  74. </PatternLayout>
  75. <Policies>
  76. <TimeBasedTriggeringPolicy/>
  77. <SizeBasedTriggeringPolicy size="400 MB"/>
  78. </Policies>
  79. <DefaultRolloverStrategy max="20"/>
  80. </RollingRandomAccessFile>
  81. </Appenders>
  82. <Loggers>
  83. <Logger name="com.alibaba.druid" additivity="false" level="info">
  84. <AppenderRef ref="TRACE_SQL_LOGGER"/>
  85. </Logger>
  86. <Root level="info">
  87. <AppenderRef ref="ERROR_LOGGER" level="error"/>
  88. <AppenderRef ref="BIZ_LOGGER" level="biz"/>
  89. <AppenderRef ref="INFO_LOGGER" level="info"/>
  90. </Root>
  91. </Loggers>
  92. </Configuration>

参考