Skip to content

Commit

Permalink
maven-plugin: Support increasing maven log level (#4148)
Browse files Browse the repository at this point in the history
Co-authored-by: filipe <flautert@liquibase.org>
  • Loading branch information
mensinda and filipelautert committed Aug 21, 2023
1 parent a48efd9 commit f7e0383
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.apache.maven.wagon.authentication.AuthenticationInfo;
import org.liquibase.maven.property.PropertyElement;
Expand Down Expand Up @@ -69,6 +70,8 @@ public abstract class AbstractLiquibaseMojo extends AbstractMojo {
*/
private static final String DEFAULT_FIELD_SUFFIX = "Default";

private MavenLog logCache;

/**
*
* Specifies whether to preserve the case of schemas and catalogs
Expand Down Expand Up @@ -217,6 +220,30 @@ public abstract class AbstractLiquibaseMojo extends AbstractMojo {
*/
@PropertyElement
protected String logging;

/**
* Determines the minimum log level liquibase uses when logging.
* <p>
* Supported values are:
*
* <ul>
* <li>DEBUG</li>
* <li>INFO</li>
* <li>WARNING</li>
* <li>ERROR</li>
* </ul>
*
* The primary use case for this option is to reduce the amount of logs from liquibase, while
* not changing the log level of maven itself, without changing ${maven.home}/conf/logging/simplelogger.properties.
* <p>
* <b>NOTE:</b> The final log level is the <i>maximum</i> of this value and the maven log level.
* Thus, it is not possible to <i>decrease</i> the effective log level with this option.
*
* @parameter property="liquibase.logLevel" default-value="DEBUG"
*/
@PropertyElement
protected String logLevel;

/**
* Specifies the <i>liquibase.properties</i> you want to use to configure Liquibase.
*
Expand Down Expand Up @@ -1202,6 +1229,14 @@ private Map<String, Object> getNativeExecutorProperties() {
return nativeProperties;
}

@Override
public synchronized Log getLog() {
if (logCache == null) {
logCache = new MavenLog(super.getLog(), logLevel);
}
return logCache;
}

/**
* Returns the OutputStream based on whether there is an outputFile provided.
* If no outputFile parameter is provided, defaults to System.out.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package org.liquibase.maven.plugins;

import org.apache.maven.plugin.logging.Log;

public class MavenLog implements Log {

public enum Level {
DEBUG(0),
INFO(1),
WARNING(2),
ERROR(3),
;

private final int level;

Level(int level) {
this.level = level;
}

public boolean isAtLeast(Level other) {
return this.level <= other.level;
}
}

private final Log actualLog;
private final Level level;

public MavenLog(Log actualLog, Level level) {
this.actualLog = actualLog;
this.level = level;
}

public MavenLog(Log actualLog, String level) {
this(actualLog, Level.valueOf(level));
}

// Debug
// =====

@Override
public boolean isDebugEnabled() {
return level.isAtLeast(Level.DEBUG) && actualLog.isDebugEnabled();
}

@Override
public void debug(CharSequence charSequence) {
if (isDebugEnabled()) {
actualLog.debug(charSequence);
}
}

@Override
public void debug(CharSequence charSequence, Throwable throwable) {
if (isDebugEnabled()) {
actualLog.debug(charSequence, throwable);
}
}

@Override
public void debug(Throwable throwable) {
if (isDebugEnabled()) {
actualLog.debug(throwable);
}
}

// Info
// ====

@Override
public boolean isInfoEnabled() {
return level.isAtLeast(Level.INFO) && actualLog.isInfoEnabled();
}

@Override
public void info(CharSequence charSequence) {
if (isInfoEnabled()) {
actualLog.info(charSequence);
}
}

@Override
public void info(CharSequence charSequence, Throwable throwable) {
if (isInfoEnabled()) {
actualLog.info(charSequence, throwable);
}
}

@Override
public void info(Throwable throwable) {
if (isInfoEnabled()) {
actualLog.info(throwable);
}
}

// Warn
// ====

@Override
public boolean isWarnEnabled() {
return level.isAtLeast(Level.WARNING) && actualLog.isWarnEnabled();
}

@Override
public void warn(CharSequence charSequence) {
if (isWarnEnabled()) {
actualLog.warn(charSequence);
}
}

@Override
public void warn(CharSequence charSequence, Throwable throwable) {
if (isWarnEnabled()) {
actualLog.warn(charSequence, throwable);
}
}

@Override
public void warn(Throwable throwable) {
if (isWarnEnabled()) {
actualLog.warn(throwable);
}
}

// Error
// =====

@Override
public boolean isErrorEnabled() {
return level.isAtLeast(Level.ERROR) && actualLog.isErrorEnabled();
}

@Override
public void error(CharSequence charSequence) {
if (isErrorEnabled()) {
actualLog.error(charSequence);
}
}

@Override
public void error(CharSequence charSequence, Throwable throwable) {
if (isErrorEnabled()) {
actualLog.error(charSequence, throwable);
}
}

@Override
public void error(Throwable throwable) {
if (isErrorEnabled()) {
actualLog.error(throwable);
}
}
}

0 comments on commit f7e0383

Please sign in to comment.