From 2626ceead636c11fbf6a2e368df677cf0372baf0 Mon Sep 17 00:00:00 2001 From: Kim Oliver Drechsel Date: Sat, 13 Aug 2022 12:07:59 +0200 Subject: [PATCH 1/6] Add dynamic log level handling --- .drone.yml | 42 ++++++++++++++++++++++++++++++++++-------- README.md | 17 +++++++++++++---- loghandler.py | 24 ++++++++++++++++-------- 3 files changed, 63 insertions(+), 20 deletions(-) diff --git a/.drone.yml b/.drone.yml index 13f02f5..166e818 100644 --- a/.drone.yml +++ b/.drone.yml @@ -6,16 +6,18 @@ workspace: path: /drone/src steps: - - name: test with DEBUG_MODE - image: python:alpine + - name: Test with LOG_LEVEL var set to debug + depends_on: + - clone + image: python:3.10-alpine pull: if-not-exists environment: - DEBUG_MODE: 'True' + LOG_LEVEL: 'debug' TZ: 'Europe/Berlin' commands: - apk --no-cache add gcc musl-dev tzdata - pip3 install --no-cache-dir -r requirements.txt - - echo "$DEBUG_MODE" + - echo "$LOG_LEVEL" - | python3 -c 'from loghandler import logger; def main(): @@ -27,16 +29,40 @@ steps: main()' - - name: test without DEBUG_MODE - image: python:alpine + - name: Test with LOG_LEVEL var set to warning + depends_on: + - clone + image: python:3.10-alpine pull: if-not-exists environment: - DEBUG_MODE: 'False' + LOG_LEVEL: 'warning' TZ: 'Europe/Berlin' commands: - apk --no-cache add gcc musl-dev tzdata - pip3 install --no-cache-dir -r requirements.txt - - echo "$DEBUG_MODE" + - echo "$LOG_LEVEL" + - | + python3 -c 'from loghandler import logger; + def main(): + logger.debug("This is a Debug Message") + logger.info("This is a Info Message") + logger.warning("This is a Warning Message") + logger.error("This is a Error Message") + logger.critical("This is a Critical Message") + + main()' + + - name: Test with unset LOG_LEVEL var + depends_on: + - clone + image: python:3.10-alpine + pull: if-not-exists + environment: + TZ: 'Europe/Berlin' + commands: + - apk --no-cache add gcc musl-dev tzdata + - pip3 install --no-cache-dir -r requirements.txt + - echo "$LOG_LEVEL" - | python3 -c 'from loghandler import logger; def main(): diff --git a/README.md b/README.md index e0c2610..be8649f 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,24 @@ Simple logger class with colored LogLevel designed to run inside Docker container -> Debug can be enabled by setting the environment variable +> Use the environment variable `LOG_LEVEL` to set the log level. > -> **DEBUG_MODE** = *True* +> **LOG_LEVEL** = *debug* + +## Possible values +Values can be set case insensitive (DEBUG, debug, Debug). +- debug +- info +- warn +- warning +- crit +- critical ## Usage example ```python -# Usage with os only necessary if environment variable has not been set before (e.g. outside Docker) +# Setting LOG_LEVEL with `os.environ` is only necessary if environment variable has not been set before (e.g. outside Docker) import os -os.environ['DEBUG_MODE'] = 'True' +os.environ['LOG_LEVEL'] = 'DEBUG' from loghandler import logger diff --git a/loghandler.py b/loghandler.py index 4844263..6b18ce7 100644 --- a/loghandler.py +++ b/loghandler.py @@ -6,18 +6,26 @@ import colorlog def loghandler(): - debug_mode = os.getenv('DEBUG_MODE', "False") - if debug_mode.lower() == "false": - debug_mode = False + loglevel = os.getenv('LOG_LEVEL', "INFO") + + match loglevel.lower(): + case "debug": + _logger.setLevel(logging.DEBUG) + case "info": + _logger.setLevel(logging.INFO) + case "warn" | "warning": + _logger.setLevel(logging.WARNING) + case "error": + _logger.setLevel(logging.ERROR) + case "crit" | "critical": + _logger.setLevel(logging.CRITICAL) + case _: + raise ValueError("Definied value of LOG_LEVEL is not known. Possible values are debug, info, warn, warning, error, crit and critical.") _logger = colorlog.getLogger(__name__) - if debug_mode: - _logger.setLevel(logging.DEBUG) - else: - _logger.setLevel(logging.INFO) handler = colorlog.StreamHandler(sys.stdout) - if debug_mode: + if loglevel: handler.setLevel(logging.DEBUG) else: handler.setLevel(logging.INFO) -- 2.47.1 From ed35590ac636bcee3fcf823858912017d0716633 Mon Sep 17 00:00:00 2001 From: Kim Oliver Drechsel Date: Sat, 13 Aug 2022 12:09:43 +0200 Subject: [PATCH 2/6] Fix _logger assignment --- loghandler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/loghandler.py b/loghandler.py index 6b18ce7..5957937 100644 --- a/loghandler.py +++ b/loghandler.py @@ -6,6 +6,8 @@ import colorlog def loghandler(): + _logger = colorlog.getLogger(__name__) + loglevel = os.getenv('LOG_LEVEL', "INFO") match loglevel.lower(): @@ -22,8 +24,6 @@ def loghandler(): case _: raise ValueError("Definied value of LOG_LEVEL is not known. Possible values are debug, info, warn, warning, error, crit and critical.") - _logger = colorlog.getLogger(__name__) - handler = colorlog.StreamHandler(sys.stdout) if loglevel: handler.setLevel(logging.DEBUG) -- 2.47.1 From ddca0fee38fd618943d01a119ecd7a4d96a11dbc Mon Sep 17 00:00:00 2001 From: Kim Oliver Drechsel Date: Sat, 13 Aug 2022 12:16:07 +0200 Subject: [PATCH 3/6] Change step name --- .drone.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.drone.yml b/.drone.yml index 166e818..0b98e5d 100644 --- a/.drone.yml +++ b/.drone.yml @@ -6,7 +6,7 @@ workspace: path: /drone/src steps: - - name: Test with LOG_LEVEL var set to debug + - name: LOG_LEVEL var set to debug depends_on: - clone image: python:3.10-alpine @@ -29,7 +29,7 @@ steps: main()' - - name: Test with LOG_LEVEL var set to warning + - name: LOG_LEVEL var set to warning depends_on: - clone image: python:3.10-alpine @@ -52,7 +52,7 @@ steps: main()' - - name: Test with unset LOG_LEVEL var + - name: Unset LOG_LEVEL var depends_on: - clone image: python:3.10-alpine -- 2.47.1 From 43373064975888cacecb9d79af32b0f7226eef3a Mon Sep 17 00:00:00 2001 From: Kim Oliver Drechsel Date: Sat, 13 Aug 2022 12:18:35 +0200 Subject: [PATCH 4/6] Add prepare step --- .drone.yml | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/.drone.yml b/.drone.yml index 0b98e5d..62054a1 100644 --- a/.drone.yml +++ b/.drone.yml @@ -6,17 +6,24 @@ workspace: path: /drone/src steps: - - name: LOG_LEVEL var set to debug + - name: Prepare environment depends_on: - clone image: python:3.10-alpine pull: if-not-exists + commands: + - apk --no-cache add gcc musl-dev tzdata + - pip3 install --no-cache-dir -r requirements.txt + + - name: LOG_LEVEL var set to debug + depends_on: + - Prepare environment + image: python:3.10-alpine + pull: if-not-exists environment: LOG_LEVEL: 'debug' TZ: 'Europe/Berlin' commands: - - apk --no-cache add gcc musl-dev tzdata - - pip3 install --no-cache-dir -r requirements.txt - echo "$LOG_LEVEL" - | python3 -c 'from loghandler import logger; @@ -31,15 +38,13 @@ steps: - name: LOG_LEVEL var set to warning depends_on: - - clone + - Prepare environment image: python:3.10-alpine pull: if-not-exists environment: LOG_LEVEL: 'warning' TZ: 'Europe/Berlin' commands: - - apk --no-cache add gcc musl-dev tzdata - - pip3 install --no-cache-dir -r requirements.txt - echo "$LOG_LEVEL" - | python3 -c 'from loghandler import logger; @@ -54,14 +59,12 @@ steps: - name: Unset LOG_LEVEL var depends_on: - - clone + - Prepare environment image: python:3.10-alpine pull: if-not-exists environment: TZ: 'Europe/Berlin' commands: - - apk --no-cache add gcc musl-dev tzdata - - pip3 install --no-cache-dir -r requirements.txt - echo "$LOG_LEVEL" - | python3 -c 'from loghandler import logger; -- 2.47.1 From 74876c1231e0374e419a33a1b2c221c92859f1f8 Mon Sep 17 00:00:00 2001 From: Kim Oliver Drechsel Date: Sat, 13 Aug 2022 12:20:29 +0200 Subject: [PATCH 5/6] Revert "Add prepare step" This reverts commit 43373064975888cacecb9d79af32b0f7226eef3a. --- .drone.yml | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/.drone.yml b/.drone.yml index 62054a1..0b98e5d 100644 --- a/.drone.yml +++ b/.drone.yml @@ -6,24 +6,17 @@ workspace: path: /drone/src steps: - - name: Prepare environment - depends_on: - - clone - image: python:3.10-alpine - pull: if-not-exists - commands: - - apk --no-cache add gcc musl-dev tzdata - - pip3 install --no-cache-dir -r requirements.txt - - name: LOG_LEVEL var set to debug depends_on: - - Prepare environment + - clone image: python:3.10-alpine pull: if-not-exists environment: LOG_LEVEL: 'debug' TZ: 'Europe/Berlin' commands: + - apk --no-cache add gcc musl-dev tzdata + - pip3 install --no-cache-dir -r requirements.txt - echo "$LOG_LEVEL" - | python3 -c 'from loghandler import logger; @@ -38,13 +31,15 @@ steps: - name: LOG_LEVEL var set to warning depends_on: - - Prepare environment + - clone image: python:3.10-alpine pull: if-not-exists environment: LOG_LEVEL: 'warning' TZ: 'Europe/Berlin' commands: + - apk --no-cache add gcc musl-dev tzdata + - pip3 install --no-cache-dir -r requirements.txt - echo "$LOG_LEVEL" - | python3 -c 'from loghandler import logger; @@ -59,12 +54,14 @@ steps: - name: Unset LOG_LEVEL var depends_on: - - Prepare environment + - clone image: python:3.10-alpine pull: if-not-exists environment: TZ: 'Europe/Berlin' commands: + - apk --no-cache add gcc musl-dev tzdata + - pip3 install --no-cache-dir -r requirements.txt - echo "$LOG_LEVEL" - | python3 -c 'from loghandler import logger; -- 2.47.1 From e3dff0c61bf8299d1953dfb9acfccc4b4484c041 Mon Sep 17 00:00:00 2001 From: Kim Oliver Drechsel Date: Sat, 13 Aug 2022 12:26:47 +0200 Subject: [PATCH 6/6] Add Requirements section --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index be8649f..98fe10d 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,9 @@ Values can be set case insensitive (DEBUG, debug, Debug). - crit - critical +## Requirements +- Python 3.10 or higher + ## Usage example ```python # Setting LOG_LEVEL with `os.environ` is only necessary if environment variable has not been set before (e.g. outside Docker) -- 2.47.1