Query Result Logging

As an extension to audit logging, Cadenza can be configured to log all data view queries and the results returned from the database. Similar to audit logging, events are written to a dedicated Log4j logger and can be directed to any appender. Usually, these events should be directed to a separate log sink than the regular application log.

Requirements and Configuration

To log all data view queries and their results:

Query Result Logging Concepts

There are five kinds of events that can be written to the query result log. The events pertaining to a specific query can then be correlated by their common query execution / result id.

queryStarted

A query has been submitted to the data source. The event contains details about the query, such as the data source identifier, the query text, and arguments.

queryDataReceived

The data source has provided some results to the query, and the event contains the result data. Result data may be spread across several events of this type; each event contains at max the number of rows specified for the data chunk size.

queryCanceled

The user has canceled query execution.

queryFailed

The data source has responded with an error to the query. The event contains a short form of the error message.

queryResultRetrieved

A user has retrieved the results of a query. The event contains information about the user.

The logging depth specifies which of these events are actually written. It can be defined both for the whole Cadenza instance and as an override for each data source. There are three logging depths available:

NONE

No events are logged. This is both the system default, i.e., without any configuration, no events are logged, but can also be configured as an override for a data source where query result logging is not needed.

QUERY_ONLY

Events are logged for the start of a query and the retrieval of its result, but not for the actual result data received from the data source.

FULL

Events are logged for the start of a query, the result data received from the data source, and the retrieval of query results.

Overview of events written by logging depth
Event type NONE QUERY_ONLY FULL

queryStarted

queryDataReceived

queryCanceled

queryFailed

queryResultRetrieved

Enabling Query Result Logging

In Cadenza configuration, query result logging is enabled as part of the auditlogging Configuration. As a baseline, it is possible to set a default query result logging depth, which can be further overridden. Moreover, the Log4j system should be configured to direct messages to the cadenza.audit.QueryResult to a dedicated appender.

The following example configuration

  • enables query result logging globally,

  • reduces the logging depth to QUERY_ONLY for the cross-repository data source with the id "cross_repo_foo", and

  • disables query result logging for data source "GG2YT4rMXbPEYFDvQWVm" in repository "1XJjk4RKiJJT2nF2U5bX", which happens to be the "Personal DB" from the disy demo repository.

A minimal configuration would omit the data source overrides.

  • XML

  • YAML

  • Environment Variables

auditlogging-config.xml configuration file
<?xml version="1.0" encoding="UTF-8"?>
<auditLoggingConfiguration>
  <queryResultLogging>
    <defaultDepth>FULL</defaultDepth>
    <dataSources>
      <dataSource>
        <dataSourceId>cross_repo_foo</dataSourceId>
        <depth>QUERY_ONLY</depth>
      </dataSource>
      <dataSource>
        <repositoryDataSourceId>
          <repository>1XJjk4RKiJJT2nF2U5bX</repository>
          <dataSource>GG2YT4rMXbPEYFDvQWVm</dataSource>
        </repositoryDataSourceId>
        <depth>OFF</depth>
      </dataSource>
    </dataSources>
  </queryResultLogging>
</auditLoggingConfiguration>
myConfig.yaml
cadenzaconfig:
  settings:
    queryResultLogging:
      defaultDepth: "FULL"
      dataSources:
        - dataSourceId: "cross_repo_foo"
          depth: "QUERY_ONLY"
        - repositoryDataSourceId:
            repository: "1XJjk4RKiJJT2nF2U5bX"
            dataSource: "GG2YT4rMXbPEYFDvQWVm"
          depth: "OFF"
cadenzaconfig_settings_queryResultLogging_defaultDepth=FULL
cadenzaconfig_settings_queryResultLogging_dataSources_0_dataSourceId=cross_repo_foo
cadenzaconfig_settings_queryResultLogging_dataSources_0_depth=QUERY_ONLY
cadenzaconfig_settings_queryResultLogging_dataSources_1_repositoryDataSourceId_repository=1XJjk4RKiJJT2nF2U5bX
cadenzaconfig_settings_queryResultLogging_dataSources_1_repositoryDataSourceId_dataSource=GG2YT4rMXbPEYFDvQWVm
cadenzaconfig_settings_queryResultLogging_dataSources_1_depth=OFF

Configuring Log4j for Query Result Logging

When Cadenza starts logging query result events, these log events need to be formatted accordingly and directed to the intended sink. Similar to general audit logging, the query result events are logged as a Log4j MapMessage. For structured log analysis, it is advisable to write these messages using a JsonTemplateLayout.

