1. Basic Concept
1.1 Logger Framework:
1. logging(java.util):
2. logback(ch.qos.logback):
3. log4j(org.apache.logging.log4j):
4. log4j2: an improved version of log4j. Outperforms than log4j, logback, java logging especially in multi-threaded context. https://logging.apache.org/log4j/2.x/
5. slf4j(org.slf4j): Simple Logging Facade for Java, 即它仅仅是一个为java程序提供日志输出的统一接口,并不是一个具体的日志实现方案,就比如JDBC一样,只是一种规则而已。所以单独的slf4j是不能工作的,必须搭配其他具体的日志实现方案,比如apache的org.apache.log4j.Logger, jdk自带的java.util.logging.Logger等
2. Difference Analysis
org.apache.log4j: is the core component for logger framework;
Log4j 是Apache的一个开源项目,通过使用log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的时间记录器,Unix Syslog守护进程等,我们也可以控制每一条日志的输出格式。通过定义每一条日志信息的级别,更加细致地控制日志的生成过程。Log4j有三个重要的组件构成: 日志记录器(Loggers), 输出端(Appenders)和日志格式化器(Layout).
5 中级别: DEBUG、INFO、WARN、ERROR、FATAL, 可以通过配置实现不同内容的输出。
3. Use Case:
3.1 Use of Spring-Boot related projects
Repository for embedding logger framework into Spring Framework.
3.1.1 spring-boot-starter-logging

3.1.2 spring-boot-starter-log4j(1.3.8.RELEASE)

3.1.3 logback

3.2 Use of log4j:
log4j.properties, log4j.configuration, etc. are default configuration files defined in log4j core component(org/apache/log4j/PropertyConfigurator.java) to provide user-customized preferences. (AppenderSkeleton interface implement required)
AsyncAppender
ConsoleAppender(org.apache.log4j)
RollingFileAppender(org.apache.log4j)
KafkaLog4jAppender(org.apache.kafka.kafka-log4j-appender)

4. Custom Appender
It's relatively easy to make users own logging api work. Users could define their own logging api to satisfy different requirements. And to enable the new logging api, two core steps are required to accomplish:
1. Design your own appender, AppenderSkeleton(org/apache/log4j/AppenderSkeleton.java)
2. Just add an appender indicating customized appender to log4j.properties or its similar configuration files.
网友评论