On 2021-02-06 21:01:37 -0600, Skip Montanaro wrote:
> The logging package can log exceptions and call stacks, but it does
> (in my opinion) a suboptimal job of it. Consider this simple example:
> >>> import logging
> >>> FORMAT = '%(asctime)-15s %(levelname)s %(message)s'
> >>> logging.basicConfig(format=FORMAT, force=True)
> >>> log.warning("msg", stack_info=True)
> 2021-02-06 20:46:52,399 WARNING msg
> Stack (most recent call last):
>   File "<stdin>", line 1, in <module>
> 
> It formats the warning message just fine, but simply dumps the
> traceback onto the stream with no timestamp or level. For my purposes,
> this is more important for exceptions. (I'm just using the stack trace
> feature as it's easier in a small example.) I would like to call
> something like

I suspect that it just adds the stack trace to the message, so that you
are left with a multi-line message.

I often produce multi-line log messages myself. They are much nicer to
read than extremely long lines or groups of messages which should really
be read as a unit ...


> log.exception("Some message...")
> 
> and find something like this in the output stream:
> 
> 2021-02-06 20:46:52,399 ERROR Some message...
> 2021-02-06 20:46:52,400 ERROR Traceback (most recent call last):
> 2021-02-06 20:46:52,402 ERROR   File "<stdin>", line 1, in <module>

... like this.


> That way I can more easily grep log files for errors and get the
> entire detail, including the traceback.

Yes, grep is unfortunately very line-oriented. I often write write
simple scripts to filter log files where one message can span multiple
lines (Python's logging module isn't the only one - Samba and PostgreSQL
come to mind).

Another possibility would be to write the logs into a database. That
also has the advantage that the messages are stored in a structure and
you don't have to parse them.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | h...@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachment: signature.asc
Description: PGP signature

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to