Skip to content

Commit

Permalink
config: setup logging
Browse files Browse the repository at this point in the history
Setup logging using dictConfig.

The default logging level is defined on the root logger and set to INFO.

Only the sambal logger is set to INFO by default, or DEBUG if the SAMBAL_DEBUG env var is set (DEBUG is on).

Closes #50
  • Loading branch information
robvdl committed Mar 28, 2024
1 parent cb3bc0a commit a4c5bcb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/sambal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
from logging import getLogger
from logging.config import dictConfig

from pyramid.config import Configurator
from pyramid.csrf import SessionCSRFStoragePolicy

from .client import get_samdb
from .security import SambalSecurityPolicy, login, logout
from .settings import SETTINGS
from .settings import LOGGING, SETTINGS

# Set up the logger as early as possibly.
dictConfig(LOGGING)

logger = getLogger(__name__)

# Create and return a wsgi app.
with Configurator(settings=SETTINGS) as config:
logger.debug("Starting sambal")
config.include("pyramid_jinja2")
config.include("sambal.template")
config.include("sambal.renderers")
Expand Down
29 changes: 29 additions & 0 deletions src/sambal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,32 @@
"redis.sessions.cookie_httponly": True,
"redis.sessions.cookie_secure": USE_HTTPS,
}

# Logging config.
# https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig
LOGGING = {
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"standard": {"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"},
},
"handlers": {
"default": {
"formatter": "standard",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
},
"loggers": {
"": {
"handlers": ["default"],
"level": "INFO",
"propagate": False,
},
"sambal": {
"handlers": ["default"],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False,
},
},
}

0 comments on commit a4c5bcb

Please sign in to comment.