Client logs record exceptions that occur during the running of a Message Queue for Apache RocketMQ client, helping rapidly locate and clear these exceptions. This topic describes how to print client logs of Message Queue for Apache RocketMQ, and provides the default and custom log configurations.

Print client logs

The TCP Java SDK for Message Queue for Apache RocketMQ is programmed through the SLF4J API.

For Java SDK 1.7.8.Final or later

Java SDK 1.7.8.Final for Message Queue for Apache RocketMQ has built-in log implementation. Therefore, you do not need to add log implementation dependencies to the client application for printing client logs of Message Queue for Apache RocketMQ.

For more information about the default configuration of client logs and how to modify the default configuration, see the custom configuration.

For Java SDK earlier than version 1.7.8.Final

The Java SDK for Message Queue for Apache RocketMQ of versions earlier than 1.7.8.Final only supports log4j and logback but not log4j2. You need to add corresponding log implementation dependencies to pom.xml or lib to print client logs of Message Queue for Apache RocketMQ.

The sample code for adding the dependency of log4j or logback is as follows:

  • Method 1: Depend on the log4j logging framework

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>                   
  • Method 2: Depend on the logback logging framework

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>                   
Notice If an application depends on both log4j and logback, client logs cannot be printed properly due to log conflicts. To print client logs properly, ensure that the application depends only on one logging framework. We recommend that you run mvn clean dependency:tree | grep log to check the dependency of the logging framework.

Configure client logs

A Message Queue for Apache RocketMQ client allows you to specify the log storage path, log level, and maximum number of historical log files retained. To facilitate log transmission and viewing, Message Queue for Apache RocketMQ retains the default size of 64 MB for a single log file rather than allowing to specify a value.

These parameters are described as follows:

  • Log storage path: Ensure that the application has the write permission for this path. Otherwise, logs cannot be printed.
  • Maximum number of historical log files retained: You can set this parameter to a value in the range from 1 to 100. If you enter a value beyond this range or in an invalid format, the system retains 10 historical log files by default.
  • Log level: The system supports logs of the ERROR, WARN, INFO, and DEBUG levels. If it is not set, the system retains the default value INFO.

Default configuration

After you start the client, the client generates log files based on the following default configuration:

  • Log storage path: /{user.home}/logs/ons.log, where {user.home} is the root directory of the account that runs the current Java process.
  • Maximum number of historical log files retained: 10
  • Log level: INFO
  • Size of a single log file: 64 MB

Custom configuration

Notice To customize the log configuration for the Message Queue for Apache RocketMQ client, upgrade Java SDK to 1.2.5 or later.

To customize the log configuration in Java SDK, set the following system parameters:

  • ons.client.logRoot: log storage path
  • ons.client.logFileMaxIndex: maximum number of historical log files retained
  • ons.client.logLevel: log level

Sample configuration

Add the following system parameters to the startup script or integrated development environment (IDE) virtual machine (VM) options:

  • Sample on Linux
        -Dons.client.logRoot=/home/admin/logs -Dons.client.logLevel=WARN -Dons.client.logFileMaxIndex=20
  • Sample on Windows
        -Dons.client.logRoot=D:\logs -Dons.client.logLevel=WARN -Dons.client.logFileMaxIndex=20

Where, /home/admin/ and D:\ are for reference only. Replace them as needed.