BDE 4.14.0 Production release
|
Provide a formatter for log records that renders output in JSON.
This component provides a function object class, ball::RecorJsonFormatter
, that formats a log record as JSON text elements according to a format specification (see {Record Format Specification
}). ball::RecordJsonFormatter
is designed to match the function signature expected by many concrete ball::Observer
implementations that publish log records (for example, see ball::FileObserver2::setLogFileFunctor
).
NOTE: ball::RecorJsonFormatter
renders individual log records as JSON, but, for example, a resulting log file would contain a sequence of JSON strings, which is not itself valid JSON text.
A format specification is, itself, a JSON array, supplied to a RecordJsonFormatter
object by the setFormat
function. If no format is specified, the default format is used. Each array element specifies the format of a log record field or a user-defined attribute. Here is a simple example:
would a result in a log record like:
Again, the format specification is a JSON array, each element of which can be one of the following:
[timestamp]
, would display the timestamp as: {"timestamp": "2020-08-28T14:43:50.375Z"}
.[{"timestamp": {"name": "My Time", "format": "bdePrint"}}]
, would display the timestamp as: {"My Time": "28AUG2020_14:43:50.375"}
.The following table lists the predefined string values for each fixed field and user-defined attributes in the log record:
The output format of each field can be customized by replacing a string value in the JSON array with a JSON object having the same name and a set of key-value pairs (attributes).
The sections that follow describe the set of fields that can be provided in the format specification supplied to setFormat
. RecordJsonFormatter::setFormat
will ignore fields in the provided format specification that are unknown, but will report an error if a known field contains a property that is not supported. For example: a format specification '["pid", { "timestamp" : {"unknown field!": "value"} }] will be accepted, but ["pid", {"timestamp": {"format": "unknown format" }}]
will produce an error.
Each key-value pair of a JSON object that specifies a format of an output of a fixed record field or a user-defined attribute has the following constrains:
RecordJsonFormatter::setFormat
method.The format attributes of the "timestamp" object are given in the following table:
Note: The default "bdePrint" format denotes the following datetime format:
For example, the following record format specification:
would a result in a log record like:
The format attributes of the process Id field are given in the following table:
For example, the following record format specification:
would a result in a log record like:
The format attributes of the thread Id field are given in the following table:
For example, the following record format specification:
would a result in a log record like:
The format attributes of the "file" field are given in the following table:
For example, the following record format specification:
would a result in a log record like:
The format attributes of the "line" field are given in the following table:
For example, the following record format specification:
would a result in a log record like:
The format attributes of the "category" field are given in the following table:
For example, the following record format specification:
would a result in a log record like:
The format attributes of the "severity" field are given in the following table:
For example, the following record format specification:
would a result in a log record like:
A message is a JSON string which is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes: (", \, \/, , \f,
, \r, \t, \u{4 hex digits}).
The format attributes of the "message" field are given in the following table:
For example, the following record format specification:
would a result in a log record like:
The "attributes" JSON object has no attributes. For example, the following record format specification:
would (assuming their are two attributes "bas.requestid" and "mylib.security") result in a log record like:
Each user-defined attribute has a single "name" attribute that can be used to rename the user-defined attribute:
For example, the following record format specification:
would a result in a log record like:
The record separator is a string that is printed after each formatted record. The default value of the record separator is a single newline, but it can be set to any string of the user's choice using the RecordJsonFormatter::setRecordSeparator
function.
This section illustrates intended use of this component.
Suppose an application needs to format log records as JSON and output them to stdout
.
First we instantiate a JSON record formatter:
Next we set a format specification to the newly created formatter
:
The chosen format specification indicates that, when a record is formatted using formatter
, the thread Id attribute of the record will be output followed by the message attribute of the record.
Then we create a default ball::Record
and set the thread Id and message attributes of the record to dummy values:
Next, invocation of the formatter
function object to format record
to bsl::cout
:
yields this output, which is terminated by a single newline:
Finally, we change the record separator and format the same record again:
The record is printed in the same format, but now terminated by two newlines: