Can't add a custom `stream` to `SteamHandler` using `dictConfig`
jderrien opened this issue ยท 1 comments
jderrien commented
Hello ๐
Trying to log on stdout
, but configuring it through picologging.config.dictConfig
doesn't work.
I tried both sys.stdout
and "ext://sys.stdout"
(which works with Python standard logging
).
Maybe I'm missing something?
Tested on Python 3.8.18 & 3.12.2
import picologging, picologging.config
import sys
picologging.config.dictConfig({
"version": 1,
"formatters": {
"standard": {
"format": "%(levelname)s :: %(asctime)s :: %(name)s :: %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S",
},
},
"handlers": {
"console_stdout": {
"class": "picologging.StreamHandler",
#"stream": "ext://sys.stdout",
"stream": sys.stdout,
"level": "DEBUG",
"formatter": "standard",
},
},
"loggers": {
"test_logger": {
"level": "INFO",
"handlers": ["console_stdout"],
"propagate": False
}
},
})
logger = picologging.getLogger("test_logger")
logger.info("Hello world")
Error:
% python picologging_stdout.py
Traceback (most recent call last):
File "/Users/jderrien/projects/litestar-test/.venv.python3.12.2/lib/python3.12/site-packages/picologging/config.py", line 265, in configure_handler
result = factory(**kwargs)
^^^^^^^^^^^^^^^^^
TypeError: 'stream' is an invalid keyword argument for this function
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/jderrien/projects/litestar-test/.venv.python3.12.2/lib/python3.12/site-packages/picologging/config.py", line 94, in configure
handler = self.configure_handler(handlers[name])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jderrien/projects/litestar-test/.venv.python3.12.2/lib/python3.12/site-packages/picologging/config.py", line 274, in configure_handler
result = factory(**kwargs)
^^^^^^^^^^^^^^^^^
TypeError: 'strm' is an invalid keyword argument for this function
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/jderrien/projects/litestar-test/picologging_stdout.py", line 4, in <module>
picologging.config.dictConfig({
File "/Users/jderrien/projects/litestar-test/.venv.python3.12.2/lib/python3.12/site-packages/picologging/config.py", line 332, in dictConfig
dictConfigClass(config).configure()
File "/Users/jderrien/projects/litestar-test/.venv.python3.12.2/lib/python3.12/site-packages/picologging/config.py", line 101, in configure
raise ValueError("Unable to configure handler " "%r" % name) from e
ValueError: Unable to configure handler 'console_stdout'
wwarne commented
I've had the same problem.
It looks like that dictConfig
tries to initialize picologging.StreamHandler
with kwargs, but it only accepts positional only arguments and throws an exception.
I ended up with overwriting __init__
function.
class StreamHandlerForConfigDict(picologging.StreamHandler):
def __init__(self, stream) -> None:
super().__init__(stream)
This new class works with dictConfig as expected
...
{
'console': {
'class': 'logging_settings.picologging_handlers.StreamHandlerForConfigDict',
'formatter': 'standard',
'stream': 'ext://sys.stdout',
},
...