The example below is self-contained. However, it specifies little for other aspects of the application log. In a production environment, the relevant parts for query result logging need to be integrated into the main log config.

Log4j supports not just XML-based configuration, but YAML and other formats, too. This documentation only shows the XML approach, others would be similar.

A simple log4j2.xml configuration file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration strict="true">

  <Appenders>
    <Appender type="Console" name="console"> (1)
      <Layout type="PatternLayout" pattern="[%p] [%d{yyyy.MM.dd-HH:mm:ss}] [%t] %-25.30c - %m%n" />
    </Appender>

    <Appender
        name="queryResultAppender" type="RollingFile"
        fileName="logs/queryResult.log" filePattern="logs/queryResult.%i.log"> (2)
      <OnStartupTriggeringPolicy/>
      <DefaultRolloverStrategy max="5"/>
      <Layout
          type="JsonTemplateLayout"
          eventTemplateUri="classpath:audit/queryresult/Log4j2-QueryResultJsonLayout.json" (3)
          maxStringLength="16777216" (4)
          stackTraceEnabled="false"/>
    </Appender>
  </Appenders>

  <Loggers>
    <Root level="warn"> (5)
      <AppenderRef ref="console"/>
    </Root>

    <Logger name="cadenza.audit.QueryResult" level="info" additivity="false"> (6)
      <AppenderRef ref="queryResultAppender"/>
    </Logger>
  </Loggers>

</Configuration>
1 A rather regular appender for application log, note the use of a PatternLayout, where %m references the full log message text. In the messages produced for audit and query result logging, it is advisable to reference individual message fields directly.
2 In contrast, the appender for the query result log. In this case, we use a rolling file appender, which can be configured with different file naming and rollover strategies. In this case, on each Cadenza startup, the last logfile is renamed to contain number in the name, and a new file logs/queryResult.log is created.
3 Within that appender, the layout defines how the log messages are formatted. See Defining a JSON Template Layout below for details.
4 By default, each string value is truncated after 16 KiB. This would remove some of the log information, especially for complex geometries which are logged as Base64-encoded well-known binary.

As Log4j eagerly allocates this to a buffer, the size should be chosen to be larger than any single datum to be logged, but not so large as to impact application operation. The example uses a limit of 16 MiB. See the documentation for maxStringLength, too.

5 The root logger definition sets the general log level to WARN and directs all messages to the console appender.
6 The logger definition for query result logging sets the log level to INFO, which means that these events are actually processed. Together with the local <AppenderRef …/> that references the previously defined appender, additivity="false" has the effect that these log messages do not end up in the main application log controlled by the root logger.

Defining a JSON Template Layout

As the audit and query result messages are map-based, the expectation is that a JSON template layout is the best fit to preserve the structure for log analysis. In the example, we use a layout distributed with Cadenza that translates the log message keys slightly. However, it is also possible to define your own message layout in a similar file, or to define it inline in the layout definition using <EventTemplateAdditionalField …/> elements completely.

Click to see the example JSON template contents
audit/queryresult/Log4j2-QueryResultJsonLayout.json
{
  "query_result_id": {
    "$resolver": "map",
    "key": "queryResultId"
  },
  "timestamp": {
    "$resolver": "map",
    "key": "timestamp"
  },
  "event_type": {
    "$resolver": "map",
    "key": "eventType"
  },
  "data_source_id": {
    "$resolver": "map",
    "key": "dataSourceId"
  },
  "query_string": {
    "$resolver": "map",
    "key": "queryString"
  },
  "query_arguments": {
    "$resolver": "map",
    "key": "queryArguments"
  },
  "query_result": {
    "$resolver": "map",
    "key": "queryResult"
  },
  "request_id": {
    "$resolver": "map",
    "key": "requestId"
  },
  "acting_user_name": {
    "$resolver": "map",
    "key": "actingUser"
  }
}

The following template keys are available:

requestId

The request identifier used in general audit logging, too. Only set for events of type queryResultRetrieved.

timestamp

The time the event occurred.

queryResultId

A correlation identifier for events related to the same query execution and its result.

eventType

The type of event.

dataSourceId

The identifier of the data source. Only set for events of type queryStarted.

queryString

The query string. Only set for events of type queryStarted.

queryArguments

The query arguments. Only set for events of type queryStarted.

queryResult

The query result. Only set for events of type queryDataReceived.

exceptionMessage

The exception message. Only set for events of type queryFailed.

actingUser

The user who performed the action. Only set for events of type queryResultRetrieved.