Teil 27: logging.config
This commit is contained in:
37
teil27/filterlogging.py
Normal file
37
teil27/filterlogging.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import logging
|
||||
from logging import config
|
||||
import json
|
||||
|
||||
# Filter definieren
|
||||
class DruckFilter(logging.Filter):
|
||||
|
||||
def __init__(self, params=None):
|
||||
self.param = params
|
||||
print('Druckfilter initialisiert')
|
||||
|
||||
def filter(self, record:logging.LogRecord)-> bool| logging.LogRecord:
|
||||
|
||||
# nur Text, der das Wort Druck enthält soll geloggt werden.
|
||||
return record.getMessage().lower().find('druck') > -1
|
||||
|
||||
|
||||
# Config aus JSON Datei laden und setzen
|
||||
|
||||
with open('log_config.json') as file_config:
|
||||
config.dictConfig(json.load(file_config))
|
||||
|
||||
|
||||
# Logging testen
|
||||
def testlogging():
|
||||
print('testlogging() aufgerufen.')
|
||||
logging.debug('Diese Zeile wird ausgefiltert.')
|
||||
logging.debug('DEBUG/Druckdaten werden gesendet.')
|
||||
logging.info('INFO/Es werden 2 Seiten gedruckt')
|
||||
logging.debug('Diese Zeile wird auch ausgefiltert.')
|
||||
logging.warning('WARN/nicht genügend Papier im Drucker')
|
||||
logging.error('ERROR/Drucker nicht gefunden.')
|
||||
logging.critical('CRITICAL/Drucker brennt')
|
||||
|
||||
if __name__ == '__main__':
|
||||
testlogging()
|
||||
|
Reference in New Issue
Block a user