(Py) Moduł Logging (Własne logi)*
Spis treści
Pokaż
Moduł Logging (Własne logi)
Wstęp
- Moduł Logging umożliwia tworzenie własnych logów z zmienioną wartością Log name.
- Każdy nowy rodzaj logów może posiadać swoje własne ustawienia.
Tworzenie własnych logów w module Logging
Definicja własnych logów
import logging
# Create main format:
logFormat = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s", "%m/%d/%Y %H:%M:%S")
# Create main consol handler and and basic handler configuration:
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logging.WARNING)
consoleHandler.setFormatter(logFormat)
# Create main file handler and and basic handler configuration:
fileHandler = logging.FileHandler("testLog.log")
fileHandler.setLevel(logging.DEBUG)
fileHandler.setFormatter(logFormat)
# Create database logger:
dataBaseLogger = logging.getLogger("DataBase")
dataBaseLogger.setLevel(logging.DEBUG)
# Add handlers to logger:
dataBaseLogger.addHandler(fileHandler)
dataBaseLogger.addHandler(consoleHandler)
dataBaseLogger.error("This is debug message")
# 02/06/2021 13:30:57 - DataBase - ERROR - This is debug message
Tworzenie własnych logów z listy
import logging
# Logg list:
loggerItemList = ["DataBase", "Conection", "GUI"]
# Create main format:
logFormat = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s", "%m/%d/%Y %H:%M:%S")
# Create main consol handler and and basic handler configuration:
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logging.WARNING)
consoleHandler.setFormatter(logFormat)
# Create main file handler and and basic handler configuration:
fileHandler = logging.FileHandler("testLog.log")
fileHandler.setLevel(logging.DEBUG)
fileHandler.setFormatter(logFormat)
logerDict = {}
for loggerItem in loggerItemList:
# Create database logger:
logger = logging.getLogger(loggerItem)
logger.setLevel(logging.DEBUG)
# Add handlers to logger:
logger.addHandler(consoleHandler)
logger.addHandler(fileHandler)
# Add logger object to logerDict:
logerDict[loggerItem] = logger
# Application code
logerDict["DataBase"].debug("This is debug message")
logerDict["Conection"].info("This is info message")
logerDict["GUI"].error("This is error message")
# 02/06/2021 13:31:08 - DataBase - DEBUG - This is debug message
# 02/06/2021 13:31:08 - Conection - INFO - This is info message
# 02/06/2021 13:31:08 - GUI - ERROR - This is error message
Pozostałe tematy związane z bibliotekami / modułami Python
Podstawowe
- Moduł Random
- Moduł Time
- Moduł Datetime
- Moduł Threading Concurrent.futures
- Moduł Multiprocessing
- Moduł CSV
- Moduł Json
- Moduł Jinja2
- Moduł Xlrd
- Moduł os
- Moduł re (Regular expression)
- Moduł TextFSM
- Moduł Click
- Moduł HashLib
- Moduł Sorted
- Biblioteka SimpleCrypt
- Biblioteka Requests
Rozszerzone
SQL
- Biblioteka sqlite3 (Wstęp)
- Biblioteka psycopg2 (Wstęp)
- SQL Database
- SQL Table
- SQL Insert
- SQL Select
- SQL Update
- SQL Delete
PyQt 5
- Biblioteka PyQt 5 (Main window)
- Biblioteka PyQt 5 (Signals)
- Biblioteka PyQt 5 (Widgets)
- Biblioteka PyQt 5 (Actions)
- Biblioteka PyQt 5 (Dialogs)
- Biblioteka PyQt 5 (Windows)
Automatyzacja
- Moduł IPAddress
- Biblioteka TelnetLib
- Biblioteka Netmiko (Podstawy)
- Biblioteka Netmiko (Rozwinięcie)
- Biblioteka Netmiko (Przykłady)
- Biblioteka NAPALM
- Biblioteka Ncclient podstawy (Netconf)
- Biblioteka Ncclient przykłady (Netconf)
- Biblioteka Requests podstawy (Restconf)
- Biblioteka Requests przykłady (Restconf)