question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Guide for configuring for Python log files

See original GitHub issue

Guide for configuring for Python log files

I thought the community would benefit from a quick walkthrough on how to configure for Python. After spending a few hours getting this to work, I have documented the steps required to build up the log format from the format configured for python logging.

My python logging format is configured in a yaml file and is as follows:

precice:
        format: "%(asctime)s - %(threadName)s - %(name)s - %(levelname)s - %(message)s"
        datefmt: '%Y-%m-%d %H:%M:%S'

Starting with the easiest first, in the ideolog settings I configured:

  • The message start pattern as ^\d{4} to match the start of the line followed by the 4 numbers that make up the year of the timestamp (yyyy); and
  • The time format as yyyy-MM-dd HH:mm:ss to match the datefmt above.

I then moved on to the message pattern. You will see that there are 5 distinct groups from the format specified above, each separated by space dash space. I built up my log format as follows:

Start of line ^

Followed by first group that contains the timestamp in the format specified by datefmt above. For this I used 4 numbers dash 2 numbers dash 2 numbers space 2 numbers colon 2 numbers colon 2 numbers. These are all contained in brackets as they will be referred to later as the time capture group: (\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})

I then added the space dash space separator outside of the capture group: \s-\s

Then for the next capture group which in my case contains the thread name. This is simple as it is just an unknown length string: (.*)

Then another separator followed by another unknown length string capture group for the class name and another separator: \s-\s(.*)\s-\s

Then the capture group containing the log level. In my case this is between 4 and 7 uppercase letters (INFO, ERROR, DEBUG or WARNING): ([A-Z]{4,7})

Then another separator followed by a unknown length string for the log message: \s-\s(.*)

Followed by end of line: $

Putting this all together gives us the following message pattern: ^(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s-\s(.*)\s-\s(.*)\s-\s([A-Z]{4,7})\s-\s(.*)$

I then set up the capture groups. Time=1, Severity=4, Category=3

Finally I set up the patterns for log level. ^\sERROR\s$ as highlight field red + stripe ^\sWARNING\s$ as highlight field orange ^\sINFO\s$ as highlight field green.

After closing and reopening my log file everything worked as expected.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:7

github_iconTop GitHub Comments

1reaction
ZeroCool2ucommented, Dec 15, 2019

There are inconsistencies with the line endings. Try hitting enter at the end of the log file or deleting the last line of the file.

1reaction
ZeroCool2ucommented, Oct 24, 2019

Okay, this message pattern works as is, but the following new patterns are required for the log level highlighting to work properly. Also, if you want to use the CRITICAL messages built into the Python logger, you’ll need to adjust the message pattern very slightly, to allow for CRITICAL to be recognized, because it is 8 characters instead of 7. The message pattern below will work for that. Note that the only difference is in the final capture group, ([A-Z]{4-7}) -> ([A-Z]{4-8}).

^(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s-\s(.*)\s-\s(.*)\s-\s([A-Z]{4,8})\s-\s(.*)$

^\s*C(RITICAL)?\s*$
^\s*E(RROR)?\s*$
^\s*W(ARN(ING)?)?\s*$
^\s*I(NFO)?\s*$

Here is a basic logging setup that you can drop in at the beginning of your program and will work with this message pattern.

import logging
logging.basicConfig(filename=r'logs/app_logs.log', format='%(asctime)s - %(threadName)s - %(name)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.INFO)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging HOWTO — Python 3.11.1 documentation
Basic Logging Tutorial: Logging is a means of tracking events that happen ... Creating a logging config file and reading it using the...
Read more >
Logging - The Hitchhiker's Guide to Python
The log record, which is created with every logging event, contains readily available diagnostic information such as the file name, full path, function,...
Read more >
Python Logging Basics - The Ultimate Guide To Logging
Python Logging Basics. This article covers the basics of using the standard logging module that ships with all Python distributions.
Read more >
Python Logging Guide - Best Practices and Hands-on Examples
The Python standard library provides a logging module as a solution to log events from applications and libraries. Once the logger is configured...
Read more >
A Step-by-Step Guide to Python Logging - Pylenin
Usually logs have little purpose being printed to the console. You would ideally want to store them in a file. To do that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found