日志组件简单使用

目录

目录

关系图

实现

logback

1. logback + slf4j

 2. logback + JCL

log4j

1. log4j

2. log4j + JCL

3. log4j + slf4j


       简单梳理日志使用方式,包括日志关系图、不同日志的依赖、日志配置文件、日志使用,便于学习和使用。

        日志组件包括: 接口层+适配层+实现层,典型的面向接口编程,依赖倒置原则,抽象与实现分离。这也是不断的完善 慢慢发展的结果。可以把日志看成是三个人实现的,每个人的具体实现不一样,为了统一规范,有两个人自己提供了接口层。在使用日志时,引入接口层的接口就可以了。具体 使用哪一种日志,可以灵活的切换。

        需要注意的是,想要达到灵活切换,就需要在使用的时候,引入的是接口层的API,而不是实现层的API。下面看一下 日志关系图和不同的使用方式。

关系图

日志组件的关系如图:

实现

logback

logback 是日志框架的实现层,也是slf4j接口层的默认实现。

logback 分为三个模块,分别是 logback-core、logback-classic、logback-access

logback-classic模块依赖了 logback-core模块和slf4j-api

1. logback + slf4j

这里直接使用  slf4j + logback 的方式

pom依赖

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.32</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.11</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.2.11</version>
    </dependency>

日志配置文件

<configuration>

    <!-- 设置日志名称,默认是default,可以使用 %contextName 来引用这个名字 -->
    <contextName>mytest</contextName>

    <!-- 监听器 -->
<!--    <statusListener class = "ch.qos.logback.core.status.OnConsoleStatusListener" />-->

    <!-- appender: name 和 class 必须设置 -->
    <appender name="file" class="ch.qos.logback.core.FileAppender">
        <!-- 设置日志文件路径  -->
        <file></file>

        <!--zero or one <layout>-->
        <!-- layout 强制属性:class。可以包含其他子元素 -->
        <layout class="ch.qos.logback.core.layout.EchoLayout"></layout>
        <!--zero or more <encoder>-->
        <encoder></encoder>
        <!--zero or more <filter>-->
        <filter></filter>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern> %contextName [%thread]  %-5level  %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- logger,只有name,level:TRACE, DEBUG, INFO, WARN, ERROR, ALL or OFF -->
    <!-- 这里可以定义具体的日志 -->
    <logger name = "aaaaa" level = "debug" additivity="false">
    <!--The <logger> element may contain zero or more <appender-ref> elements-->
        <appender-ref ref="STDOUT"/>
    </logger>

    <!--The <root> element configures the root logger. 只能有一个 level属性
        引入 root logger 已经叫 root名字了,其他的logger不能再叫 root-->
    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

使用

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * logback 配置文件路径配置
 * Let us begin by discussing the initialization steps that logback follows to try to configure itself:
 *
 *         Logback tries to find a file called logback-test.xml in the classpath.
 *
 *         If no such file is found, it checks for the file logback.xml in the classpath..
 *
 *         If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.
 *
 *         If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
 *
 * 通过jvm配置参数:
 * -Dlogback.configurationFile=src/main/resources/simple0.xml
 */
public class AppLOGBACK {

    static Logger logger = LoggerFactory.getLogger("aaaaa");

    public static void main( String[] args )
    {
        logger.debug( "Hello World!" );
    }
}

 2. logback + JCL

pom.xml

<!-- jcl + logback -->
    <!-- jcl -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- jcl 转换 logback -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
      <version>1.7.32</version>
    </dependency>

    <!-- logback -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.32</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.11</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.2.11</version>
    </dependency>
    <!-- jcl + logback  end -->

日志配置及使用

安照logback的方式

log4j

1. log4j

单独使用 log4j 的具体实现

pom依赖

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
    </dependency>

日志配置

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="   %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

使用

//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;

/**
 * log4j 配置文件:
 *
 * Log4j has the ability to automatically configure itself during initialization. When Log4j starts it will locate all the ConfigurationFactory plugins and arrange them in weighted order from highest to lowest. As delivered, Log4j contains four ConfigurationFactory implementations: one for JSON, one for YAML, one for properties, and one for XML.
 *
 * Log4j will inspect the "log4j2.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension. Note that this is not restricted to a location on the local file system and may contain a URL.
 *   -Dlog4j2.configurationFile=src/main/resources/simple1.xml
 *
 * If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
 * If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
 * If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
 * If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
 * If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
 * If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
 * If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
 * If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
 * If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.
 */
public class APPLOG4J {

//    static Logger logger = LogManager.getLogger(APPSLF4J.class);
    public static void main(String[] args) {

//        logger.info("APPSLF4J");

    }
}

2. log4j + JCL

接口层+实现层

依赖

    <!-- jcl + log4j-->
    <!-- jcl api -->
    <<dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- log4j 绑定 jcl  -->
    <<dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-jcl</artifactId>
      <version>2.17.2</version>
    </dependency>

    <!-- log4j 依赖 -->
   <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
    </dependency>
    <!-- jcl + log4j end -->

日志配置

 按照 log4j的方式设置

使用

package com.wxj;

//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;

public class APPJCL {
//    static Log logger = LogFactory.getLog(APPJCL.class);
    public static void main(String[] args) {

//        logger.info("APPJCL");


    }
}

3. log4j + slf4j

接口层+实现层

pom.xml

    <!-- slf4j + log4j -->
    <!-- slf4j -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.32</version>
    </dependency>

    <!-- log4j 转换 slf4j -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
    </dependency>
    <!-- log4j依赖 -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
    </dependency>

日志配置及使用:

   安照 log4j的方式

官网地址:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值