-
Notifications
You must be signed in to change notification settings - Fork 12.9k
/
Copy pathLogFactory.java.html
123 lines (104 loc) · 6.39 KB
/
LogFactory.java.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>LogFactory.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">mybatis</a> > <a href="index.source.html" class="el_package">org.apache.ibatis.logging</a> > <span class="el_source">LogFactory.java</span></div><h1>LogFactory.java</h1><pre class="source lang-java linenums">/*
* Copyright 2009-2024 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
*
* https://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;
import java.util.concurrent.locks.ReentrantLock;
/**
* @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";
<span class="fc" id="L32"> private static final ReentrantLock lock = new ReentrantLock();</span>
private static Constructor<? extends Log> logConstructor;
static {
<span class="fc" id="L36"> tryImplementation(LogFactory::useSlf4jLogging);</span>
<span class="fc" id="L37"> tryImplementation(LogFactory::useCommonsLogging);</span>
<span class="fc" id="L38"> tryImplementation(LogFactory::useLog4J2Logging);</span>
<span class="fc" id="L39"> tryImplementation(LogFactory::useLog4JLogging);</span>
<span class="fc" id="L40"> tryImplementation(LogFactory::useJdkLogging);</span>
<span class="fc" id="L41"> tryImplementation(LogFactory::useNoLogging);</span>
<span class="fc" id="L42"> }</span>
private LogFactory() {
// disable construction
}
public static Log getLog(Class<?> clazz) {
<span class="fc" id="L49"> return getLog(clazz.getName());</span>
}
public static Log getLog(String logger) {
try {
<span class="fc" id="L54"> return logConstructor.newInstance(logger);</span>
<span class="nc" id="L55"> } catch (Throwable t) {</span>
<span class="nc" id="L56"> throw new LogException("Error creating logger for logger " + logger + ". Cause: " + t, t);</span>
}
}
public static void useCustomLogging(Class<? extends Log> clazz) {
<span class="fc" id="L61"> setImplementation(clazz);</span>
<span class="fc" id="L62"> }</span>
public static void useSlf4jLogging() {
<span class="fc" id="L65"> setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);</span>
<span class="fc" id="L66"> }</span>
public static void useCommonsLogging() {
<span class="fc" id="L69"> setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);</span>
<span class="fc" id="L70"> }</span>
/**
* @deprecated Since 3.5.9 - See https://github.com/mybatis/mybatis-3/issues/1223. This method will remove future.
*/
@Deprecated
public static void useLog4JLogging() {
<span class="fc" id="L77"> setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);</span>
<span class="fc" id="L78"> }</span>
public static void useLog4J2Logging() {
<span class="fc" id="L81"> setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);</span>
<span class="fc" id="L82"> }</span>
public static void useJdkLogging() {
<span class="fc" id="L85"> setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);</span>
<span class="fc" id="L86"> }</span>
public static void useStdOutLogging() {
<span class="fc" id="L89"> setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);</span>
<span class="fc" id="L90"> }</span>
public static void useNoLogging() {
<span class="fc" id="L93"> setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);</span>
<span class="fc" id="L94"> }</span>
private static void tryImplementation(Runnable runnable) {
<span class="fc bfc" id="L97" title="All 2 branches covered."> if (logConstructor == null) {</span>
try {
<span class="fc" id="L99"> runnable.run();</span>
<span class="nc" id="L100"> } catch (Throwable t) {</span>
// ignore
<span class="fc" id="L102"> }</span>
}
<span class="fc" id="L104"> }</span>
private static void setImplementation(Class<? extends Log> implClass) {
<span class="fc" id="L107"> lock.lock();</span>
try {
<span class="fc" id="L109"> Constructor<? extends Log> candidate = implClass.getConstructor(String.class);</span>
<span class="fc" id="L110"> Log log = candidate.newInstance(LogFactory.class.getName());</span>
<span class="fc bfc" id="L111" title="All 2 branches covered."> if (log.isDebugEnabled()) {</span>
<span class="fc" id="L112"> log.debug("Logging initialized using '" + implClass + "' adapter.");</span>
}
<span class="fc" id="L114"> logConstructor = candidate;</span>
<span class="nc" id="L115"> } catch (Throwable t) {</span>
<span class="nc" id="L116"> throw new LogException("Error setting Log implementation. Cause: " + t, t);</span>
} finally {
<span class="fc" id="L118"> lock.unlock();</span>
}
<span class="fc" id="L120"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>