Bookmark this page

Chapter 7.  Configuring the Logging Subsystem

Abstract

Goal

Configure log handlers and loggers.

Objectives
  • Configure different types of logging handlers.

  • Configure ROOT, and category loggers.

Sections
  • Configuring Logging Handlers (and Guided Exercise)

  • Configuring Loggers (and Guided Exercise)

Lab
  • Configuring the Logging Subsystem

Configuring Logging Handlers

Objectives

  • Configure different types of logging handlers.

Overview of the logging subsystem

Logging is one of the most crucial tasks performed by an application server. Gathering and analyzing log files is an important task for a Red Hat JBoss Enterprise Application Platform (JBoss EAP) administrator to help diagnose, troubleshoot and analyze issues in production. Sometimes, there might also be a business requirement to store logging information from applications to meet regulatory and compliance related laws.

Consistent with the modular design of JBoss EAP, logging is handled by a subsystem. The logging configuration is represented by the logging subsystem, and is configured in the domain.xml in managed domain mode and standalone.xml in a standalone server.

The JBoss EAP logging subsystem has three main components:

  • Handlers: Handlers determine where and how the server logs events. JBoss EAP comes with several handlers out-of-the-box that can be used to log messages from applications.

  • Loggers: Loggers, also called log categories, organize events into logically related categories. A category usually maps to a certain package namespace that is running within the Java Virtual Machine and defines one or more handlers that process and log the messages.

  • Root Logger: Loggers are organized in a parent-child hierarchy and the root logger resides at the top of the logger hierarchy.

A handler is activated only if one or more loggers reference it. Loggers can reference more than one handler or none at all. The root logger captures all log messages of the specified log level, that are not captured by a log category.

The default log level is INFO, and the following two handlers are enabled:

  • CONSOLE: a handler that logs events to the console window

  • FILE: a handler that logs events to a file named server.log

Default Log File Locations

Earlier versions of JBoss EAP had a single log directory. JBoss EAP 7 has multiple log file locations depending on whether it is running as a standalone server or managed domain mode.

For example, if JBoss EAP is running as a standalone server, then the log files are found in the standalone/log directory within the base directory.

The standalone/log directory contains two log files:

  • gc.log.0.current: This file contains logs for the Java Virtual Machine flags that JBoss EAP was started with and memory management events while the server is running. The settings for configuring this file are found in the BASE_DIR/bin/standalone.sh file. The gc.log.0.current only shows log events that occurred during the most recent startup of the server.

  • server.log: This file contains the server's log events. Log events are appended to this file, so typically the server needs to roll this file over based on the file size or length of time, such as hourly or daily. The settings for configuring server.log are found in the logging subsystem section of standalone.xml.

If JBoss EAP is running in managed domain mode, then logging occurs in multiple locations relative to the base directory:

  • domain/log/host-controller.log: This file contains log events during the start of the host controller or domain controller if the host is the domain controller. It also registers events such as lost connection with domain controller messages, newly registered host controller messages, debug messages, and runtime errors in the host controller.

  • domain/log/process-controller.log: This file contains log events related to the starting and stopping of processes on the host. Note that the host controller runs in its own process, and also remember that each server on the host runs in its own process.

  • domain/servers/<server-name>/log/server.log: This file contains this server's log events.

Notice that each server on a host has its own directory for log files. For example, if a host has a server on it named dev-server-one, then the log file for that server is domain/servers/dev-server-one/log/server.log

The <server-name>/log/server.log is analogous to server.log the one from a standalone server, and is a good place to start looking for issues on a server. This file is appended to each time the server starts, so it is a good practice to rotate server.log on an hourly or daily basis, or when the file reaches a certain size.

Note

The default location of the log directories can be changed by setting the jboss.server.log.dir property at runtime. Remember that the jboss.server.log.dir directory is relative to the jboss.domain.base.dir property. If the jboss.domain.base.dir property is defined, a different value for the jboss.server.log.dir property is not needed unless there are specific requirements for the managed domain.

Built-in Log Handlers

JBoss EAP comes with the six following built-in handlers:

  • Console handler: This handler writes to the console. It is configured using the <console-handler> element.

  • File handler: This handler writes to a file. It is configured using the <file-handler> element.

  • Periodic-rotating file handler: This handler writes to a file, rotating the log after a time period derived from the given suffix string. It is configured using the <periodic-rotating-file-handler> element.

  • Size-rotating file handler: This handler writes to a file, rotating the log after the size of the file grows beyond a certain point and keeping a fixed number of backups. It is configured using the <size-rotating-file-handler> element.

  • Asynchronous handler: This handler writes to the sub-handlers in an asynchronous thread. This is useful in scenarios where the application generates a large number of log messages in parallel threads and where the file system's I/O throughput becomes a bottleneck. It is configured using the <async-handler> element.

  • Syslog handler: This handler sends events to a remote syslog server. It is configured using the <syslog-handler> element.

