DEV Community

Masui Masanori
Masui Masanori

Posted on

Try Micronaut and Doma2

Intro

This time, I will try changing the default settings of Micronaut and using Doma2.

Stop banner on starting Micronaut applications

I can stop Micronaut banner output by setting the flag to false in "Application.java".

Application.java

package jp.masanori;

import io.micronaut.runtime.Micronaut;

public class Application {

    public static void main(String[] args) {
        Micronaut.build(args)
                .mainClass(Application.class)
                .banner(false)
                .start();
    }
}
Enter fullscreen mode Exit fullscreen mode

Using application.yml for debugging

I want to set the port number to 8083 only when debugging.

To do this, I can add "application-local.yml" and add "micronaut.environments" for "gradlew run".

application.yml

micronaut:
    application:
        name: ourtasks
    router:
        static-resources:
            default:
                enabled: true
                paths: classpath:static
    server:
        context-path: /ourtasks
Enter fullscreen mode Exit fullscreen mode

application-local.yml

micronaut:
    server:
        port: 8083
Enter fullscreen mode Exit fullscreen mode

Overwrite

I can overwrite the "application.yml" values by same names.
However, I couldn't find a way to configure it if, for example, I wanted to use an HTTP proxy only in a production environment.

application.yml

micronaut:
...
    http:
        client:
            proxy-type: HTTP
            proxy-address: http://proxysample:8082
Enter fullscreen mode Exit fullscreen mode

[OK]application-local.yml

micronaut:
...
    http:
        client:
            proxy-type: HTTP
            proxy-address: http://debug-proxysample:8085
Enter fullscreen mode Exit fullscreen mode

[NG]application-local.yml

micronaut:
...
    http:
        client:
            proxy-type: HTTP
            # I can't set an empty string, null, and invalid URL.
            proxy-address: ""
Enter fullscreen mode Exit fullscreen mode

[Doma2] Change log level

By default, when I access database by Doma2, some debug logs will be output like below.

2月 13, 2024 12:01:21 午前 org.seasar.doma.jdbc.tx.LocalTransaction begin
情報: [DOMA2063] The local transaction "1066372655" is begun.
2月 13, 2024 12:01:21 午前 jp.masanori.appusers.dao.AppUserDaoImpl findAllUsers
情報: [DOMA2220] ENTER  : CLASS=jp.masanori.appusers.dao.AppUserDaoImpl, METHOD=findAllUsers
2月 13, 2024 12:01:22 午前 jp.masanori.appusers.dao.AppUserDaoImpl findAllUsers
情報: [DOMA2076] SQL LOG : PATH=[jp.masanori.appusers.dao.AppUserDao#findAllUsers],
SELECT * FROM app_user
2月 13, 2024 12:01:22 午前 jp.masanori.appusers.dao.AppUserDaoImpl findAllUsers
情報: [DOMA2221] EXIT   : CLASS=jp.masanori.appusers.dao.AppUserDaoImpl, METHOD=findAllUsers
2月 13, 2024 12:01:22 午前 jp.masanori.tasks.dao.TaskDaoImpl findAllStatuses
情報: [DOMA2220] ENTER  : CLASS=jp.masanori.tasks.dao.TaskDaoImpl, METHOD=findAllStatuses
2月 13, 2024 12:01:22 午前 
...
Enter fullscreen mode Exit fullscreen mode

In release builds, I want to stop log output, so change the log level.

DomaConfig.java

...
@Singleton
public class DomaConfig implements Config {

    private final LocalTransactionDataSource dataSource;

    private final LocalTransactionManager transactionManager;

    public DomaConfig() {
        LocalTransactionDataSource dataSource;
...
        this.dataSource = dataSource;
        this.transactionManager = new LocalTransactionManager(
                this.dataSource.getLocalTransaction(getJdbcLogger()));
    }

    // Add this to change the log level
    @Override
    public JdbcLogger getJdbcLogger() {
        return new UtilLoggingJdbcLogger(Level.FINE);
    }
...
}
Enter fullscreen mode Exit fullscreen mode

logback.xml

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.seasar.doma.jdbc.LogKind" level="debug"/>

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>
Enter fullscreen mode Exit fullscreen mode

By default, the log level of Doma2 is set as "INFO".
I can stop outputting logs if I don't want them to be output by changing Doma2's log level settings or the log level output by my application.

I also can get a logger instance in a similar way to NLog.

TaskController.java

...
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
....
@Controller("/tasks")
public class TaskController {
    private final Logger logger;
    private final TaskService tasks;

    public TaskController(TaskService tasks) {
        this.logger = LoggerFactory.getLogger(TaskController.class);
        logger.debug("Hello world!");
...
    }
...
Enter fullscreen mode Exit fullscreen mode

Top comments (0)