Chapter 2. Enabling JBoss Logging for your application

When you want to use JBoss Logging to collect application logs, you must add a logger to each class that you want to produce log. The following procedure demonstrates how you can add logging to your application programmatically using the API approach or declaratively using annotations.

Procedure

  1. Depending on your application code, use one of the following approaches:

    1. Create an instance of org.jboss.logging.Logger and initialize it by calling the static method Logger.getLogger(Class) for each class:

      src/main/java/org/acme/ExampleResource.java

      import org.jboss.logging.Logger; 1
      
      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      import javax.ws.rs.Produces;
      import javax.ws.rs.core.MediaType;
      
      @Path("/hello")
      public class ExampleResource {
      
          private static final Logger LOG = Logger.getLogger(ExampleResource.class); 2
      
          @GET
          @Produces(MediaType.TEXT_PLAIN)
          public String hello() {
              LOG.info("Hello"); 3
              return "hello";
          }
      }

      1
      Add the org.jboss.logging.Logger import statement for each class namespace that you want to use.
      2
      Add reference to the logger singleton for each class.
      3
      Set a logging level for the log message.
    2. Inject a configured org.jboss.logging.Logger instance in your beans and resource classes:

      src/main/java/org/acme/ExampleResource.java

      import javax.inject.Inject;
      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      import javax.ws.rs.Produces;
      import javax.ws.rs.core.MediaType;
      
      import org.jboss.logging.Logger; 1
      
      import io.quarkus.arc.log.LoggerName;
      
      @Path("/hello")
      public class ExampleResource {
      
          @Inject
          Logger log; 2
      
          @LoggerName("foo")
          Logger fooLog; 3
      
          @GET
          @Produces(MediaType.TEXT_PLAIN)
          public String hello() {
              log.info("Simple!");
              fooLog.info("Goes to foo logger!");
              return "hello";
          }
      }

      1
      Add the org.jboss.logging.Logger import statement for each class namespace that you want to use.
      2
      The fully qualified class name of the declaring class is used as the logger name, which is equivalent to the initialization statement org.jboss.logging.Logger.getLogger(ExampleResource.class).
      3
      Set a name for the logger. In this example, the logger name is foo, which is equivalent to the initialization statement org.jboss.logging.Logger.getLogger("foo").
      Note

      The logger instances are cached internally. A logger, that you inject into a bean, is shared for all bean instances to avoid the possible performance penalty associated with logger instantiation.

  2. (Optional) Configure the logging output in your application.properties file:

    src/main/resources/application.properties

    <configuration_key>=<value>

    For example, you can create a log file and print the output to a console and to the file:

    src/main/resources/application.properties

    quarkus.log.file.enable=true
    quarkus.log.file.path=/tmp/trace.log

  3. Run your application in development mode:

    ./mvnw quarkus:dev
  4. Navigate to http://localhost:8080/hello.
  5. Depending on your configuration, review the log messages on your terminal or in your log file.

    Example output for the ExampleResource.class with logging level set to INFO:

    2021-05-21 15:38:39,751 INFO  [io.quarkus] (Quarkus Main Thread) my-project my-version on JVM (powered by Quarkus 1.13.3.Final) started in 1.189s. Listening on: http://localhost:8080
    2021-05-21 15:38:39,765 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
    2021-05-21 15:38:39,766 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy]
    2021-05-21 15:38:58,790 INFO  [ExampleResource] (executor-thread-1) Hello