惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
W
WeLiveSecurity
IT之家
IT之家
A
About on SuperTechFans
B
Blog
L
LangChain Blog
H
Help Net Security
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
Google Online Security Blog
Google Online Security Blog
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
Schneier on Security
Schneier on Security
GbyAI
GbyAI
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Cloudbric
Cloudbric
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
S
Security Affairs
The Last Watchdog
The Last Watchdog
H
Heimdal Security Blog
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
V
Visual Studio Blog
H
Hacker News: Front Page
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
Jina AI
Jina AI
N
News | PayPal Newsroom
S
Schneier on Security

博客园 - lonecloud

联通云主机安装openclaw 可食用教程 win10 出现你的设备有工作或学校账户方面的问题,请再次登录以访问组织资源 解决MAC升级系统后,GIT报错missing xcrun Git 在Mac下的中文乱码问题 记一个使用fyne-cross编译的坑 windows gcc 遇到的问题解决 Git 提交界面中文乱码解决 WARNING: IPv4 forwarding is disabled. Networking will not work. postgresql数据类型 JDBC 规范中文版 4.2- 第五章 类和接口 JDBC 规范中文版 4.2 -第四章 概览 JDBC 规范中文版 4.2 -第三章 新特性 JDBC 规范中文版 4.2 -第二章 目标 JDBC 规范中文版 4.2 -第一章 简介 基础回顾-线程的几种状态 一文读懂BeanFactory和FactoryBean区别 IDEA技巧-自动导包(Auto import)以及自动优化包 Drools学习教程 - 3. 规则属性 Drools学习教程 - 2. 规则文件语法
Mybatis源码-详解Mybatis日志设计
lonecloud · 2020-12-31 · via 博客园 - lonecloud

众所周知,Mybatis是在ORM领域,使用的较为广泛,而Mybatis日志体系,也是值得我们学习的地方,由于各个厂商都有自己的日志api,基于此,Mybatis对日志进行了封装,来适配各个厂商的日志

1. 日志级别

在Mybatis内部定义了4个级别:

  1. Error:错误
  2. warn:警告
  3. debug:调试
  4. trance:

2. 日志优先级

error>warn>debug>trance

3. 源码解析

接口

org.apache.ibatis.logging.Log

该接口定义了一系列的常用日志方法,便于日志打印

/**
 * 日志相关接口
 * @author Clinton Begin
 */
public interface Log {
  /**
   * 判断debug是否打开
   * @return
   */
  boolean isDebugEnabled();

  /**
   * 判断trance是否打开
   * @return
   */
  boolean isTraceEnabled();

  /**
   * 错误日志打印
   * @param s
   * @param e
   */
  void error(String s, Throwable e);

  void error(String s);

  /**
   * debug日志打印
   * @param s
   */
  void debug(String s);

  /**
   * trance日志打印
   * @param s
   */
  void trace(String s);

  /**
   * warn 日志打印
   * @param s
   */
  void warn(String s);

}

实现类:

基于各个厂商的日志对Log有以下实现,都基本上是根据厂商日志,编写对应的适配器类来调用对应的日志系统

日志集成核心类

org.apache.ibatis.logging.LogFactory

实现原理:

  1. 使用Static 方法在类进行初始化的时候去尝试加载对应的实现类
  2. 优先级为:slf4j>commonLog>log4j2>log4j>jdk>noLog
  3. 当获取到的实现类不为空时候则不进行加载了
/**
 *    Copyright 2009-2020 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.logging;

import java.lang.reflect.Constructor;

/**
 * 日志工厂类
 * @author Clinton Begin
 * @author Eduardo Macarron
 */
public final class LogFactory {

  /**
   * Marker to be used by logging implementations that support markers.
   */
  public static final String MARKER = "MYBATIS";
  /**
   * 日志系统构造函数
   */
  private static Constructor<? extends Log> logConstructor;

  static {
    //尝试加载实现类
    /**
     * slf4j>commonLog>log4j2>log4j>jdk>noLog
     */
    tryImplementation(LogFactory::useSlf4jLogging);
    tryImplementation(LogFactory::useCommonsLogging);
    tryImplementation(LogFactory::useLog4J2Logging);
    tryImplementation(LogFactory::useLog4JLogging);
    tryImplementation(LogFactory::useJdkLogging);
    tryImplementation(LogFactory::useNoLogging);
  }

  private LogFactory() {
    // disable construction
  }

  public static Log getLog(Class<?> clazz) {
    //获取日志打印
    return getLog(clazz.getName());
  }

  /**
   * 根据类名获取对应的日志实例
   * @param logger
   * @return
   */
  public static Log getLog(String logger) {
    try {
      return logConstructor.newInstance(logger);
    } catch (Throwable t) {
      throw new LogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);
    }
  }

  /**
   * 设置个性化日志
   * @param clazz
   */
  public static synchronized void useCustomLogging(Class<? extends Log> clazz) {
    setImplementation(clazz);
  }

  public static synchronized void useSlf4jLogging() {
    setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
  }

  public static synchronized void useCommonsLogging() {
    setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
  }

  public static synchronized void useLog4JLogging() {
    setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
  }

  public static synchronized void useLog4J2Logging() {
    setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
  }

  public static synchronized void useJdkLogging() {
    setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
  }

  public static synchronized void useStdOutLogging() {
    setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
  }

  public static synchronized void useNoLogging() {
    setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
  }

  /**
   * 尝试加载实现类
   * @param runnable
   */
  private static void tryImplementation(Runnable runnable) {
    if (logConstructor == null) {
      try {
        runnable.run();
      } catch (Throwable t) {
        // ignore
      }
    }
  }

  /**
   * 设置实现类方法
   * @param implClass
   */
  private static void setImplementation(Class<? extends Log> implClass) {
    try {
      //获取Log默认String 的构造参数
      Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
      //创建对应的实例
      Log log = candidate.newInstance(LogFactory.class.getName());
      if (log.isDebugEnabled()) {
        log.debug("Logging initialized using '" + implClass + "' adapter.");
      }
      //将构造喊出赋值给对应对象
      logConstructor = candidate;
    } catch (Throwable t) {
      throw new LogException("Error setting Log implementation.  Cause: " + t, t);
    }
  }

}