Skip to content

Commit

Permalink
chore: fix liquibase logging during tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardas-buc committed Dec 13, 2023
1 parent c918e81 commit 14478f6
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,23 @@
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import liquibase.Liquibase;
import liquibase.Scope;
import liquibase.exception.DatabaseException;
import liquibase.integration.spring.SpringLiquibase;
import liquibase.logging.core.AbstractLogService;
import liquibase.ui.LoggerUIService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -47,6 +54,7 @@
@Slf4j
@Component
public class LiquibaseExecutor extends SpringLiquibase {
private static final Logger LIQUIBASE_LOGGER = LoggerFactory.getLogger("liquibase");
private final PostgresContextualContainer postgresContextualContainer;

public LiquibaseExecutor(PostgresContextualContainer postgresContextualContainer) {
Expand All @@ -64,15 +72,18 @@ public LiquibaseExecutor(PostgresContextualContainer postgresContextualContainer
* Executes changesets.
* Will drop any existing data in database.
*/
@SneakyThrows
public void executeChangesets() {
var stopWatch = StopWatch.createStarted();

if (getDataSource() == null) {
setDataSource(createDataSource());
}
executeUpdate();

log.info("Liquibase schema initialized in {} ms.", stopWatch.getTime(TimeUnit.MILLISECONDS));
Scope.child(createLiquibaseLoggableScope(), () -> {
executeUpdate();
log.info("Liquibase schema initialized in {} ms.", stopWatch.getTime(TimeUnit.MILLISECONDS));
});
}

@SneakyThrows
Expand All @@ -94,4 +105,22 @@ private DataSource createDataSource() {
return new HikariDataSource(config);
}

private Map<String, Object> createLiquibaseLoggableScope() {
final Map<String, Object> scopeValues = new HashMap<>();
scopeValues.put(Scope.Attr.ui.name(), new LoggerUIService());
scopeValues.put(Scope.Attr.logService.name(), new AbstractLogService() {
private final LiquibaseSlf4jLogger logger = new LiquibaseSlf4jLogger(LIQUIBASE_LOGGER);

@Override
public int getPriority() {
return 1;
}

@Override
public liquibase.logging.Logger getLog(Class clazz) {
return logger;
}
});
return scopeValues;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* The MIT License
* <p>
* Copyright (c) 2019- Nordic Institute for Interoperability Solutions (NIIS)
* Copyright (c) 2018 Estonian Information System Authority (RIA),
* Nordic Institute for Interoperability Solutions (NIIS), Population Register Centre (VRK)
* Copyright (c) 2015-2017 Estonian Information System Authority (RIA), Population Register Centre (VRK)
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.niis.xroad.cs.test.container.database;

import liquibase.logging.core.AbstractLogger;
import org.slf4j.Logger;

import java.util.logging.Level;

final class LiquibaseSlf4jLogger extends AbstractLogger {
private final Logger logger;

LiquibaseSlf4jLogger(Logger logger) {
super();
this.logger = logger;
}

@Override
public void log(Level level, String message, Throwable e) {
int levelValue = level.intValue();
if (levelValue <= Level.FINEST.intValue()) {
logger.trace(message, e);
} else if (levelValue <= Level.FINE.intValue()) {
logger.debug(message, e);
} else if (levelValue <= Level.INFO.intValue()) {
logger.info(message, e);
} else if (levelValue <= Level.WARNING.intValue()) {
logger.warn(message, e);
} else {
logger.error(message, e);
}
}

@Override
public void severe(String message) {
if (logger.isErrorEnabled()) {
logger.error(message);
}
}

@Override
public void severe(String message, Throwable e) {
if (logger.isErrorEnabled()) {
logger.error(message, e);
}
}

@Override
public void warning(String message) {
if (logger.isWarnEnabled()) {
logger.warn(message);
}
}

@Override
public void warning(String message, Throwable e) {
if (logger.isWarnEnabled()) {
logger.warn(message, e);
}
}

@Override
public void info(String message) {
if (logger.isInfoEnabled()) {
logger.info(message);
}
}

@Override
public void info(String message, Throwable e) {
if (logger.isInfoEnabled()) {
logger.info(message, e);
}
}

@Override
public void config(String message) {
if (logger.isInfoEnabled()) {
logger.info(message);
}
}

@Override
public void config(String message, Throwable e) {
if (logger.isInfoEnabled()) {
logger.info(message, e);
}
}

@Override
public void fine(String message) {
if (logger.isDebugEnabled()) {
logger.debug(message);
}
}

@Override
public void fine(String message, Throwable e) {
if (logger.isDebugEnabled()) {
logger.debug(message, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ logging:
level:
ROOT: INFO
cucumber: TRACE
liquibase: WARN
liquibase: ERROR
org.springframework: INFO
com.nortal.test: INFO # TRACE is helpful for development

Expand Down

0 comments on commit 14478f6

Please sign in to comment.