Teil 27: logging.config
This commit is contained in:
41
teil27/halloween.py
Normal file
41
teil27/halloween.py
Normal file
@@ -0,0 +1,41 @@
|
||||
#encoding: utf-8
|
||||
import logging
|
||||
from logging import config
|
||||
import json
|
||||
import random
|
||||
import JSONFormatter
|
||||
|
||||
#JSON Formatter
|
||||
ATTR_TO_JSON = ['created', 'filename', 'funcName', 'levelname', 'lineno', 'module', 'msecs', 'msg', 'name', 'pathname', 'process', 'processName', 'relativeCreated', 'thread', 'threadName']
|
||||
class JsonFormatter:
|
||||
def format(self, record):
|
||||
obj = {attr: getattr(record, attr)
|
||||
for attr in ATTR_TO_JSON}
|
||||
return json.dumps(obj, indent=4)
|
||||
|
||||
#CustomFilter
|
||||
|
||||
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','Monster','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')
|
Reference in New Issue
Block a user