From d4165b0582f65ec02b106ed474bba19fa4632894 Mon Sep 17 00:00:00 2001 From: Olli Graf Date: Tue, 8 Oct 2024 09:38:11 +0200 Subject: [PATCH] halloween.py mit halloween_log_conf.json --- logging-config/halloween.py | 29 ++++++++++++++++++++++ logging-config/halloween_log_conf.json | 34 ++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 logging-config/halloween.py create mode 100644 logging-config/halloween_log_conf.json diff --git a/logging-config/halloween.py b/logging-config/halloween.py new file mode 100644 index 0000000..c71898e --- /dev/null +++ b/logging-config/halloween.py @@ -0,0 +1,29 @@ +import logging +from logging import config +import json +import random + +class HalloweenFilter(logging.Filter): + + def __init__(self, params=None): + self.param = params + print('Halloweenfilter initialisiert') + + def filter(self, record:logging.LogRecord)-> bool| logging.LogRecord: + +# Der Unsichtbare soll keine Spuren im Logfile hinterlassen. + return record.getMessage().lower().find('unsichtbar') == -1 + +visitors = ['Werwolf','Gespenst','Hexe','Unsichtbarer','Vampir','Dämon','Gorilla','Hulk','Mumie'] + +treats = ['Bonbons','Twinkies','Lollis','Schokoladen','Kuchen', 'Äpfel'] + +secrets = random.SystemRandom() + +with open('halloween_log_conf.json') as file_config: + config.dictConfig(json.load(file_config)) +for v in visitors: + logging.info(f'An der Tür klopft eine gruselige Gestalt: {v}') + number = secrets._randbelow(20) + treat = secrets.choice(treats) + logging.info(f'Du gibst ihr {number} {treat} und sie zieht weiter') diff --git a/logging-config/halloween_log_conf.json b/logging-config/halloween_log_conf.json new file mode 100644 index 0000000..bda18a7 --- /dev/null +++ b/logging-config/halloween_log_conf.json @@ -0,0 +1,34 @@ +{ + "version":1, + "filters": { + "halloween":{ + "()": "__main__.HalloweenFilter" + } + }, + "formatters":{ + "std_out":{ + "format": "%(asctime)s : %(levelname)s : %(lineno)d : %(message)s", + "datefmt":"%I:%M:%S %d.%m.%Y" + } + }, + "handlers":{ + "console":{ + "formatter": "std_out", + "class": "logging.StreamHandler", + "level": "DEBUG" + }, + "file":{ + "formatter":"std_out", + "class":"logging.FileHandler", + "level":"DEBUG", + "filename" : "halloween.log", + "filters": ["halloween"] + } + }, + "root":{ + "handlers":["console","file"], + "level": "DEBUG" + } + +} +