Maven

版本配置

  1. <properties>
  2. <log4j2.version>2.11.1</log4j2.version>
  3. </properties>

依赖配置

  1. <!-- log4j2 -->
  2. <dependency>
  3. <groupId>org.apache.logging.log4j</groupId>
  4. <artifactId>log4j-1.2-api</artifactId>
  5. <version>${log4j2.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.logging.log4j</groupId>
  9. <artifactId>log4j-jcl</artifactId>
  10. <version>${log4j2.version}</version>
  11. <exclusions>
  12. <exclusion>
  13. <artifactId>commons-logging</artifactId>
  14. <groupId>commons-logging</groupId>
  15. </exclusion>
  16. </exclusions>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.apache.logging.log4j</groupId>
  20. <artifactId>log4j-slf4j-impl</artifactId>
  21. <version>${log4j2.version}</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.apache.logging.log4j</groupId>
  25. <artifactId>log4j-jul</artifactId>
  26. <version>${log4j2.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.apache.logging.log4j</groupId>
  30. <artifactId>log4j-api-scala_${scala.binary.version}</artifactId>
  31. <version>11.0</version>
  32. <exclusions>
  33. <exclusion>
  34. <artifactId>log4j-api</artifactId>
  35. <groupId>org.apache.logging.log4j</groupId>
  36. </exclusion>
  37. <exclusion>
  38. <artifactId>scala-library</artifactId>
  39. <groupId>org.scala-lang</groupId>
  40. </exclusion>
  41. <exclusion>
  42. <artifactId>scala-reflect</artifactId>
  43. <groupId>org.scala-lang</groupId>
  44. </exclusion>
  45. </exclusions>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.apache.logging.log4j</groupId>
  49. <artifactId>log4j-api</artifactId>
  50. <version>${log4j2.version}</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.apache.logging.log4j</groupId>
  54. <artifactId>log4j-core</artifactId>
  55. <version>${log4j2.version}</version>
  56. </dependency>

配置模板

dev

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration status="OFF">
  3. <properties>
  4. <property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n</property>
  5. </properties>
  6. <appenders>
  7. <Console name="Console" target="SYSTEM_OUT">
  8. <PatternLayout pattern="${pattern}"/>
  9. </Console>
  10. </appenders>
  11. <loggers>
  12. <logger name="com.haima" level="debug"/>
  13. <root level="info">
  14. <appender-ref ref="Console"/>
  15. </root>
  16. </loggers>
  17. </configuration>

prod

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration status="OFF">
  3. <properties>
  4. <property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n</property>
  5. <property name="default_file_name">logs/audit.log</property>
  6. <property name="rolling_file_name">logs/audit-%d{yyyy-MM-dd}-%i.log</property>
  7. <property name="every_file_size">50M</property>
  8. </properties>
  9. <appenders>
  10. <Console name="Console" target="SYSTEM_OUT">
  11. <PatternLayout pattern="${pattern}" />
  12. </Console>
  13. <RollingFile name="RollingFile" fileName="${default_file_name}"
  14. filePattern="${rolling_file_name}">
  15. <PatternLayout pattern="${pattern}" />
  16. <SizeBasedTriggeringPolicy size="${every_file_size}" />
  17. </RollingFile>
  18. </appenders>
  19. <loggers>
  20. <logger name="com.haima" level="info" />
  21. <root level="info">
  22. <appender-ref ref="RollingFile" />
  23. </root>
  24. </loggers>
  25. </configuration>

定制脚本文件名称

参考

  1. -Dlog4j.configurationFile=/opt/conf/log4j2-audit.xml
文档更新时间: 2019-06-21 12:02   作者:admin