Add dynamic log level handling
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
ee25613eda
commit
2626ceead6
42
.drone.yml
42
.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():
|
||||
|
17
README.md
17
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
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user