Maksym Ivanov
Maksym Ivanov

Reputation: 35

Liquibase log level changing

I use code from https://www.liquibase.org/blog/3-ways-to-run-liquibase to run updates on DB. It works but produces a lot of logs. I tried to change the log level for Liquibase using

DefaultLoggerConfiguration defaultLoggerConfiguration = new DefaultLoggerConfiguration();
defaultLoggerConfiguration.setLogLevel("off");

but it doesn't help.

pom.xml

<properties>
        <mockito.version>3.3.3</mockito.version>
        <junit.version>5.6.2</junit.version>
        <mysql.connector.version>8.0.20</mysql.connector.version>
        <liquibase.version>3.9.0</liquibase.version>
        <snakeyaml.version>1.26</snakeyaml.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>${mockito.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>${liquibase.version}</version>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>${snakeyaml.version}</version>
        </dependency>
    </dependencies>

May I reduce logs number without any additional dependencies?

Example of app https://github.com/Wismut/liquibase_h2_example

Upvotes: 0

Views: 3606

Answers (1)

Willis Blackburn
Willis Blackburn

Reputation: 8204

WRONG ANSWER, see UPDATE section below

I think that you're trying to configure the Liquibase log level from Java code, as opposed to from Maven or something like that.

Your code doesn't work because you're creating a DefaultLoggerConfiguration object and setting a property but not making it available to Liquibase. It's just a random Java object and will go away as soon as defaultLoggerConfiguration goes out of scope.

It looks like the right way to do this is to ask Liquibase to give you a reference to its configuration, then set the log level using that object.

LiquibaseConfiguration.getInstance()
    .getConfiguration(DefaultLoggerConfiguration.class)
    .setLogLevel("off");

UPDATE

Okay looks like that was totally wrong. The Liquibase logger is apparently a red herring. Possibly there was a way to configure it before, but now it just unconditionally delegates to SLF4J, which in your example on GitHub is using Logback as the actual logger.

By adding a file src/main/resources/logback.xml with the following contents I eliminated all but the info messages from your example.

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <logger name="liquibase" level="info"/>
    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration> 

See http://logback.qos.ch/ for more information on how to configure Logback. Dropping a logback.xml file into the classpath is one way to configure, but you can use other strategies too.

Upvotes: 1

Related Questions