liquibase.logging.core.DefaultLogger Maven / Gradle / Ivy
package liquibase.logging.core;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.logging.LogLevel;
import liquibase.util.StringUtils;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DateFormat;
import java.util.Date;
public class DefaultLogger extends AbstractLogger {
private String name = "liquibase";
private PrintStream err = System.err;
public DefaultLogger() {
}
@Override
public int getPriority() {
return 1;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public LogLevel getLogLevel() {
LogLevel logLevel = super.getLogLevel();
if (logLevel == null) {
return toLogLevel(LiquibaseConfiguration.getInstance().getConfiguration(DefaultLoggerConfiguration.class).getLogLevel());
} else {
return logLevel;
}
}
@Override
public void setLogLevel(String logLevel, String logFile) {
setLogLevel(logLevel);
if (logFile != null) {
File log = new File(logFile);
try {
if (!log.exists()) {
if (!log.createNewFile()) {
throw new RuntimeException("Could not create logFile "+log.getAbsolutePath());
}
}
err = new PrintStream(log);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override
public void severe(String message) {
if (getLogLevel().compareTo(LogLevel.SEVERE) <=0) {
print(LogLevel.SEVERE, message);
}
}
protected void print(LogLevel logLevel, String message) {
if (StringUtils.trimToNull(message) == null) {
return;
}
err.println(logLevel + " " + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date()) + ": " + name + ": " + buildMessage(message));
}
@Override
public void severe(String message, Throwable e) {
if (getLogLevel().compareTo(LogLevel.SEVERE) <=0) {
print(LogLevel.SEVERE, message);
e.printStackTrace(err);
}
}
@Override
public void warning(String message) {
if (getLogLevel().compareTo(LogLevel.WARNING) <=0) {
print(LogLevel.WARNING, message);
}
}
@Override
public void warning(String message, Throwable e) {
if (getLogLevel().compareTo(LogLevel.WARNING) <=0) {
print(LogLevel.WARNING, message);
e.printStackTrace(err);
}
}
@Override
public void info(String message) {
if (getLogLevel().compareTo(LogLevel.INFO) <=0) {
print(LogLevel.INFO, message);
}
}
@Override
public void info(String message, Throwable e) {
if (getLogLevel().compareTo(LogLevel.INFO) <=0) {
print(LogLevel.INFO, message);
e.printStackTrace(err);
}
}
@Override
public void debug(String message) {
if (getLogLevel().compareTo(LogLevel.DEBUG) <=0) {
print(LogLevel.DEBUG, message);
}
}
@Override
public void debug(String message, Throwable e) {
if (getLogLevel().compareTo(LogLevel.DEBUG) <=0) {
print(LogLevel.DEBUG, message);
e.printStackTrace(err);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy