Hello All!
I got asked how to configure the logging stack to be able to output directly to console using both stdout and stderr and I could not really find a great answer as adding both as StreamHandlers will result in error and above messages going to stdout.
The usecase is having a cli or app that wants to log to console as other tools. Errors and bove to stderr and normal information to stdout. I know the recommended way in Python is to just use print on simple scripts, but it can happen that you import a library you want to see the logs of, and therefore you need to set the logging stack.
I have drafted two implementations but I am open to suggestions: 1) A "Console Handler" that uses multiple streams and chooses based on the level. 2) An inverted filter that can be used to filter everything above info for the stdout Stream handler.
What do you people think? If people like it I'll send an issue + PR.
Regards, Mario Corchero
On Sun, Feb 11, 2018 at 6:29 AM, Mario Corchero mariocj89@gmail.com wrote:
Hello All!
I got asked how to configure the logging stack to be able to output directly to console using both stdout and stderr and I could not really find a great answer as adding both as StreamHandlers will result in error and above messages going to stdout.
There's a recipe in the docs that shows how to "fork" to console and file:
https://docs.python.org/3/howto/logging-cookbook.html#logging-to-multiple-de...
I presume that's what you were looking at? Because yes, that'll do exactly what you say.
The usecase is having a cli or app that wants to log to console as other tools. Errors and bove to stderr and normal information to stdout. I know the recommended way in Python is to just use print on simple scripts, but it can happen that you import a library you want to see the logs of, and therefore you need to set the logging stack.
I have drafted two implementations but I am open to suggestions:
- A "Console Handler" that uses multiple streams and chooses based on the
level. 2) An inverted filter that can be used to filter everything above info for the stdout Stream handler.
What do you people think? If people like it I'll send an issue + PR.
It would be an interesting variant on the recipe to say "debug and above, but NOT error and above, goes to this stream". How complex are the implementations? Would they fit nicely into the cookbook?
ChrisA
The recipe as you pointed out works by logging to both (just using multiple handlers). The objective is to log *up to a level* to stdout and the rest to stderr.
See the example console handler here https://github.com/mariocj89/cpython/commit/501cfcd0f4cad1e04d87b89784988c52a77a80ad and the filter here https://gist.github.com/mariocj89/7d873fc84bf8723f5e2740adbeccde12.
Good point about just adding it to the how-to.
On 10 February 2018 at 20:20, Chris Angelico rosuav@gmail.com wrote:
On Sun, Feb 11, 2018 at 6:29 AM, Mario Corchero mariocj89@gmail.com wrote:
Hello All!
I got asked how to configure the logging stack to be able to output
directly
to console using both stdout and stderr and I could not really find a
great
answer as adding both as StreamHandlers will result in error and above messages going to stdout.
There's a recipe in the docs that shows how to "fork" to console and file:
https://docs.python.org/3/howto/logging-cookbook.html#logging-to-multiple- destinations
I presume that's what you were looking at? Because yes, that'll do exactly what you say.
The usecase is having a cli or app that wants to log to console as other tools. Errors and bove to stderr and normal information to stdout. I know the recommended way in Python is to just use print on simple scripts,
but it
can happen that you import a library you want to see the logs of, and therefore you need to set the logging stack.
I have drafted two implementations but I am open to suggestions:
- A "Console Handler" that uses multiple streams and chooses based on
the
level. 2) An inverted filter that can be used to filter everything above info
for
the stdout Stream handler.
What do you people think? If people like it I'll send an issue + PR.
It would be an interesting variant on the recipe to say "debug and above, but NOT error and above, goes to this stream". How complex are the implementations? Would they fit nicely into the cookbook?
ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Sun, Feb 11, 2018 at 7:49 AM, Mario Corchero mariocj89@gmail.com wrote:
The recipe as you pointed out works by logging to both (just using multiple handlers).
Yep. It's a "forking" setup. What you're proposing is a "splitting" setup, which would be a great recipe to put immediately underneath that one.
The objective is to log *up to a level* to stdout and the rest to stderr.
See the example console handler here and the filter here.
Good point about just adding it to the how-to.
You know, I think BOTH of those are worth adding. Especially the filter method, which is pretty concise and easy to build on. Want to write up a docs patch?
ChrisA
Sure thing! Will prepare it tomorrow.
On Sat, 10 Feb 2018 at 21:00, Chris Angelico rosuav@gmail.com wrote:
On Sun, Feb 11, 2018 at 7:49 AM, Mario Corchero mariocj89@gmail.com wrote:
The recipe as you pointed out works by logging to both (just using
multiple
handlers).
Yep. It's a "forking" setup. What you're proposing is a "splitting" setup, which would be a great recipe to put immediately underneath that one.
The objective is to log *up to a level* to stdout and the rest to stderr.
See the example console handler here and the filter here.
Good point about just adding it to the how-to.
You know, I think BOTH of those are worth adding. Especially the filter method, which is pretty concise and easy to build on. Want to write up a docs patch?
ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/