PKC logging now uses Kodi log levels
This commit is contained in:
parent
41b4493072
commit
32c43855f7
1 changed files with 15 additions and 56 deletions
|
@ -1,74 +1,33 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
###############################################################################
|
||||||
##################################################################################################
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import xbmc
|
import xbmc
|
||||||
|
|
||||||
from utils import window, tryEncode
|
from utils import tryEncode
|
||||||
|
###############################################################################
|
||||||
##################################################################################################
|
LEVELS = {
|
||||||
|
logging.ERROR: xbmc.LOGERROR,
|
||||||
|
logging.WARNING: xbmc.LOGWARNING,
|
||||||
|
logging.INFO: xbmc.LOGNOTICE,
|
||||||
|
logging.DEBUG: xbmc.LOGDEBUG
|
||||||
|
}
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
|
||||||
def config():
|
def config():
|
||||||
|
|
||||||
logger = logging.getLogger('PLEX')
|
logger = logging.getLogger('PLEX')
|
||||||
logger.addHandler(LogHandler())
|
logger.addHandler(LogHandler())
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
|
||||||
class LogHandler(logging.StreamHandler):
|
class LogHandler(logging.StreamHandler):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
logging.StreamHandler.__init__(self)
|
logging.StreamHandler.__init__(self)
|
||||||
self.setFormatter(MyFormatter())
|
self.setFormatter(logging.Formatter(fmt="%(name)s: %(message)s"))
|
||||||
|
|
||||||
def emit(self, record):
|
def emit(self, record):
|
||||||
|
|
||||||
if self._get_log_level(record.levelno):
|
|
||||||
try:
|
|
||||||
xbmc.log(self.format(record), level=xbmc.LOGNOTICE)
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
xbmc.log(tryEncode(self.format(record)), level=xbmc.LOGNOTICE)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _get_log_level(cls, level):
|
|
||||||
|
|
||||||
levels = {
|
|
||||||
logging.ERROR: 0,
|
|
||||||
logging.WARNING: 0,
|
|
||||||
logging.INFO: 1,
|
|
||||||
logging.DEBUG: 2
|
|
||||||
}
|
|
||||||
try:
|
try:
|
||||||
log_level = int(window('plex_logLevel'))
|
xbmc.log(self.format(record), level=LEVELS[record.levelno])
|
||||||
except ValueError:
|
except UnicodeEncodeError:
|
||||||
log_level = 0
|
xbmc.log(tryEncode(self.format(record)),
|
||||||
|
level=LEVELS[record.levelno])
|
||||||
return log_level >= levels[level]
|
|
||||||
|
|
||||||
|
|
||||||
class MyFormatter(logging.Formatter):
|
|
||||||
|
|
||||||
def __init__(self, fmt="%(name)s -> %(message)s"):
|
|
||||||
|
|
||||||
logging.Formatter.__init__(self, fmt)
|
|
||||||
|
|
||||||
def format(self, record):
|
|
||||||
|
|
||||||
# Save the original format configured by the user
|
|
||||||
# when the logger formatter was instantiated
|
|
||||||
format_orig = self._fmt
|
|
||||||
|
|
||||||
# Replace the original format with one customized by logging level
|
|
||||||
if record.levelno in (logging.DEBUG, logging.ERROR):
|
|
||||||
self._fmt = '%(name)s -> %(levelname)s: %(message)s'
|
|
||||||
|
|
||||||
# Call the original formatter class to do the grunt work
|
|
||||||
result = logging.Formatter.format(self, record)
|
|
||||||
|
|
||||||
# Restore the original format configured by the user
|
|
||||||
self._fmt = format_orig
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
Loading…
Reference in a new issue