Note

Handlers specify where and how an event is logged, but defining a handler does not automatically turn it on. That is done in either the <logger> or <root-logger> tags, which are discussed later in this chapter.

A custom handler can be developed for logging events to any type of resource and in any format required. To do so, write a Java class that extends the base logging framework API and define a custom-handler in the JBoss EAP configuration file. Writing a custom handler is beyond the scope of this course.

Handlers are configured within the logging subsystem section of the server configuration file. For example, the standalone.xml file and most of the profiles in the domain.xml file come preconfigured with a <console-handler> definition.

The management CLI definition looks like the following:

[standalone@localhost:9990]$ /subsystem=logging/console-handler=CONSOLE:read-resource
{
  "outcome" => "success",
  "result" => {
    "autoflush" => true,
    "enabled" => true,
    "encoding" => undefined,
    "filter" => undefined,
    "filter-spec" => undefined,
    "formatter" => "%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%e%n",
    "level" => "INFO",
    "name" => "CONSOLE",
    "named-formatter" => "COLOR-PATTERN",
    "target" => "System.out"
  }
}

The corresponding XML looks like:

<subsystem xmlns="urn:jboss:domain:logging:8.0">
    <console-handler name="CONSOLE"> 1
        <level name="INFO"/> 2
        <formatter> 3
            <named-formatter name="COLOR-PATTERN"/> 4
        </formatter>
    </console-handler>
    ...output omitted...
</subsystem>

1

The CONSOLE handler can also be configured using the JBoss EAP management console in the ConfigurationSubsystemsLogging section.

2

The <level> element specifies the minimum log level for this handler.

3

The <formatter> element lets you specify a named pattern to use when logging events.

4

The COLOR-PATTERN formatter colorizes the log output for different levels and makes it easy to locate errors when parsing large log files. For example, log messages with ERROR or FATAL level severity are colorized in red to draw the administrator's attention when viewing the logs.

Periodic Rotating File Handlers

Use the periodic-rotating-file-handler to automatically rotate the log files based on a certain amount of elapsed time. For example, the default FILE handler is set to roll over the server.log file every 24 hours. The definition of the handler is as follows:

<periodic-rotating-file-handler name="FILE" autoflush="true"> 1
    <formatter>
        <named-formatter name="PATTERN"/>
    </formatter>
    <file relative-to="jboss.server.log.dir" path="server.log"/> 2
    <suffix value=".yyyy-MM-dd"/> 3
    <append value="true"/>
</periodic-rotating-file-handler>

1

The name of this handler is FILE and is the handler that JBoss EAP uses by default.

2

The value of relative-to is either a Java property or a path element defined in this configuration file. In this case, we are using the predefined property jboss.server.log.dir, which points to the $BASE_DIR/standalone/log directory in a standalone server.

3

The value of suffix determines how often the log file rotates. For example, the suffix yyyy-MM-dd represents a day format. Therefore, this log rotates once a day at midnight.

The suffix value is a date-time format, and the pattern specifies how often the log file rotates. For example, if a log file should rotate every hour, set the suffix to yyyy-MM-dd.HH.

Similarly, monthly rotation is set by using the suffix yyyy-MM.

The periodic-rotating-file-handler is managed by using the management console in the ConfigurationSubsystemsLogging section.

The management CLI can also define a new handler by using the add operation. For example, the following CLI command adds a new periodic-rotating-file-handler called MY_HANDLER:

/profile=default/subsystem=logging/periodic-rotating-file-handler=MY_HANDLER:add(
      autoflush=true,
      append=true,
      named-formatter=PATTERN,
      file={
            path=mylog.log,
            relative-to=jboss.server.log.dir
      },
      level=INFO,
      suffix=.yyyy-DD-mm-HH
)

Size Rotating File Handlers

Use the size-rotating-file-handler to automatically rotate log files based on the size of the log file. This handler is useful in scenarios where the number of log files generated within a period is too large. The handler is configured to rotate the log file after a set rotate-size value.

The following is a sample definition of this handler:

