[#3761] Add code generator configuration to specify a log level threshold
This commit is contained in:
parent
a39f42399f
commit
8e8a7a9645
@ -89,6 +89,12 @@ public class Plugin extends AbstractMojo {
|
||||
@Parameter
|
||||
private boolean skip;
|
||||
|
||||
/**
|
||||
* The logging threshold.
|
||||
*/
|
||||
@Parameter
|
||||
private org.jooq.util.jaxb.Logging logging;
|
||||
|
||||
/**
|
||||
* The jdbc settings.
|
||||
*/
|
||||
@ -121,6 +127,7 @@ public class Plugin extends AbstractMojo {
|
||||
}
|
||||
|
||||
Configuration configuration = new Configuration();
|
||||
configuration.setLogging(logging);
|
||||
configuration.setJdbc(jdbc);
|
||||
configuration.setGenerator(generator);
|
||||
|
||||
|
||||
@ -70,6 +70,7 @@ import javax.xml.validation.SchemaFactory;
|
||||
|
||||
import org.jooq.Constants;
|
||||
import org.jooq.tools.JooqLogger;
|
||||
import org.jooq.tools.JooqLogger.Level;
|
||||
import org.jooq.tools.StringUtils;
|
||||
import org.jooq.tools.jdbc.JDBCUtils;
|
||||
import org.jooq.util.jaxb.Configuration;
|
||||
@ -196,6 +197,29 @@ public class GenerationTool {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void run(Configuration configuration) throws Exception {
|
||||
if (configuration.getLogging() != null) {
|
||||
switch (configuration.getLogging()) {
|
||||
case TRACE:
|
||||
JooqLogger.globalThreshold(Level.TRACE);
|
||||
break;
|
||||
case DEBUG:
|
||||
JooqLogger.globalThreshold(Level.DEBUG);
|
||||
break;
|
||||
case INFO:
|
||||
JooqLogger.globalThreshold(Level.INFO);
|
||||
break;
|
||||
case WARN:
|
||||
JooqLogger.globalThreshold(Level.WARN);
|
||||
break;
|
||||
case ERROR:
|
||||
JooqLogger.globalThreshold(Level.ERROR);
|
||||
break;
|
||||
case FATAL:
|
||||
JooqLogger.globalThreshold(Level.FATAL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Jdbc j = configuration.getJdbc();
|
||||
org.jooq.util.jaxb.Generator g = configuration.getGenerator();
|
||||
errorIfNull(g, "The <generator/> tag is mandatory.");
|
||||
|
||||
@ -8,21 +8,38 @@
|
||||
<complexType>
|
||||
<all>
|
||||
|
||||
<!--
|
||||
The logging configuration element specifies the code generation
|
||||
logging threshold.
|
||||
-->
|
||||
<element name="logging" type="tns:Logging" minOccurs="0" maxOccurs="1" />
|
||||
|
||||
<!--
|
||||
The JDBC configuration element contains information about how
|
||||
to set up the database connection used for source code generation
|
||||
to set up the database connection used for source code generation.
|
||||
-->
|
||||
<element name="jdbc" type="tns:Jdbc" minOccurs="0" maxOccurs="1" />
|
||||
|
||||
<!--
|
||||
The GENERATOR configuration element contains information about
|
||||
source code generation itself
|
||||
source code generation itself.
|
||||
-->
|
||||
<element name="generator" type="tns:Generator" minOccurs="1" maxOccurs="1" />
|
||||
</all>
|
||||
</complexType>
|
||||
</element>
|
||||
|
||||
<simpleType name="Logging">
|
||||
<restriction base="string">
|
||||
<enumeration value="TRACE"/>
|
||||
<enumeration value="DEBUG"/>
|
||||
<enumeration value="INFO"/>
|
||||
<enumeration value="WARN"/>
|
||||
<enumeration value="ERROR"/>
|
||||
<enumeration value="FATAL"/>
|
||||
</restriction>
|
||||
</simpleType>
|
||||
|
||||
<complexType name="Jdbc">
|
||||
<all>
|
||||
<!-- The JDBC driver -->
|
||||
|
||||
@ -40,8 +40,6 @@
|
||||
*/
|
||||
package org.jooq.tools;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* The jOOQ logger abstraction.
|
||||
* <p>
|
||||
@ -60,6 +58,11 @@ import java.util.logging.Level;
|
||||
*/
|
||||
public final class JooqLogger {
|
||||
|
||||
/**
|
||||
* The global logger threshold.
|
||||
*/
|
||||
private static volatile Level globalThreshold = Level.TRACE;
|
||||
|
||||
/**
|
||||
* The SLF4j Logger instance, if available.
|
||||
*/
|
||||
@ -145,18 +148,16 @@ public final class JooqLogger {
|
||||
* Check if <code>TRACE</code> level logging is enabled.
|
||||
*/
|
||||
public boolean isTraceEnabled() {
|
||||
if (!supportsTrace) {
|
||||
if (!globalThreshold.supports(Level.TRACE))
|
||||
return false;
|
||||
}
|
||||
else if (slf4j != null) {
|
||||
else if (!supportsTrace)
|
||||
return false;
|
||||
else if (slf4j != null)
|
||||
return slf4j.isTraceEnabled();
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
return log4j.isTraceEnabled();
|
||||
}
|
||||
else {
|
||||
return util.isLoggable(Level.FINER);
|
||||
}
|
||||
else
|
||||
return util.isLoggable(java.util.logging.Level.FINER);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -175,15 +176,14 @@ public final class JooqLogger {
|
||||
* @param details The message details (padded to a constant-width message)
|
||||
*/
|
||||
public void trace(Object message, Object details) {
|
||||
if (slf4j != null) {
|
||||
if (!globalThreshold.supports(Level.TRACE))
|
||||
return;
|
||||
else if (slf4j != null)
|
||||
slf4j.trace(getMessage(message, details));
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
log4j.trace(getMessage(message, details));
|
||||
}
|
||||
else {
|
||||
else
|
||||
util.finer("" + getMessage(message, details));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -206,33 +206,30 @@ public final class JooqLogger {
|
||||
* message
|
||||
*/
|
||||
public void trace(Object message, Object details, Throwable throwable) {
|
||||
if (slf4j != null) {
|
||||
if (!globalThreshold.supports(Level.TRACE))
|
||||
return;
|
||||
else if (slf4j != null)
|
||||
slf4j.trace(getMessage(message, details), throwable);
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
log4j.trace(getMessage(message, details), throwable);
|
||||
}
|
||||
else {
|
||||
util.log(Level.FINER, "" + getMessage(message, details), throwable);
|
||||
}
|
||||
else
|
||||
util.log(java.util.logging.Level.FINER, "" + getMessage(message, details), throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if <code>DEBUG</code> level logging is enabled.
|
||||
*/
|
||||
public boolean isDebugEnabled() {
|
||||
if (!supportsDebug) {
|
||||
if (!globalThreshold.supports(Level.DEBUG))
|
||||
return false;
|
||||
}
|
||||
else if (slf4j != null) {
|
||||
else if (!supportsDebug)
|
||||
return false;
|
||||
else if (slf4j != null)
|
||||
return slf4j.isDebugEnabled();
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
return log4j.isDebugEnabled();
|
||||
}
|
||||
else {
|
||||
return util.isLoggable(Level.FINE);
|
||||
}
|
||||
else
|
||||
return util.isLoggable(java.util.logging.Level.FINE);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -251,15 +248,14 @@ public final class JooqLogger {
|
||||
* @param details The message details (padded to a constant-width message)
|
||||
*/
|
||||
public void debug(Object message, Object details) {
|
||||
if (slf4j != null) {
|
||||
if (!globalThreshold.supports(Level.DEBUG))
|
||||
return;
|
||||
else if (slf4j != null)
|
||||
slf4j.debug(getMessage(message, details));
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
log4j.debug(getMessage(message, details));
|
||||
}
|
||||
else {
|
||||
else
|
||||
util.fine("" + getMessage(message, details));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -282,33 +278,30 @@ public final class JooqLogger {
|
||||
* message
|
||||
*/
|
||||
public void debug(Object message, Object details, Throwable throwable) {
|
||||
if (slf4j != null) {
|
||||
if (!globalThreshold.supports(Level.DEBUG))
|
||||
return;
|
||||
else if (slf4j != null)
|
||||
slf4j.debug(getMessage(message, details), throwable);
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
log4j.debug(getMessage(message, details), throwable);
|
||||
}
|
||||
else {
|
||||
util.log(Level.FINE, "" + getMessage(message, details), throwable);
|
||||
}
|
||||
else
|
||||
util.log(java.util.logging.Level.FINE, "" + getMessage(message, details), throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if <code>INFO</code> level logging is enabled.
|
||||
*/
|
||||
public boolean isInfoEnabled() {
|
||||
if (!supportsInfo) {
|
||||
if (!globalThreshold.supports(Level.INFO))
|
||||
return false;
|
||||
}
|
||||
else if (slf4j != null) {
|
||||
if (!supportsInfo)
|
||||
return false;
|
||||
else if (slf4j != null)
|
||||
return slf4j.isInfoEnabled();
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
return log4j.isInfoEnabled();
|
||||
}
|
||||
else {
|
||||
return util.isLoggable(Level.INFO);
|
||||
}
|
||||
else
|
||||
return util.isLoggable(java.util.logging.Level.INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -327,15 +320,14 @@ public final class JooqLogger {
|
||||
* @param details The message details (padded to a constant-width message)
|
||||
*/
|
||||
public void info(Object message, Object details) {
|
||||
if (slf4j != null) {
|
||||
if (!globalThreshold.supports(Level.INFO))
|
||||
return;
|
||||
else if (slf4j != null)
|
||||
slf4j.info(getMessage(message, details));
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
log4j.info(getMessage(message, details));
|
||||
}
|
||||
else {
|
||||
else
|
||||
util.info("" + getMessage(message, details));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -358,15 +350,14 @@ public final class JooqLogger {
|
||||
* message
|
||||
*/
|
||||
public void info(Object message, Object details, Throwable throwable) {
|
||||
if (slf4j != null) {
|
||||
if (!globalThreshold.supports(Level.INFO))
|
||||
return;
|
||||
else if (slf4j != null)
|
||||
slf4j.info(getMessage(message, details), throwable);
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
log4j.info(getMessage(message, details), throwable);
|
||||
}
|
||||
else {
|
||||
util.log(Level.INFO, "" + getMessage(message, details), throwable);
|
||||
}
|
||||
else
|
||||
util.log(java.util.logging.Level.INFO, "" + getMessage(message, details), throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -385,15 +376,14 @@ public final class JooqLogger {
|
||||
* @param details The message details (padded to a constant-width message)
|
||||
*/
|
||||
public void warn(Object message, Object details) {
|
||||
if (slf4j != null) {
|
||||
if (!globalThreshold.supports(Level.WARN))
|
||||
return;
|
||||
else if (slf4j != null)
|
||||
slf4j.warn(getMessage(message, details));
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
log4j.warn(getMessage(message, details));
|
||||
}
|
||||
else {
|
||||
else
|
||||
util.warning("" + getMessage(message, details));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -416,15 +406,14 @@ public final class JooqLogger {
|
||||
* message
|
||||
*/
|
||||
public void warn(Object message, Object details, Throwable throwable) {
|
||||
if (slf4j != null) {
|
||||
if (!globalThreshold.supports(Level.WARN))
|
||||
return;
|
||||
else if (slf4j != null)
|
||||
slf4j.warn(getMessage(message, details), throwable);
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
log4j.warn(getMessage(message, details), throwable);
|
||||
}
|
||||
else {
|
||||
util.log(Level.WARNING, "" + getMessage(message, details), throwable);
|
||||
}
|
||||
else
|
||||
util.log(java.util.logging.Level.WARNING, "" + getMessage(message, details), throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -443,15 +432,14 @@ public final class JooqLogger {
|
||||
* @param details The message details (padded to a constant-width message)
|
||||
*/
|
||||
public void error(Object message, Object details) {
|
||||
if (slf4j != null) {
|
||||
if (!globalThreshold.supports(Level.ERROR))
|
||||
return;
|
||||
else if (slf4j != null)
|
||||
slf4j.error(getMessage(message, details));
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
log4j.error(getMessage(message, details));
|
||||
}
|
||||
else {
|
||||
else
|
||||
util.severe("" + getMessage(message, details));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -474,15 +462,14 @@ public final class JooqLogger {
|
||||
* message
|
||||
*/
|
||||
public void error(Object message, Object details, Throwable throwable) {
|
||||
if (slf4j != null) {
|
||||
if (!globalThreshold.supports(Level.ERROR))
|
||||
return;
|
||||
else if (slf4j != null)
|
||||
slf4j.error(getMessage(message, details), throwable);
|
||||
}
|
||||
else if (log4j != null) {
|
||||
else if (log4j != null)
|
||||
log4j.error(getMessage(message, details), throwable);
|
||||
}
|
||||
else {
|
||||
util.log(Level.SEVERE, "" + getMessage(message, details), throwable);
|
||||
}
|
||||
else
|
||||
util.log(java.util.logging.Level.SEVERE, "" + getMessage(message, details), throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -500,4 +487,28 @@ public final class JooqLogger {
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a global level threshold to all JooqLoggers.
|
||||
*/
|
||||
public static void globalThreshold(Level level) {
|
||||
globalThreshold = level;
|
||||
}
|
||||
|
||||
/**
|
||||
* The log level.
|
||||
*/
|
||||
public static enum Level {
|
||||
|
||||
TRACE,
|
||||
DEBUG,
|
||||
INFO,
|
||||
WARN,
|
||||
ERROR,
|
||||
FATAL;
|
||||
|
||||
public boolean supports(Level level) {
|
||||
return ordinal() <= level.ordinal();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user