[Python-ideas] add a LogReader to the logging module
Oleg Broytman
phd at phdru.name
Wed May 10 11:52:08 EDT 2017
Hi! Isn't it just basicConfig?
import logging
logging.basicConfig(
filename='test.log',
format='[%(asctime)s] %(name)s %(levelname)s: %(message)s',
level=logging.DEBUG,
)
log = logging.getLogger("TEST")
On Wed, May 10, 2017 at 08:40:01AM -0700, terji78 at gmail.com wrote:
> I get a message back that I'm not subscribed to the mailing list, but see
> my message in google groups. My sincerest apologies in advance, if this
> appears several times for you. Anyways:
>
> The logging module has an easy-to-setup logger:
>
> import logging
> logger = logging.getLogger(__name__)
>
> and you just log away. Easy.
>
> However, it's quite a bit more difficult to set up log readers, requiring
> IMO an unreasonably number of lines of code:
>
> import logging
>
> logger = logging.getLogger('mypackage')
> logger.setLevel(logging.DEBUG)
> handler = logging.StreamHandler()
> formatter = logging.Formatter('%(
> levelname)s:%(name)s:%(message)s')
> handler.setFormatter(formatter)
> logger.addHandler(handler)
>
> I propose adding a function that sets up a log reader with sensible
> defaults, but allowing customization. So, I propose a
> ``logging.getLogReader`` in the logging module to mirror
> ``logging.getLogger``. So, to use this, in your main.py, you'd typically
> just do:
>
> import logging
>
> log_reader = logging.getLogReader('mypackage.models')
>
>
> and you'd get all log output from mypackage.models with sensible defaults
> set. Much easier.
>
> You could also set up it up in more detail, e.g.:
>
> log_reader = logging.getLogReader('mypackage.models',
> level='debug',
> format='%(levelname)s | %(filename)s|
> line %(lineno)s | %(message)s'
> )
>
> For a specific proposal, see:
>
> https://gist.github.com/topper-123/85e27ffe261850eed150eac53d61b82d
>
> Because it's just a logger, log_reader can be further customized as
> necessary.
>
> In summary, I think that today it's unneccesarily complex to set up a log
> reader and the proposed function serves a general enough need, that it - or
> something similar - should be in the logging module. Thoughts?
Oleg.
--
Oleg Broytman http://phdru.name/ phd at phdru.name
Programmers don't die, they just GOSUB without RETURN.
More information about the Python-ideas
mailing list