Logging

Apache Causeway uses log4j2 as its logging framework, configured using the log42j-spring.xml file.

The applib provides a couple of plugins that you might wish to use in your own applications.

DuplicateMessageFilter

If you use an extension such as the Execution Outbox then you might find repeating log messages. The DuplicateMessageFilter detects these and only logs the first of them, continuing to count any duplicates. When a different log message needs to be printed, the filter it prints out a repeat count, and then logs the new message.

For example:

... 07:37:09.992 ...ObjectActionDefault   : Executing: org.apache.isis.extensions.executionoutbox.applib.restapi.OutboxRestApi#pending()
... 07:37:47.865 ...DuplicateMessageFilter: ... repeated 3 times
... 07:37:47.865 ...ObjectActionMixedIn   : Executing: org.apache.isis.extensions.secman.applib.user.menu.MeService#me()
... 07:37:49.984 ...ObjectActionDefault   : Executing: org.apache.isis.extensions.executionoutbox.applib.restapi.OutboxRestApi#pending()

To configure:

log4j-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" packages="org.apache.causeway.applib.log4j2">  (1)

</Configuration>
  ...
  <Appenders>
      <Console name="Console" target="SYSTEM_OUT" follow="true">
          <PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}" />
          <DuplicateMessageFilter/>                                         (2)
      </Console>
      <Console name="DuplicateMessages" target="SYSTEM_OUT" follow="true">  (3)
          <PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}" />
      </Console>
  </Appenders>

  <Loggers>
    <Logger                                                                 (4)
      name="org.apache.causeway.applib.log4j2.DuplicateMessageFilter"
      level="info"
      additivity="false">
        <AppenderRef ref="DuplicateMessages"/>                              (3)
    </Logger>
    ...
  </Loggers>
  ...
</Configuration>
1 add org.apache.causeway.applib.log4j2 to the packages attribute
2 log messages send to the Console appender will have the filter applied to them
3 defines an appender for the filter’s own messages (cannot use Console)
4 instruct the filter’s own messages to use the DuplicateMessages appender. Note that additivity=false.

TruncatingMessageFactory

Most log messages use placeholders: log.info("Executing {} {}", foo, bar); The values of the arguments to these placeholders (`foo, bar) are evaluated using toString() if required.

If this toString() representation is very large, then this could make the log difficult to read. The TruncatingMessageFactory automatically trims all arguments to no more than 128 characters.

Only the arguments for the placeholder are truncated, not the original message. If you want to prevent truncation, simply log a single string, eg log.info(xmlStr)

To use, either specify the -Dlog4j2.messageFactory system property or the LOG4J_MESSAGE_FACTORY environment variable:

-Dlog4j2.messageFactory=org.apache.causeway.applib.log4j2.TruncatingMessageFactory

See log4j docs for further details.

The implementation is not the most efficient. If your application logs many messages, you may want to consider refining it.