DEV Community

Masui Masanori
Masui Masanori

Posted on

1

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

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more