Skip to content

Commit

Permalink
feat($logging): support logger's level configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Mar 4, 2023
1 parent 4bc6f3e commit 94777bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 11 additions & 2 deletions python_boilerplate/configuration/loguru_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@
logger.add(sys.stderr, level=log_level, format=_message_format)


# Intercept standard logging https://gist.github.com/devsetgo/28c2edaca2d09e267dec46bb2e54b9e2
class InterceptHandler(logging.Handler):
"""
Intercept standard logging
https://loguru.readthedocs.io/en/stable/overview.html#entirely-compatible-with-standard-logging
https://gist.github.com/devsetgo/28c2edaca2d09e267dec46bb2e54b9e2
"""

class InterceptHandler(logging.Handler):
def emit(self, record):
# Get corresponding Loguru level if it exists
try:
Expand All @@ -64,6 +68,11 @@ def emit(self, record):


logging.basicConfig(handlers=[InterceptHandler()], level=0)
logger.info(f"{type(logger)} is intercepting standard logging")

for key, value in application_conf.get_config("log").items():
logging.getLogger(key).setLevel(value)
logger.info(f"Configured logger[{key}]'s level to {value}")


def configure() -> None:
Expand Down
3 changes: 3 additions & 0 deletions python_boilerplate/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
log_level = "DEBUG"
log_level = ${?LOG_LEVEL}
log {
faker = "INFO"
}
headless = true
headless = ${?HEADLESS}
email {
Expand Down

0 comments on commit 94777bb

Please sign in to comment.