Skip to content

Instantly share code, notes, and snippets.

@raphaelcastaneda
Created September 2, 2020 01:06
Show Gist options
  • Save raphaelcastaneda/d47d1a26183c1a5a892f8f5e1c22a8d6 to your computer and use it in GitHub Desktop.
Save raphaelcastaneda/d47d1a26183c1a5a892f8f5e1c22a8d6 to your computer and use it in GitHub Desktop.
lnav python logger interpretter
{
"pythonlogger": {
"title": "Python logger format",
"description": "Log format used by python logger class",
"url": "",
"regex": {
"main": {
"pattern": "^\\[(?<timestamp>\\d{4}\\-\\d{2}\\-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3})] (?<level>\\w+) (?<module>(\\w(\\.)?)+) \\- (?<body>.*)$"
}
},
"timestamp-format": ["%Y-%m-%d %H:%M:%S,%L"],
"level-field": "level",
"level": {
"critical": "CRITICAL",
"error": "ERROR",
"warning": "WARNING",
"info": "INFO",
"debug": "DEBUG"
},
"value": {
"level": { "kind": "string", "identifier": true },
"module": { "kind": "string", "identifier": true },
"body": { "kind": "string" }
},
"sample": [
{
"line": "[2017-01-12 17:32:02,056] INFO module.blah - we are doing the thing",
"line": "[2017-01-12 17:32:02,911] ERROR foobar - ThingNoWorkExeption: it failed",
"line": "[2020-09-01 17:22:37,491] INFO scheduler - Starting job runner"
}
]
}
}
@raphaelcastaneda
Copy link
Author

Based on https://gist.github.com/benje/e2ab750b0a81d11920d83af637d289f7

To Install

Download this file and copy it to ~/.lnav/formats/lnav_format_python.json.

Run the following lnav command to install the format permanently:
lnav -i ~/.lnav/formats/lnav_format_python.json

http://lnav.readthedocs.io/en/latest/formats.html#installing-formats

@raphaelcastaneda
Copy link
Author

raphaelcastaneda commented Sep 2, 2020

To produce logs of this format, use the format string below with the python logging moddule.
When piping directly to lnav, make sure to force the logger to use sys.stdout

import sys
import logging

FORMAT = '[%(asctime)s] %(levelname)s %(name)s - %(message)s'
logging.basicConfig(format=FORMAT, stream=sys.stdout)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment