Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maven-plugin: Support increasing maven log level #4148

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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 @@ -68,6 +69,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 @@ -216,6 +219,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 @@ -1182,6 +1209,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;
}
}
StevenMassaro marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
}