Skip to content

Commit

Permalink
support systemd journal logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jheld committed Aug 22, 2023
1 parent 889119c commit ecefbf0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
38 changes: 35 additions & 3 deletions diycrate/logging_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import os

LOG_LEVEL = os.environ.get("DIY_CRATE_LOGGING_LEVEL")
LOG_TO_CONSOLE = os.environ.get("DIY_CRATE_LOGGING_CONSOLE")

if not LOG_LEVEL:
LOG_LEVEL = "INFO"

try:
from systemd import journal
except ImportError:
journal = None


LOGGING = {
"version": 1,
"disable_existing_loggers": True,
Expand All @@ -15,18 +24,41 @@
},
"handlers": {
"console": {
"level": "DEBUG",
"level": LOG_LEVEL,
"class": "logging.StreamHandler",
"formatter": "verbose",
}
},
"loggers": {
"diycrate": {"handlers": ["console"], "propagate": True, "level": LOG_LEVEL},
"diycrate": {
"handlers": ["console"],
"propagate": True,
"level": LOG_LEVEL,
},
"diycrate_app": {
"handlers": ["console"],
"propagate": True,
"level": LOG_LEVEL,
},
"__main__": {"handlers": ["console"], "propagate": True, "level": LOG_LEVEL},
"__main__": {
"handlers": ["console"],
"propagate": True,
"level": LOG_LEVEL,
},
},
}
#
if journal:
LOGGING["handlers"]["journal"] = {
"level": LOG_LEVEL,
"class": "systemd.journal.JournalHandler",
"formatter": "verbose",
}

LOGGING["loggers"]["root"] = {
"handlers": ["journal"],
}
if not LOG_TO_CONSOLE:
LOGGING["loggers"]["diycrate_app"]["handlers"] = []
LOGGING["loggers"]["__main__"]["handlers"] = []
LOGGING["loggers"]["diycrate"]["handlers"] = []
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ diycrate_server = "diycrate.server_app:main"

[project.optional-dependencies]
"dev" = ["ipython", "check-manifest", "black", "flake8", "pre-commit", "mypy"]
"systemd" = ["systemd-python"]
"ci" = ["python-digitalocean"]
"test" = ["tox", "check-manifest"]

0 comments on commit ecefbf0

Please sign in to comment.