filterlogging.py mit log_config.json

This commit is contained in:
2024-10-07 12:04:38 +02:00
parent c0a7704db8
commit 69338434ac
6 changed files with 76 additions and 9 deletions

3
logging-config/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
example.py
ex2.py
*.log

View 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()

View File

@@ -0,0 +1,35 @@
{
"version":1,
"filters": {
"drucker":{
"()": "__main__.DruckFilter",
"params": "noshow"
}
},
"formatters":{
"std_out":{
"format": "%(asctime)s : %(levelname)s : %(module)s : %(funcName)s : %(lineno)d : (Process Details : (%(process)d, %(processName)s), Thread Details : (%(thread)d, %(threadName)s))\nLog : %(message)s",
"datefmt":"%d-%m-%Y %I:%M:%S"
}
},
"handlers":{
"console":{
"formatter": "std_out",
"class": "logging.StreamHandler",
"level": "DEBUG",
"filters": ["drucker"]
},
"file":{
"formatter":"std_out",
"class":"logging.FileHandler",
"level":"DEBUG",
"filename" : "raspithek.log"
}
},
"root":{
"handlers":["console","file"],
"level": "DEBUG"
}
}

View File

View File

@@ -24,14 +24,6 @@ log_config = {
"level":"DEBUG",
"filename":"raspithek.log"
},
# Handler für rotierende Logfiles
"rotating": {
"formater":"std_out",
"class": 'logging.handlers.RotatingFileHandler',
"filename": 'logs/random.log',
"maxBytes": 10240
'backupCount': 3
}
},
# Format einer Logzeile

2
teil16/.gitignore vendored
View File

@@ -1,2 +1,2 @@
./__pycache__
__pycache__
./network/__pycache__/*