How to configure Logging with Quarkus

Quarkus uses the JBoss Log Manager project as facade for application logging. Therefore, the main configuration options should be familiar to JBoss/WildFly users. Let’s see how to configure most common options.

Quarkus logging in a nutshell

To use Logging with Quarkus you don’t need to include any extra dependency in your project. As a matter of fact, JBoss Log Manager is a transitive dependency that you have in any Quarkus runtime. See this example

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import org.jboss.logging.Logger;

@Path("/hello")
public class GreetingResource {
	private static final Logger LOG = Logger.getLogger(GreetingResource.class);
	
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
    	LOG.info("Called Hello");
        return "Hello RESTEasy";
    }
}

When you request the example endpoint, the INFO message will be printed on the Console:

how to log in quarkus

Te default log level is INFO. The following log levels are available:

  • OFF – Turns off logging.
  • FATAL – A critical service failure/complete inability to service requests of any kind.
  • ERROR – A significant disruption in a request or the inability to service a request.
  • WARN – A non-critical service error or problem that may not require immediate correction.
  • INFO – Default. Service lifecycle events or important related very-low-frequency information.
  • DEBUG – Messages that convey extra information regarding lifecycle or non-request-bound events which may be helpful for debugging.
  • TRACE – Messages that convey extra per-request debugging information that may be very high frequency.
  • ALL – Special level for all messages including custom levels.

How to change the default Log Level

The property you need to use is quarkus.log.level. You can set in application.properties. As an alternative, you can pass it with -Dquarkus.log.level=LEVEL at startup:

quarkus.log.level=DEBUG

In most cases, you won’t need to change the Log Level for all packages available in your application. For example, you can define a log level for a single namespace such as org.jboss.resteasy. Example:

quarkus.log.category."org.jboss.resteasy".level=DEBUG

How to change the Log Format and Output

Quarkus inherits from JBoss Log Manager a set of options to customize the format of logging output. You can read the full list of patterns that you can plug in your log format in the Project Documentation. As an example, here is how to define a custom Console log format which prints the Date – Log Level – ClassName- execution Thread- Log message – CR:

quarkus.log.console.format=%d{HH:mm:ss} %-5p [%c{2.}] (%t) %s%e%n

The above, results in the following output on the Console:

16:33:48 INFO  [or.ac.GreetingResource] (executor-thread-0) Called Hello

On the other hand, if you need to produce the Console logs in JSON format, you can set the following property to true:

quarkus.log.console.json=true

JSON Logging requires an external dependency in your project:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-logging-json</artifactId>
</dependency>

Now, your Console emits logs in JSON Format:

{
   "timestamp":"2022-02-24T16:38:57.423+01:00",
   "sequence":1472,
   "loggerClassName":"org.jboss.logging.Logger",
   "loggerName":"org.acme.GreetingResource",
   "level":"INFO",
   "message":"Called Hello",
   "threadName":"executor-thread-0",
   "threadId":94,
   "mdc":{
      
   },
   "ndc":"",
   "hostName":"fedora",
   "processName":"code-with-quarkus-dev.jar",
  

Finally, to Log on a File, you have to enable the File Log Handler. If you don’t provide a File Path for your logs, Quarkus will write logs in the file quarkus.log. Here is an example configuration:

quarkus.log.file.enable=true
# Send output to a trace.log file under the /tmp directory
quarkus.log.file.path=/home/quarkus/logs/trace.log
quarkus.log.file.level=TRACE

How to Rotate Log files

When logging in a File, you should configure a rotation policy to prevent your log from growing too much. You can do that with the properties quarkus.log.file.rotation.max-file-size and quarkus.log.file.rotation.max-backup-index. The first one will let you define the maximum size of a log before rotating takes place. The latter, the maximum number of backups to keep.

Example:

quarkus.log.file.enable=true
quarkus.log.file.level=INFO
quarkus.log.file.format=%d{HH:mm:ss} %-5p [%c{2.}]] (%t) %s%e%n
quarkus.log.file.rotation.max-file-size=1M
quarkus.log.file.rotation.max-backup-index=100

Using Log4j with Quarkus

You can use a set of external logging frameworks with Quarkus, such as Apache Commons Logging, Log4j, or SLF4j,

It is important to note, that you mustn’t include the external dependency in your project (e.g. log4j:log4j:1.2.17) but the JBoss Log Manager wrapper for that Logger. For example, to use Log4j with Quarkus you need to include the following dependency:

<dependency>
    <groupId>org.jboss.logmanager</groupId>
    <artifactId>log4j2-jboss-logmanager</artifactId> 
</dependency>

With the above configuration in place, you can access Log4j Logger and LogManager API:

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 

@Path("/hello")
public class GreetingResource {
	private static final Logger logger = LogManager.getLogger(GreetingResource.class);
	
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
    	  
    	logger.info("Hello from Log4j2");
        return "Hello RESTEasy";
    }
}

Log4j configuration not picked up by Quarkus

JBoss LogManager facade allows to intercept Logger and LogManager API calls from log4j packages. However, as we are not supposed to include log4j core API in the project, you cannot use directly log4j configuration. For configuration purposes, you need to use the logging configuration options in application.properties

Found the article helpful? if so please follow us on Socials