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 the verbose attribute to true, 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.

MBG will use Apache Log4J logging if Log4J is in the runtime classpath. See http://logging.apache.org/log4j/ for more information about Log4J. If Log4J is not in the runtime classpath, MBG will use standard Java logging.

If for any reason you prefer to force the use of standard Java logging, even if Log4J is in the runtime classpath, you may specify the -forceJavaLogging command line argument, or specify the following line of code when running MBG from Java:

org.mybatis.generator.logging.LogFactory.forceJavaLogging();

Important: You should specify the above line of code before any other MBG code.

Supplying an Alternate Implementation

If you prefer to use a different logging implementation than Log4J or standard Java logging, you may supply an alternate implementation of the key logging interfaces as follows:

  1. Create an implementation of the org.mybatis.generator.logging.Log interface that implements the key logging methods for you logging implementation of choice.
  2. Create an implementation of the org.mybatis.generator.logging.AbstractLogFactory interface that will return instances of your Log implementation.
  3. Configure MBG to use your new LogFactory by calling the method org.mybatis.generator.logging.LogFactory.setLogFactory(AbstractLogFactory) and supplying an instance of your AbstractLogFactory implementation.

Configuring Log4J Logging

The following is a sample Log4J configuration file:

# Set root logger
log4j.rootLogger=INFO, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p %c - %m%n

# MBG logging configuration...
log4j.logger.org.mybatis.generator=DEBUG

This file will instruct Log4J to write all MBG debug messages to the console. To use this file:

  1. Create a file called log4j.properties in the root of your runtime classpath
  2. Copy the above entries into the new file
  3. Run MBG with the Log4J JAR file also in the runtime classpath.

You should see many log messages in the console.

You may also configure Log4J 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:

  1. 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).
  2. Copy the above entries into the new file
  3. 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.