Skip to content

Commit

Permalink
Merge pull request #2859 from liquibase/add-log-channels
Browse files Browse the repository at this point in the history
Added whitelist for CLI log channels to include
  • Loading branch information
nvoxland committed May 23, 2022
2 parents d67e4cd + edb548a commit 0a9d6d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -595,16 +595,13 @@ protected Map<String, Object> configureLogging() throws IOException {
private void configureLogging(Level logLevel, File logFile) throws IOException {
configuredLogLevel = logLevel;

System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] %4$s [%2$s] %5$s%6$s%n");

java.util.logging.Logger liquibaseLogger = java.util.logging.Logger.getLogger("liquibase");

final JavaLogService logService = (JavaLogService) Scope.getCurrentScope().get(Scope.Attr.logService, LogService.class);
java.util.logging.Logger liquibaseLogger = java.util.logging.Logger.getLogger("liquibase");
logService.setParent(liquibaseLogger);

System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] %4$s [%2$s] %5$s%6$s%n");

java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger("");

Level cliLogLevel = logLevel;

if (logFile != null) {
Expand All @@ -622,42 +619,30 @@ private void configureLogging(Level logLevel, File logFile) throws IOException {
cliLogLevel = Level.OFF;
}

rootLogger.setLevel(logLevel);
liquibaseLogger.setLevel(logLevel);
final String configuredChannels = LiquibaseCommandLineConfiguration.LOG_CHANNELS.getCurrentValue();
List<String> channels;
if (configuredChannels.equalsIgnoreCase("all")) {
channels = new ArrayList<>(Arrays.asList("", "liquibase"));
} else {
channels = StringUtil.splitAndTrim(configuredChannels, ",");

for (Handler handler : rootLogger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
handler.setLevel(cliLogLevel);
if (logLevel == Level.OFF) {
channels.add("");
}
}

try {
//Attempt to disable the sun.net.www.protocol.http.HttpURLConnection logging, since it is verbose and can contain sensitive info in the headers
//using reflection since it net.sun.* classes are not always available
Class httpConnectionClass = Class.forName("sun.net.www.protocol.http.HttpURLConnection");
final Class<? extends Enum> levelEnum = (Class<? extends Enum>) Class.forName("sun.util.logging.PlatformLogger$Level");

final Method getLoggerMethod = httpConnectionClass.getMethod("getHttpLogger");
final Object httpLogger = getLoggerMethod.invoke(null);

final Method setLevelMethod = httpLogger.getClass().getMethod("setLevel", levelEnum);

Enum offEnum = null;
for (Enum definedEnum : levelEnum.getEnumConstants()) {
if (definedEnum.name().equals("OFF")) {
offEnum = definedEnum;
break;
}
}
if (offEnum == null) {
throw new UnexpectedLiquibaseException("Could not find OFF enum");
for (String channel : channels) {
if (channel.equalsIgnoreCase("all")) {
channel = "";
}

setLevelMethod.invoke(httpLogger, offEnum);
} catch (Throwable e) {
liquibaseLogger.fine("Error disabling system HTTP logging: "+e.getMessage());
java.util.logging.Logger.getLogger(channel).setLevel(logLevel);
}

for (Handler handler : rootLogger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
handler.setLevel(cliLogLevel);
}
}
}

private CommandLine getRootCommand(CommandLine commandLine) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class LiquibaseCommandLineConfiguration implements AutoloadedConfiguratio
public static final ConfigurationDefinition<Boolean> INCLUDE_SYSTEM_CLASSPATH;
public static final ConfigurationDefinition<String> DEFAULTS_FILE;
public static final ConfigurationDefinition<Level> LOG_LEVEL;
public static final ConfigurationDefinition<String> LOG_CHANNELS;
public static final ConfigurationDefinition<File> LOG_FILE;
public static final ConfigurationDefinition<File> OUTPUT_FILE;
public static final ConfigurationDefinition<Boolean> SHOULD_RUN;
Expand Down Expand Up @@ -61,6 +62,10 @@ public class LiquibaseCommandLineConfiguration implements AutoloadedConfiguratio
.setValueHandler(ConfigurationValueConverter.LOG_LEVEL)
.build();

LOG_CHANNELS = builder.define("logChannels", String.class)
.setDefaultValue("liquibase", "Controls which log channels have their level set by the liquibase.logLevel setting. Comma separate multiple values. To set the level of all channels, use 'all'. Example: liquibase,org.mariadb.jdbc")
.build();

LOG_FILE = builder.define("logFile", File.class).build();
OUTPUT_FILE = builder.define("outputFile", File.class).build();

Expand Down

0 comments on commit 0a9d6d8

Please sign in to comment.