Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
746aaa6c2a | |||
16820fa083 | |||
ee25613eda | |||
da452afc98 | |||
516e068b2c | |||
a04f8dd5de | |||
e9ac6de0d0 | |||
1d37b1bc2d | |||
eb7fea1898 |
42
.drone.yml
42
.drone.yml
@ -6,16 +6,18 @@ workspace:
|
|||||||
path: /drone/src
|
path: /drone/src
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: test with DEBUG_MODE
|
- name: LOG_LEVEL var set to debug
|
||||||
image: python:alpine
|
depends_on:
|
||||||
|
- clone
|
||||||
|
image: python:3.10-alpine
|
||||||
pull: if-not-exists
|
pull: if-not-exists
|
||||||
environment:
|
environment:
|
||||||
DEBUG_MODE: 'True'
|
LOG_LEVEL: 'debug'
|
||||||
TZ: 'Europe/Berlin'
|
TZ: 'Europe/Berlin'
|
||||||
commands:
|
commands:
|
||||||
- apk --no-cache add gcc musl-dev tzdata
|
- apk --no-cache add gcc musl-dev tzdata
|
||||||
- pip3 install --no-cache-dir -r requirements.txt
|
- pip3 install --no-cache-dir -r requirements.txt
|
||||||
- echo "$DEBUG_MODE"
|
- echo "$LOG_LEVEL"
|
||||||
- |
|
- |
|
||||||
python3 -c 'from loghandler import logger;
|
python3 -c 'from loghandler import logger;
|
||||||
def main():
|
def main():
|
||||||
@ -27,16 +29,40 @@ steps:
|
|||||||
|
|
||||||
main()'
|
main()'
|
||||||
|
|
||||||
- name: test without DEBUG_MODE
|
- name: LOG_LEVEL var set to warning
|
||||||
image: python:alpine
|
depends_on:
|
||||||
|
- clone
|
||||||
|
image: python:3.10-alpine
|
||||||
pull: if-not-exists
|
pull: if-not-exists
|
||||||
environment:
|
environment:
|
||||||
DEBUG_MODE: 'False'
|
LOG_LEVEL: 'warning'
|
||||||
TZ: 'Europe/Berlin'
|
TZ: 'Europe/Berlin'
|
||||||
commands:
|
commands:
|
||||||
- apk --no-cache add gcc musl-dev tzdata
|
- apk --no-cache add gcc musl-dev tzdata
|
||||||
- pip3 install --no-cache-dir -r requirements.txt
|
- 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: 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;
|
python3 -c 'from loghandler import logger;
|
||||||
def main():
|
def main():
|
||||||
|
27
README.md
27
README.md
@ -1,18 +1,31 @@
|
|||||||
# logger
|
# logger.py
|
||||||
|
|
||||||
[![Build Status](https://drone.pyas.de/api/badges/Kim/logger/status.svg)](https://drone.pyas.de/Kim/logger)
|
[![Build Status](https://drone.pyas.de/api/badges/Kim/logger.py/status.svg)](https://drone.pyas.de/Kim/logger.py)
|
||||||
|
|
||||||
Simple logger function with colored LogLevel designed to run inside Docker container
|
Simple logger function with colored LogLevel designed to run inside Docker container
|
||||||
|
|
||||||
> Debug can be enabled by setting the environment variable
|
## Possible values
|
||||||
>
|
Values can be set case insensitive (DEBUG, debug, Debug).
|
||||||
> **DEBUG_MODE** = *True*
|
- debug
|
||||||
|
- info (default value)
|
||||||
|
- warn
|
||||||
|
- warning
|
||||||
|
- crit
|
||||||
|
- critical
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
- Python 3.10 or higher
|
||||||
|
|
||||||
## Usage example
|
## Usage example
|
||||||
|
|
||||||
|
> Use the environment variable `LOG_LEVEL` to set the log level.
|
||||||
|
>
|
||||||
|
> **LOG_LEVEL** = *debug*
|
||||||
|
|
||||||
```python
|
```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
|
import os
|
||||||
os.environ['DEBUG_MODE'] = 'True'
|
os.environ['LOG_LEVEL'] = 'DEBUG'
|
||||||
|
|
||||||
from loghandler import logger
|
from loghandler import logger
|
||||||
|
|
||||||
|
@ -6,18 +6,26 @@ import colorlog
|
|||||||
|
|
||||||
|
|
||||||
def loghandler():
|
def loghandler():
|
||||||
debug_mode = os.getenv('DEBUG_MODE', "False")
|
|
||||||
if debug_mode.lower() == "false":
|
|
||||||
debug_mode = False
|
|
||||||
|
|
||||||
_logger = colorlog.getLogger(__name__)
|
_logger = colorlog.getLogger(__name__)
|
||||||
if debug_mode:
|
|
||||||
_logger.setLevel(logging.DEBUG)
|
loglevel = os.getenv('LOG_LEVEL', "INFO")
|
||||||
else:
|
|
||||||
_logger.setLevel(logging.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.")
|
||||||
|
|
||||||
handler = colorlog.StreamHandler(sys.stdout)
|
handler = colorlog.StreamHandler(sys.stdout)
|
||||||
if debug_mode:
|
if loglevel:
|
||||||
handler.setLevel(logging.DEBUG)
|
handler.setLevel(logging.DEBUG)
|
||||||
else:
|
else:
|
||||||
handler.setLevel(logging.INFO)
|
handler.setLevel(logging.INFO)
|
||||||
|
@ -1 +1 @@
|
|||||||
colorlog~=4.2.1
|
colorlog==6.6.0
|
Loading…
x
Reference in New Issue
Block a user