Este contenido no está disponible en el idioma seleccionado.
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
Depending on your application code, use one of the following approaches:
Create an instance of
org.jboss.logging.Loggerand initialize it by calling the static methodLogger.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"; } }Inject a configured
org.jboss.logging.Loggerinstance 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.Loggerimport 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").
NoteThe 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.
(Optional) Configure the logging output in your
application.propertiesfile: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.logRun your application in development mode:
./mvnw quarkus:dev-
Navigate to
http://localhost:8080/hello. Depending on your configuration, review the log messages on your terminal or in your log file.
Example output for the
ExampleResource.classwith logging level set toINFO: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