logging.LogRecord asctime aattribute - easily override somehow?

Peter Otten __peter__ at web.de
Wed Mar 29 15:35:20 EDT 2017


Skip Montanaro wrote:

>>>> import logging
>>>> logging.basicConfig(format="%(asctime)s" + logging.BASIC_FORMAT,
>>>> datefmt="***%A***") logging.warn("foo")
>  ***Wednesday***WARNING:root:foo
> 
> Thanks, Peter. I suppose a bit more detail of my environment would
> have helped. I'm actually using the logging package indirectly via
> Flask. After a quick skim of Flask's code, it didn't look like the
> Flask author considered that users might want to do their own thing
> with logging. I think I could override the Flask.logger property and
> chuck in something like your code. I'll just suffer for the time being
> with my "two word" timestamps. 

$ cat hello.py
from flask import Flask

import functools
import flask.logging

flask.logging.Formatter = functools.partial(
    flask.logging.Formatter, datefmt="%A"
)

app = Flask(__name__)

@app.route('/')
def hello_world():
    app.logger.critical("test")
    return 'Hello, World!'
$ FLASK_APP=hello.py python -m flask run &
[1] 6028
$  * Serving Flask app "hello"
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

$ curl http://127.0.0.1:5000/
[Wednesday] CRITICAL in hello: test
127.0.0.1 - - [29/Mar/2017 21:26:38] "GET / HTTP/1.1" 200 -
Hello, World!$ 

> Whoever thought you'd want to break up
> timestamps into two words by default, and hard-code a comma as the
> separator between seconds and milliseconds?

In Germany the comma is the decimal separator, so this doesn't look like two 
words to my eye. However, the culprit is

https://en.wikipedia.org/wiki/ISO_8601

"""
A decimal mark, either a comma or a dot (without any preference as stated in 
resolution 10 of the 22nd General Conference CGPM in 2003,[16] but with a 
preference for a comma according to ISO 8601:2004)[17] is used as a 
separator between the time element and its fraction
"""

> 
> Just for the record, for my own standalone code I never use the
> logging module. I just have a simple Logger class which does all I
> need. The logging module (and log4j) have always seemed to me to be an
> overly general solution in search of a problem.
> 
> Skip





More information about the Python-list mailing list