<size-rotating-file-handler name="FILE_BY_SIZE"> 1
   <level name="INFO"/> 2
   <formatter>
        <named-formatter name="PATTERN"/> 3
    </formatter>
   <file relative-to="jboss.server.log.dir" path="production-server.log"/> 4 5
   <rotate-size value="1m"/> 6
   <max-backup-index value="3"/> 7
</size-rotating-file-handler>

1

The name attribute is required and is the name of the handler. In managed domain mode, it must be unique within a profile.

2

The level attribute sets the default log level for this handler. The handler handles all log messages at this level and below in the logger hierarchy.

3

The formatter attribute references a valid named-formatter reference defined in the configuration file. The pattern can be directly provided by using the pattern-formatter tag and defining a pattern template.

4

The relative-to attribute references either the full path where the log file should be created, or it can refer to a logical path reference defined in the configuration file. The default value is the pre-defined property jboss.server.log.dir, which points to the $BASE_DIR/standalone/log directory in a standalone server and $BASE_DIR/domain/servers/<server-name>/log directory in managed domain mode.

5

The path attribute provides a path to the log file which is relative to the relative-to path value. For example, if the path value of mylog.log is provided, then the log file is created at $BASE_DIR/standalone/log/mylog.log in a standalone server and $BASE_DIR/domain/servers/<server-name>/log/mylog.log in managed domain mode.

6

The rotate-size value is the size of the log file after which the handler rotates the file. The name of the rolled-over file is appended with a numeric index. The rotate size value can be given in megabytes by appending the value with an m. For example, to rotate the log file after 5 MB, the rotate-size value is given as 5m.

7

The max-backup-index attribute determines how many backup copies of the rotated log files are maintained before the files are reused. For example, a log file named mylog.log has a max-backup-index value of 3. After the log file reaches the rotate-size value, the handler rotates the log file three times into log files called mylog.log.1, mylog.log.2 and mylog.log.3. On the next rotation, mylog.log is overwritten and the old log messages are purged.

Size rotating file handlers can be defined in the management console in the ConfigurationSubsystemsLoggingConfigurationView section.

Asynchronous Logging

The async-handler provides a simple way to accomplish asynchronous logging, but it does not write log events to a file or the console. Instead, the async-handler sits in front of an existing handler and adds the asynchronous behavior. Use the <subhandler> element to define which handler the asynchronous behavior is being added to.

The following is an example definition for an asynchronous handler:

<async-handler name="ASYNC"> 1
    <level name="INFO"/> 2
    <queue-length value="1024"/> 3
    <overflow-action value="BLOCK"/> 4
    <subhandlers>
        <handler name="FILE"/> 5
    </subhandlers>
</async-handler>

1

The name of the handler is ASYNC.

2

The log level is INFO.

3

The maximum number of log messages that can be held in the queue is 1024.

4

The possible values of overflow-action are BLOCK and DISCARD. If the queue of log events is full when a new log event is attempted to be added, then BLOCK means the current thread blocks and waits for room in the queue, and DISCARD means the log event is simply discarded.

5

The <subhandlers> section can contain multiple handlers. In this case, the asynchronous behavior is added to the default FILE handler.

Adding the asynchronous behavior to logging can improve performance on high-volume servers. If a request is made to the server that causes a log event to occur, a response can be sent back to the client without making the client wait for the actual writing of the log event to the handler.

Syslog Handler

The syslog-handler provides a simple way to send events to a remote logging server. This allows multiple applications to send their log messages to a central SYSLOG server. On a central server, logs can be stored, analyzed, and archived simultaneously to simplify backups.

The following is an example definition of the syslog-handler handler:

<syslog-handler name="SYSLOG"> 1
    <level name="INFO"/> 2
    <server-address value="172.25.2.9"/> 3
    <hostname value="workstation.lab.example.com"/> 4
    <port value="514"/> 5
    <app-name value="MY_APP"/>
</syslog-handler>

1

The name of this handler is SYSLOG.

2

The log level is INFO.

3

This handler sends log messages to the remote logging server located at 172.25.2.9.

4

The name of the host name that the messages are being sent from is set to workstation.lab.example.com to uniquely identify this host because multiple hosts can send SYSLOG messages to the remote server.

5

The remote logging server listens on TCP port 514.

This handler formats the log message according to the RFC5424 (SYSLOG protocol) specification. Any remote server that complies with this specification can accept these log messages. The log messages on the remote server are parsed and filtered based on this attribute.

Revision: ad248-7.4-18a9db2