[Play 2.5] Logging doesn't work in modules

I’m using the Logger class in my module to schedule some tasks but nothing is shown in my console.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class Task {

    private final ActorSystem actorSystem;
    private final ExecutionContext executionContext;
    private final Logger log;

    @Inject
    public Task(ActorSystem actorSystem, ExecutionContext executionContext) {
        this.actorSystem = actorSystem;
        this.executionContext = executionContext;
        this.log = LoggerFactory.getLogger(getClass());
        this.init();
    }

    private void init() {
        actorSystem.scheduler().schedule(
            Duration.create(5, TimeUnit.SECONDS),
            Duration.create(5, TimeUnit.SECONDS),
            this::doTask,
            executionContext
        );
    }

    private void doTask() {
        log.info("Do my task.");
    }
}

public class MyModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(Task.class).asEagerSingleton();
    }
}

If I replace the log.info() call by a System.out.println() I can see that the task is run.

When you create a custom logger like this one, the best practice is to add an entry to conf/logback.xml. In your case, assuming the class Task is inside a package modules, you would could add the following.

<logger name="modules.Task" level="DEBUG"/>

or if you have multiple class in the package modules, you can just add

<logger name="modules" level="DEBUG"/>

Then just set the log level you want.

1 Like