filterlogging.py mit log_config.json
This commit is contained in:
3
logging-config/.gitignore
vendored
Normal file
3
logging-config/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
example.py
|
||||
ex2.py
|
||||
*.log
|
37
logging-config/filterlogging.py
Normal file
37
logging-config/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()
|
||||
|
35
logging-config/log_config.json
Normal file
35
logging-config/log_config.json
Normal 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"
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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
2
teil16/.gitignore
vendored
@@ -1,2 +1,2 @@
|
||||
./__pycache__
|
||||
__pycache__
|
||||
./network/__pycache__/*
|
||||
|
Reference in New Issue
Block a user