Entry Formatting
In LogKit, Log Entries go from LXLogEntry
to string by using each target Endpoint’s formatters. Every Endpoint has its own formatters, so different Endpoints can output Entries in different formats. For example, an application using both a Console Endpoint and a File Endpoint could choose concise formatters for the console, and more detailed formatters for the file.
An Endpoint requires two formatters to convert each Log Entry to a string. The Endpoint’s dateFormatter
describes how an Entry’s dateTime
property will be converted. Its entryFormatter
describes how the Entry itself will be converted. Using the Endpoint’s formatters, each LXLogEntry
is converted to a string before the Endpoint writes it to its final destination.
Although the formatters of each Endpoint may be customized, all Endpoints include sensible defaults that will suit most developer’s needs. If desired, however, a developer can choose to use another of the variety of date and Entry formatters included with LogKit, or even create and use their own.
Date Formatters
Date formatting is handled by the LXDateFormatter
object.
Built-in Date Formatters
LXDateFormatter
comes with the following built-in presets:
.standardFormatter() |
Converts NSDate objects into a datetime string, using the UTC timezone Format String: yyyy-MM-dd HH:mm:ss.SSS |
.timeOnlyFormatter() |
Converts NSDate objects into a time-only string, using the UTC timezone Format String: HH:mm:ss.SSS |
.dateOnlyFormatter() |
Converts NSDate objects into a date-only string, using the UTC timezone Format String: yyyy-MM-dd |
.ISO8601DateTimeFormatter() |
Converts NSDate objects into strings following the ISO 8601 combined datetime format, using the UTC timezone Format String: yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ |
Default Date Formatter
Unless otherwise specified by a specific Endpoint’s documentation, when the dateFormatter
initialization parameter is omitted, the Endpoint will be initialized with the .standardFormatter()
date formatter.
Custom Date Formatters
LogKit makes creating custom date formatters easy. The LXDateFormatter
object comes with the following initializer:
Parameters
formatString |
Type: String Required |
The date format to be used by the formatter |
timeZone |
Type: NSTimeZone Default: UTC timezone |
The time zone to use during formatting |
The formatString
parameter requires a string in the same format as Apple’s NSDateFormatter.dateFormat
parameter. If omitted, the formatter will use the UTC timezone for its timeZone
parameter. UTC is recommended for most applications, as it makes it easy to compare datetimes from different systems in different locations.
Returns an initialized LXDateFormatter
instance.
Example: The following creates a date formatter that outputs only the hour and minute of a Log Entry’s dateTime
in the user’s local time zone:
A note about precision:
Although LogKit captures timestamps with microsecond precision,
LXDateFormatter
always rounds to the nearest millisecond. If your application requires microsecond precision, you should customize your Endpoint’sentryFormatter
to display each Entry’stimestamp
property, which retains microsecond precision.
Using a Date Formatter
To use a date formatter, simply supply it to the Endpoint you are initializing using the dateFormatter
parameter. Some examples:
Entry Formatters
Log Entry formatting is handled by the LXEntryFormatter
object.
Built-in Entry Formatters
LXEntryFormatter
includes the following built-in presets:
Default Entry Formatter
Unless otherwise specified by a specific Endpoint’s documentation, when the entryFormatter
initialization parameter is omitted, the Endpoint will be initialized with the .standardFormatter()
Entry formatter.
Custom Entry Formatters
LogKit makes creating custom Entry formatters easy. The LXEntryFormatter
object comes with the following initializer:
The initializer takes only one parameter - a closure of type (LXLogEntry) -> String
. This closure must accept an LXLogEntry
and return a String
. The closure is free to use as many or as few of a Log Entry’s properties as the developer wishes.
Returns an initialized LXEntryFormatter
instance.
Example: The following creates a concise Entry formatter that outputs only a Log Entry’s dateTime
and message
:
Example: The following creates a detailed Entry formatter that outputs an Entry’s timestamp
, level
, and detailed information regarding location of the function that generated the message, as well as the message
itself:
Using an Entry Formatter
To use an Entry formatter, simply provide it to the Endpoint you are initializing using the entryFormatter
parameter. Some examples:
Customizing Entry Properties
Each Log Entry instance contains a userInfo
dictionary property. This dictionary’s contents are set by the application developer during each Logger method call. For example:
Including data in userData
can be useful in a few ways.
- The developer can supply additional properties to be included in an Entry formatter’s string output.
- The developer can supply parameters intended to control an Entry formatter’s string output.
Note: Some Endpoints (such as the included HTTP JSON Endpoint) may automatically attempt to include any
userInfo
provided as a part of theirentryFormatter
output string.