|
BDE 4.39.x Production Release
|
Provide common methods for scheme-based formatters for observers.
Provide common methods for scheme-based formatters for observers
This component provides a common implementation of methods necessary to support scheme-based formatter configuration.
By default, the output format for log records is set by constructor arguments format and timezoneDefault. The default format (and the formatter used) can be changed by calling the setFormat method. See {Scheme-Based Formatters} for more information.
The above statement will cause subsequent records to be formatted as JSON objects that contains a timestamp in 'DDMonYYYY_HH:MM:SS.mmm' format, the severity, and the log message.
The default time zone (UTC or local) for timestamps may be changed using the setTimezoneDefault method. Note that this method creates and installs a new formatter that uses the last successfully set format and the new time zone default.
The time zone default is called default because certain format configuration syntaxes (JSON:// at the time of writing) allow the user to explicitly specify the time zone of the timestamp. For fields where it is not specified the default is used. The % formats (at the time of writing) do not support specifying the time zone, so for such syntaxes the time zone default is used for all timestamp fields.
When using printf-style format strings, the respective formats are specified using %-prefixed conversion specifications. (See ball_recordstringformatter for information on how format specifications are defined and interpreted.) For example, the following statement will force subsequent records to be logged in a format that is almost identical to the default format except that the timestamp attribute will be written in ISO 8601 format:
The setFormat method uses the "text" scheme by default for format config strings that do not have a scheme, so the following is an equivalent (though less expressive and deprecated) method to produce the same configuration:
The recommended way to specify log record formats is using URI-like scheme-tagged format configuration strings. A scheme-tagged format string begins with a scheme identifier followed by :// and then a scheme-specific format specification:
The scheme determines which formatter will be used and the syntax of the format specification. The following schemes are currently supported: text, json, qjson. See Scheme-Based Formatters for more details of the supported schemes and their accompanying format specification syntaxes.
Examples:
For backward compatibility, format specifications that do not begin with a scheme tag are treated as legacy printf-style format strings. Such specifications are implicitly treated as if they had a text:// prefix and use ball::RecordStringFormatter. For example, the following two calls are equivalent:
These %-prefixed conversion specifications are defined in ball_recordstringformatter .
There is also a legacy way to change the format by supplying a suitable formatting functor using setFormatFunctor. For example, an instance of ball::RecordStringFormatter conveniently is such a functor:
The above statement will cause subsequent records to be formatted by that string formatter. When the formatter is set up this legacy way the setTimezoneDefault method will not affect the formatter and so the return value of getTimezoneDefault is meaningless.
This class is deliberately only const thread-safe, meaning that the concrete observer implementation has to provide and lock a mutex before calling the methods of this class (except for the constructor).
This section illustrates intended use of this component.
In this example, we demonstrate how to use ObserverFormatterImp to implement a simple observer that writes formatted log records to standard output. First, we define a simple observer class that uses ObserverFormatterImp to manage formatting:
Now, we can create an instance of our observer and configure its formatting. First, we create an observer with default text-based format:
Next, we can change the format to JSON format using the setFormat method:
We can also enable local time for timestamps:
To revert to UTC time:
We can also retrieve the current format configuration:
For backwards compatibility, we can also use a custom formatter functor:
Now let's demonstrate actually publishing a log record. First, we create a sample record and context with a fixed timestamp:
To capture the output for verification, we redirect 'bsl::cout' to a string stream:
When we publish this record with text format, it produces plain text output:
Next, we publish the same record with JSON format that produces structured JSON output:
Finally, we verify the JSON fields: