Skip to content

Commit

Permalink
Fix parsing of the ini file to setup loggers
Browse files Browse the repository at this point in the history
Done for sqlalchemylogger, but actually the logger would try
to write logs (process) on another process than the logs
collecteur (emit) which can't work. The setup of sqlalchemy
logger must be done manually in the project and not via the
ini file.
  • Loading branch information
ger-benjamin committed Jun 14, 2024
1 parent b88f762 commit 56e1bd8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions c2cwsgiutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import re
import ast
import sys
from configparser import SectionProxy
from typing import Any
Expand Down Expand Up @@ -35,20 +36,24 @@ def _create_handlers(config: configparser.ConfigParser) -> dict[str, Any]:
stream_re = re.compile(r"\((.*?),\)")
for hh in handlers:
block = config[f"handler_{hh}"]
stream_match = stream_re.match(block["args"])
if stream_match is None:
raise Exception(f"Could not parse args of handler {hh}") # pylint: disable=broad-exception-raised
args = stream_match.groups()[0]
if "args" in block:
raise ValueError(f"Can not parse args of handlers {hh}, use kwargs instead.")
c = block["class"]
if "." not in c:
# classes like StreamHandler does not need the prefix in the ini so we add it here
c = f"logging.{c}"
conf = {
"class": c,
"stream": f"ext://{args}", # like ext://sys.stdout
"class": c,
}
if "level" in block:
conf["level"] = block["level"]
if "formatter" in block:
conf["formatter"] = block["formatter"]
if "filters" in block:
conf["filters"] = block["filters"]
if "kwargs" in block:
kwargs = ast.literal_eval(block["kwargs"])
conf.update(kwargs)
d_handlers[hh] = conf
return d_handlers

Expand All @@ -57,7 +62,7 @@ def _filter_logger(block: SectionProxy) -> dict[str, Any]:
out: dict[str, Any] = {"level": block["level"]}
handlers = block.get("handlers", "")
if handlers != "":
out["handlers"] = [block["handlers"]]
out["handlers"] = [k.strip() for k in block["handlers"].split(",")]
return out


Expand Down
4 changes: 2 additions & 2 deletions c2cwsgiutils/sqlalchemylogger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ keys = sqlalchemy_logger
[handler_sqlalchemy_logger]
class = c2cwsgiutils.sqlalchemylogger.handlers.SQLAlchemyHandler
#args = ({'url':'sqlite:///logger_db.sqlite3','tablename':'test'},'curl')
args = ({'url':'postgresql://postgres:password@localhost:5432/test','tablename':'test','tableargs': {'schema':'xyz'}},'curl')
# kwargs = {'sqlalchemy_url': {'url':'sqlite:///logger_db.sqlite3','tablename':'test'}, 'does_not_contain_expression': 'curl'}
kwargs = {'sqlalchemy_url': {'url':'postgresql://postgres:password@localhost:5432/test','tablename':'test','tableargs': {'schema':'xyz'}}, 'does_not_contain_expression': 'curl'}
level = NOTSET
formatter = generic
propagate = 0
Expand Down

0 comments on commit 56e1bd8

Please sign in to comment.