Logowanie przy użyciu wieloprocesorowości
Mam następującelogger
class (jako logger.py):
import logging, logging.handlers
import config
log = logging.getLogger('myLog')
def start():
"Function sets up the logging environment."
log.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt='%(asctime)s [%(levelname)s] %(message)s', datefmt='%d-%m-%y %H:%M:%S')
if config.logfile_enable:
filehandler = logging.handlers.RotatingFileHandler(config.logfile_name, maxBytes=config.logfile_maxsize,backupCount=config.logfile_backupCount)
filehandler.setLevel(logging.DEBUG)
filehandler.setFormatter(formatter)
log.addHandler(filehandler)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(logging.Formatter('[%(levelname)s] %(message)s')) # nicer format for console
log.addHandler(console)
# Levels are: debug, info, warning, error, critical.
log.debug("Started logging to %s [maxBytes: %d, backupCount: %d]" % (config.logfile_name, config.logfile_maxsize, config.logfile_backupCount))
def stop():
"Function closes and cleans up the logging environment."
logging.shutdown()
Aby się zalogować, uruchamiamlogger.start()
raz, a następnie zaimportujfrom logger import log
w dowolnym pliku projektu. Wtedy po prostu używamlog.debug()
ilog.error()
kiedy był potrzebny. Działa dobrze z dowolnego miejsca na skrypcie (różne klasy, funkcje i pliki), ale nie będzie działać w różnych procesach przechodzących przez klasę wieloprocesorową.
Otrzymuję następujący błąd:No handlers could be found for logger "myLog"
.
Co mogę zrobić?