Try Micronaut and Doma2
Tags: java, micronaut, doma
Published: 2024-02-12 17:25:01

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

* [Try Micronaut](https://dev.to/masanori_msl/try-micronaut-41o4)
* [Run a Micronaut application on Tomcat](https://dev.to/masanori_msl/run-a-micronaut-application-on-tomcat-2jbc)

## Stop banner on starting Micronaut applications
I can stop Micronaut banner output by setting the flag to false in "Application.java".

#### Application.java
```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();
    }
}
```

* [Micronaut Banner - Micronaut Framework](https://docs.micronaut.io/latest/guide/#environments)

## 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
```yml
micronaut:
    application:
        name: ourtasks
    router:
        static-resources:
            default:
                enabled: true
                paths: classpath:static
    server:
        context-path: /ourtasks
```

#### application-local.yml
```yml
micronaut:
    server:
        port: 8083
```
### 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
```yml
micronaut:
...
    http:
        client:
            proxy-type: HTTP
            proxy-address: http://proxysample:8082
```

#### [OK]application-local.yml
```yml
micronaut:
...
    http:
        client:
            proxy-type: HTTP
            proxy-address: http://debug-proxysample:8085
```

#### [NG]application-local.yml
```yml
micronaut:
...
    http:
        client:
            proxy-type: HTTP
            # I can't set an empty string, null, and invalid URL.
            proxy-address: ""
```

## [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 午前 
...
```

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

#### DomaConfig.java
```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);
    }
...
}
```

#### logback.xml
```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>
```

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
```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!");
...
    }
...
```

* [SLF4J support - Doma documentation](https://doma.readthedocs.io/en/2.46.1/slf4j-support/)
* [Doma2 でログ出力を制御する - Qiita](https://qiita.com/opengl-8080/items/8769c6981c59e4e1abbd)