2016-07-24 18:59:48 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import xbmc
|
|
|
|
|
2016-09-02 03:09:51 +10:00
|
|
|
from utils import window, tryEncode
|
2016-07-24 18:59:48 +10:00
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
def config():
|
2016-08-22 12:51:23 +10:00
|
|
|
|
2016-08-30 03:31:41 +10:00
|
|
|
logger = logging.getLogger('PLEX')
|
2016-07-24 18:59:48 +10:00
|
|
|
logger.addHandler(LogHandler())
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
|
|
|
|
class LogHandler(logging.StreamHandler):
|
|
|
|
|
|
|
|
def __init__(self):
|
2016-08-22 12:51:23 +10:00
|
|
|
|
2016-07-24 18:59:48 +10:00
|
|
|
logging.StreamHandler.__init__(self)
|
|
|
|
self.setFormatter(MyFormatter())
|
|
|
|
|
|
|
|
def emit(self, record):
|
2016-09-06 04:50:42 +10:00
|
|
|
|
2016-08-22 12:51:23 +10:00
|
|
|
if self._get_log_level(record.levelno):
|
2016-07-24 18:59:48 +10:00
|
|
|
try:
|
2016-08-07 18:40:18 +10:00
|
|
|
xbmc.log(self.format(record), level=xbmc.LOGNOTICE)
|
2016-07-24 18:59:48 +10:00
|
|
|
except UnicodeEncodeError:
|
2016-09-02 03:09:51 +10:00
|
|
|
xbmc.log(tryEncode(self.format(record)), level=xbmc.LOGNOTICE)
|
2016-07-24 18:59:48 +10:00
|
|
|
|
2016-08-22 12:51:23 +10:00
|
|
|
@classmethod
|
|
|
|
def _get_log_level(cls, level):
|
2016-07-24 18:59:48 +10:00
|
|
|
|
|
|
|
levels = {
|
|
|
|
logging.ERROR: 0,
|
|
|
|
logging.WARNING: 0,
|
|
|
|
logging.INFO: 1,
|
2016-07-25 09:46:24 +10:00
|
|
|
logging.DEBUG: 2
|
2016-07-24 18:59:48 +10:00
|
|
|
}
|
|
|
|
try:
|
2016-08-30 03:31:41 +10:00
|
|
|
log_level = int(window('plex_logLevel'))
|
2016-07-24 18:59:48 +10:00
|
|
|
except ValueError:
|
2016-08-22 12:51:23 +10:00
|
|
|
log_level = 0
|
2016-07-24 18:59:48 +10:00
|
|
|
|
2016-08-22 12:51:23 +10:00
|
|
|
return log_level >= levels[level]
|
2016-07-24 18:59:48 +10:00
|
|
|
|
|
|
|
|
|
|
|
class MyFormatter(logging.Formatter):
|
|
|
|
|
|
|
|
def __init__(self, fmt="%(name)s -> %(message)s"):
|
2016-08-22 12:51:23 +10:00
|
|
|
|
2016-07-24 18:59:48 +10:00
|
|
|
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
|
|
|
|
|
2016-08-22 12:51:23 +10:00
|
|
|
return result
|