Logging Information
MyBatis Generator (MBG) reports logging information in several different ways:
- MBG may generate and display warning messages every time it is run. These messages are meant to inform the user of significant events that may, or may not, require attention. Examples are files being overwritten, non-fatal configuration errors, etc. Warnings are always displayed - regardless of configuration or command line options.
- MBG generates and may, or may not, display progress messages every time it is run.
These messages are meant to inform to user of progress in code generation. These messages
are not displayed by default, but may be enabled by specifying the
-verbose
command line argument. Or, if you are running MBG with the built in Ant task, these messages may be enabled by setting theverbose
attribute totrue
, and then running Ant in verbose mode. - Lastly, MBG will generate tracing (logging) messages for detailed debugging. This page explains how to enable these statements.
In general, MBG will not repeat messages. So if MBG generates a warning, that warning is typically not also logged. In some situations it may be useful to enable logging as well as asking MBG to be verbose with progress messages. This may generate a substantial output, but it will also give a very complete picture of what's happening internally during the MBG run.
By default, MBG will use SLF4J logging if it is on the runtime classpath. See https://www.slf4j.org/ for more information about SLF4J. If SLF4J is not in the runtime classpath, MBG will search for logging implementations in the following order:
- SLF4J
- Jakarta Commons Logging
- Log4J V2
- JDK Standard logging
If for any reason you prefer to force the use of JDK standard logging, even
if other implementations are on the runtime classpath, you may specify the command line
options -forceJavaLogging
.
If you are running MBG from Java, you can also call one of the following methods to force a particular logging implementation:
org.mybatis.generator.logging.LogFactory.forceJavaLogging();
org.mybatis.generator.logging.LogFactory.forceSlf4jLogging();
org.mybatis.generator.logging.LogFactory.forceCommonsLogging();
org.mybatis.generator.logging.LogFactory.forceLog4j2Logging();
org.mybatis.generator.logging.LogFactory.forceNoLogging();
Important: You should call these methods before any other MBG code.
Supplying an Alternate Implementation
If you prefer to use a different logging implementation than one of the build in implementations, you may supply an alternate implementation of the key logging interfaces as follows:
- Create an implementation of the
org.mybatis.generator.logging.Log
interface that implements the key logging methods for you logging implementation of choice. - Create an implementation of the
org.mybatis.generator.logging.AbstractLogFactory
interface that will return instances of yourLog
implementation. - Configure MBG to use your new LogFactory by calling the method
org.mybatis.generator.logging.LogFactory.setLogFactory(AbstractLogFactory)
and supplying an instance of yourAbstractLogFactory
implementation.
Configuring SLF4J Logging
The following is a sample Logback configuration file (Logback is the default implementation of SLF4J):
<configuration> <appender name="Console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <logger name="org.mybatis.generator" level="debug" /> <root level="error"> <appender-ref ref="Console" /> </root> </configuration>
This file will instruct SLF4J to write all MBG debug messages to the console. To use this file:
- Create a file called
logback.xml
in the root of your runtime classpath - Copy the above entries into the new file
- Run MBG with Logback classic as a dependency in your POM.
You should see many log messages in the console.
You may also configure SLF4J in any of the other supported methods if you prefer.
Configuring Java Logging
The following is a sample Java logging configuration file:
# Specify the handlers to create in the root logger # (all loggers are children of the root logger) handlers = java.util.logging.ConsoleHandler # Set the default logging level for the root logger .level = INFO # Set the default logging level for new ConsoleHandler instances java.util.logging.ConsoleHandler.level = ALL # Set the default formatter for new ConsoleHandler instances java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter # Set the default logging level for the logger named org.mybatis.generator org.mybatis.generator.level = FINE
This file will instruct Java to write all MBG debug messages to the console. To use this file:
- Create a file called
logging.properties
(or any file name you prefer). The file can exist anywhere in the file system (for example, in a\temp
directory). - Copy the above entries into the new file
- Run MBG with this VM argument:
-Djava.util.logging.config.file=\temp\logging.properties
(specify whatever actual file name and directory you used)
You should see many log messages in the console.
You may also configure Java logging in any of the other supported methods if you prefer.