• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / generator / 1942

12 Jan 2026 05:01PM UTC coverage: 88.75% (+0.4%) from 88.365%
1942

push

github

web-flow
Merge pull request #1412 from jeffgbutler/jspecify

Adopt JSpecify

2331 of 3162 branches covered (73.72%)

1800 of 1949 new or added lines in 202 files covered. (92.36%)

18 existing lines in 10 files now uncovered.

11384 of 12827 relevant lines covered (88.75%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.37
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/logging/LogFactory.java
1
/*
2
 *    Copyright 2006-2026 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.generator.logging;
17

18
import static org.mybatis.generator.internal.util.messages.Messages.getString;
19

20
import org.jspecify.annotations.Nullable;
21
import org.mybatis.generator.logging.commons.JakartaCommonsLoggingLogFactory;
22
import org.mybatis.generator.logging.jdk14.Jdk14LoggingLogFactory;
23
import org.mybatis.generator.logging.log4j2.Log4j2LoggingLogFactory;
24
import org.mybatis.generator.logging.nologging.NoLoggingLogFactory;
25
import org.mybatis.generator.logging.slf4j.Slf4jLoggingLogFactory;
26

27
/**
28
 * Factory for creating loggers.
29
 *
30
 * @author Jeff Butler
31
 */
32
public class LogFactory {
33
    private static @Nullable AbstractLogFactory theFactory;
34
    public static final String MARKER = "MYBATIS-GENERATOR"; //$NON-NLS-1$
35

36
    static {
37
        tryImplementation(new Slf4jLoggingLogFactory());
1✔
38
        tryImplementation(new JakartaCommonsLoggingLogFactory());
1✔
39
        tryImplementation(new Log4j2LoggingLogFactory());
1✔
40
        tryImplementation(new Jdk14LoggingLogFactory());
1✔
41
        tryImplementation(new NoLoggingLogFactory());
1✔
42
    }
1✔
43

44
    private LogFactory() {
45
    }
46

47
    public static Log getLog(Class<?> clazz) {
48
        if (theFactory == null) {
1!
NEW
49
            throw new RuntimeException(getString("RuntimeError.21", //$NON-NLS-1$
×
NEW
50
                    clazz.getName(), "LogFactory Not Initialized"));
×
51
        }
52

53
        try {
54
            return theFactory.getLog(clazz);
1✔
55
        } catch (Exception t) {
×
56
            throw new RuntimeException(getString("RuntimeError.21", //$NON-NLS-1$
×
57
                    clazz.getName(), t.getMessage()), t);
×
58
        }
59
    }
60

61
    /**
62
     * This method will switch the logging implementation to Java native
63
     * logging. This is useful in situations where you want to use Java native
64
     * logging to log activity but Log4J is on the classpath. Note that this
65
     * method is only effective for log classes obtained after calling this
66
     * method. If you intend to use this method you should call it before
67
     * calling any other method.
68
     */
69
    public static synchronized void forceJavaLogging() {
70
        setImplementation(new Jdk14LoggingLogFactory());
×
71
    }
×
72

73
    public static synchronized void forceSlf4jLogging() {
74
        setImplementation(new Slf4jLoggingLogFactory());
×
75
    }
×
76

77
    public static synchronized void forceCommonsLogging() {
78
        setImplementation(new JakartaCommonsLoggingLogFactory());
×
79
    }
×
80

81
    public static synchronized void forceLog4j2Logging() {
82
        setImplementation(new Log4j2LoggingLogFactory());
×
83
    }
×
84

85
    public static synchronized void forceNoLogging() {
86
        setImplementation(new NoLoggingLogFactory());
×
87
    }
×
88

89
    public static void setLogFactory(AbstractLogFactory logFactory) {
90
        setImplementation(logFactory);
×
91
    }
×
92

93
    private static void tryImplementation(AbstractLogFactory factory) {
94
        if (theFactory == null) {
1✔
95
            try {
96
                setImplementation(factory);
1✔
97
            } catch (LogException e) {
×
98
                // ignore
99
            }
1✔
100
        }
101
    }
1✔
102

103
    private static void setImplementation(AbstractLogFactory factory) {
104
        try {
105
            Log log = factory.getLog(LogFactory.class);
1✔
106
            if (log.isDebugEnabled()) {
1!
107
                log.debug("Logging initialized using '" + factory + "' adapter."); //$NON-NLS-1$ //$NON-NLS-2$
1✔
108
            }
109
            theFactory = factory;
1✔
110
        } catch (Throwable t) {
×
111
            throw new LogException("Error setting Log implementation.  Cause: " + t.getMessage(), t); //$NON-NLS-1$
×
112
        }
1✔
113
    }
1✔
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc