Alle Dateien aus dem Pythonkurs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

41 lines
1.3 KiB

#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')