2013-07-26 15:23:45 +00:00
|
|
|
|
|
|
|
.. _log-formats:
|
|
|
|
|
|
|
|
Log Formats
|
|
|
|
===========
|
|
|
|
|
2013-08-01 15:56:00 +00:00
|
|
|
Log files are parsed based on formats defined in configuration files. Many
|
|
|
|
formats are already built in to the **lnav** binary and you can define your own
|
|
|
|
using a JSON file. When loading files, each format is checked to see if it can
|
|
|
|
parse the first few lines in the file. Once a match is found, that format will
|
|
|
|
be considered that files format and used to parse the remaining lines in the
|
|
|
|
file. If no match is found, the file is considered to be plain text and can
|
|
|
|
be viewed in the "text" view that is accessed with the **t** key.
|
2013-07-29 00:05:09 +00:00
|
|
|
|
|
|
|
The following log formats are built into **lnav**:
|
2013-07-26 15:23:45 +00:00
|
|
|
|
2013-07-27 18:04:43 +00:00
|
|
|
.. csv-table::
|
|
|
|
:header: "Name", "Table Name", "Description"
|
|
|
|
:widths: 8 5 20
|
|
|
|
:file: format-table.csv
|
|
|
|
|
2013-07-26 15:23:45 +00:00
|
|
|
Defining a New Format
|
|
|
|
---------------------
|
2013-07-29 00:05:09 +00:00
|
|
|
|
2013-09-10 16:42:30 +00:00
|
|
|
New log formats can be defined by placing JSON configuration files in
|
|
|
|
subdirectories of the :file:`~/.lnav/formats/` directory. The directories and
|
|
|
|
files can be named anything you like, but they must have the '.json' suffix. A
|
|
|
|
sample file containing the builtin configuration will be written to this
|
|
|
|
directory when **lnav** starts up. You can consult that file when writing your
|
|
|
|
own formats or if you need to modify existing ones.
|
2013-08-01 15:56:00 +00:00
|
|
|
|
|
|
|
The contents of the format configuration should be a JSON object with a field
|
2013-08-07 06:14:24 +00:00
|
|
|
for each format defined by the file. Each field name should be the symbolic
|
|
|
|
name of the format. This value will also be used as the SQL table name for
|
|
|
|
the log. The value for each field should be another object with the following
|
|
|
|
fields:
|
2013-08-01 15:56:00 +00:00
|
|
|
|
|
|
|
:title: The short and human-readable name for the format.
|
|
|
|
:description: A longer description of the format.
|
|
|
|
:url: A URL to the definition of the format.
|
|
|
|
|
2013-09-10 13:45:48 +00:00
|
|
|
:file-pattern: A regular expression used to match log file paths. Typically,
|
|
|
|
every file format will be tried during the detection process. This field
|
|
|
|
can be used to limit which files a format is applied to in case there is
|
|
|
|
a potential for conflicts.
|
|
|
|
|
2013-08-25 14:12:41 +00:00
|
|
|
:regex: This object contains sub-objects that describe the message formats
|
2013-09-10 13:45:48 +00:00
|
|
|
to match in a plain log file. Log files that contain JSON messages should
|
|
|
|
not specify this field.
|
2013-08-25 14:12:41 +00:00
|
|
|
|
|
|
|
:pattern: The regular expression that should be used to match log messages.
|
|
|
|
The `PCRE <http://www.pcre.org>`_ library is used by **lnav** to do all
|
|
|
|
regular expression matching.
|
2013-08-01 15:56:00 +00:00
|
|
|
|
2013-09-10 13:45:48 +00:00
|
|
|
:json: True if each log line is JSON-encoded.
|
|
|
|
|
|
|
|
:line-format: An array that specifies the text format for JSON-encoded
|
|
|
|
log messages. Log files that are JSON-encoded will have each message
|
|
|
|
converted from the raw JSON encoding into this format. Each element
|
|
|
|
is either an object that defines which fields should be inserted into
|
|
|
|
the final message string and or a string constant that should be
|
|
|
|
inserted. For example, the following configuration will tranform each
|
|
|
|
log message object into a string that contains the timestamp, followed
|
|
|
|
by a space, and then the message body::
|
|
|
|
|
|
|
|
[ { "field": "ts" }, " ", { "field": "msg" } ]
|
|
|
|
|
|
|
|
:field: The name of the message field that should be inserted at this
|
|
|
|
point in the message.
|
|
|
|
:default-value: The default value to use if the field could not be found
|
|
|
|
in the current log message. The built-in default is "-".
|
|
|
|
|
|
|
|
:timestamp-field: The name of the field that contains the log message
|
|
|
|
timestamp. Defaults to "timestamp".
|
|
|
|
|
2015-04-11 05:55:57 +00:00
|
|
|
:timestamp-format: An array of timestamp formats using a subset of the
|
|
|
|
strftime conversion specification. The following conversions are
|
|
|
|
supported: %a, %b, %L, %M, %H, %I, %d, %e, %k, %l, %m, %p, %y, %Y, %S, %s,
|
|
|
|
%Z, %z. In addition, you can also use '%i' for milliseconds from the
|
|
|
|
epoch.
|
|
|
|
|
2013-08-01 15:56:00 +00:00
|
|
|
:level-field: The name of the regex capture group that contains the log
|
2013-09-10 13:45:48 +00:00
|
|
|
message level. Defaults to "level".
|
|
|
|
|
|
|
|
:body-field: The name of the field that contains the main body of the
|
|
|
|
message. Defaults to "body".
|
2013-08-01 15:56:00 +00:00
|
|
|
|
|
|
|
:level: A mapping of error levels to regular expressions. During scanning
|
|
|
|
the contents of the capture group specified by *level-field* will be
|
|
|
|
checked against each of these regexes. Once a match is found, the log
|
|
|
|
message level will set to the corresponding level. The available levels,
|
|
|
|
in order of severity, are: **fatal**, **critical**, **error**,
|
2015-04-11 02:50:05 +00:00
|
|
|
**warning**, **stats**, **info**, **debug**, **debug2-5**, **trace**.
|
2013-08-01 15:56:00 +00:00
|
|
|
|
2013-08-07 06:14:24 +00:00
|
|
|
:value: This object contains the definitions for the values captured by the
|
|
|
|
regexes.
|
|
|
|
|
|
|
|
:kind: The type of data that was captured **string**, **integer**,
|
2014-06-18 04:29:42 +00:00
|
|
|
**float**, **json**, **quoted**.
|
2013-08-07 06:14:24 +00:00
|
|
|
:collate: The collation function for this value.
|
|
|
|
:identifier: A boolean that indicates whether or not this field represents
|
|
|
|
an identifier and should be syntax colored.
|
|
|
|
:foreign-key: A boolean that indicates that this field is a key and should
|
|
|
|
not be graphed. This should only need to be set for integer fields.
|
|
|
|
|
|
|
|
:sample: A list of objects that contain sample log messages. All formats
|
|
|
|
must include at least one sample and it must be matched by one of the
|
|
|
|
included regexes. Each object must contain the following field:
|
2013-08-01 15:56:00 +00:00
|
|
|
|
2013-08-07 06:14:24 +00:00
|
|
|
:line: The sample message.
|
2013-08-01 15:56:00 +00:00
|
|
|
|
|
|
|
Example format::
|
|
|
|
|
|
|
|
{
|
|
|
|
"example_log" : {
|
|
|
|
"title" : "Example Log Format",
|
|
|
|
"description" : "Log format used in the documentation example.",
|
|
|
|
"url" : "http://example.com/log-format.html",
|
|
|
|
"regex" : {
|
|
|
|
"basic" : {
|
2013-08-26 14:20:01 +00:00
|
|
|
"pattern" : "^(?<timestamp>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z)>>(?<level>\\w+)>>(?<component>\\w+)>>(?<body>.*)$"
|
2013-08-01 15:56:00 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"level-field" : "level",
|
|
|
|
"level" : {
|
|
|
|
"error" : "ERROR",
|
|
|
|
"warning" : "WARNING"
|
|
|
|
},
|
|
|
|
"value" : {
|
|
|
|
"component" : {
|
|
|
|
"kind" : "string",
|
|
|
|
"identifier" : true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"sample" : [
|
|
|
|
{
|
2013-08-26 14:20:01 +00:00
|
|
|
"line" : "2011-04-01T15:14:34.203Z>>ERROR>>core>>Shit's on fire yo!"
|
2013-08-01 15:56:00 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Modifying an Existing Format
|
|
|
|
----------------------------
|
2013-08-07 06:14:24 +00:00
|
|
|
|
2013-08-26 14:20:01 +00:00
|
|
|
When loading log formats from files, **lnav** will overlay any new data over
|
|
|
|
previously loaded data. This feature allows you to override existing value or
|
|
|
|
append new ones to the format configurations. For example, you can separately
|
|
|
|
add a new regex to the example log format given above by creating another file
|
|
|
|
with the following contents::
|
2013-08-07 06:14:24 +00:00
|
|
|
|
2013-08-26 14:20:01 +00:00
|
|
|
{
|
|
|
|
"example_log" : {
|
|
|
|
"regex" : {
|
|
|
|
"custom1" : {
|
|
|
|
"pattern" : "^(?<timestamp>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z)<<(?<level>\\w+)--(?<component>\\w+)>>(?<body>.*)$"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"sample" : [
|
|
|
|
{
|
|
|
|
"line" : "2011-04-01T15:14:34.203Z<<ERROR--core>>Shit's on fire yo!"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2014-11-05 17:01:09 +00:00
|
|
|
|
|
|
|
Installing Formats
|
|
|
|
------------------
|
|
|
|
|
|
|
|
File formats are loaded from subdirectories in :file:`/etc/lnav/formats` and
|
|
|
|
:file:`~/.lnav/formats/`. You can manually create these subdirectories and
|
|
|
|
copy the format files into there. Or, you can pass the '-i' option to **lnav**
|
|
|
|
to automatically install formats from the command-line. For example::
|
|
|
|
|
|
|
|
$ lnav -i myformat.json
|
|
|
|
info: installed: /home/example/.lnav/formats/installed/myformat_log.json
|
|
|
|
|
|
|
|
Formats installed using this method will be placed in the :file:`installed`
|
|
|
|
subdirectory and named based on the first format name found in the file.
|
|
|
|
|
|
|
|
Format files can also be made executable by adding a shebang (#!) line to the
|
|
|
|
top of the file, like so::
|
|
|
|
|
|
|
|
#! /usr/bin/env lnav -i
|
|
|
|
{
|
|
|
|
"myformat_log" : ...
|
|
|
|
}
|
|
|
|
|
|
|
|
Executing the format file should then install it automatically::
|
|
|
|
|
|
|
|
$ chmod ugo+rx myformat.json
|
|
|
|
$ ./myformat.json
|
|
|
|
info: installed: /home/example/.lnav/formats/installed/myformat_log.json
|
2015-04-05 14:12:20 +00:00
|
|
|
|
|
|
|
Format Order When Scanning a File
|
|
|
|
---------------------------------
|
|
|
|
|
|
|
|
When **lnav** loads a file, it tries each log format against the first ~1000
|
|
|
|
lines of the file trying to find a match. When a match is found, that log
|
|
|
|
format will be locked in and used for the rest of the lines in that file.
|
|
|
|
Since there may be overlap between formats, **lnav** performs a test on
|
|
|
|
startup to determine which formats match each others sample lines. Using
|
|
|
|
this information it will create an ordering of the formats so that the more
|
|
|
|
specific formats are tried before the more generic ones. For example, a
|
|
|
|
format that matches certain syslog messages will match its own sample lines,
|
|
|
|
but not the ones in the syslog samples. On the other hand, the syslog format
|
|
|
|
will match its own samples and those in the more specific format. You can
|
|
|
|
see the order of the format by enabling debugging and checking the **lnav**
|
|
|
|
log file for the "Format order" message::
|
|
|
|
|
|
|
|
$ lnav -d /tmp/lnav.log
|