FIX:(#1912)(#1905) Logging would fail to initialize properly on non-English systems

This commit is contained in:
evilhero 2018-04-08 14:21:01 -04:00
parent a895f3f8d3
commit 127b109011
7 changed files with 89 additions and 75 deletions

View File

@ -62,17 +62,10 @@ def main():
if not mylar.SYS_ENCODING or mylar.SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
mylar.SYS_ENCODING = 'UTF-8'
#setup logger for non-english
try:
language = locale.getdefaultlocale()[0][:2]
except:
language = 'en'
mylar.LOG_LANG = language
if language != 'en':
print 'language detected as non-English. Forcing specific logging module - errors WILL NOT be captured in the logs'
if not logger.LOG_LANG.startswith('en'):
print 'language detected as non-English (%s). Forcing specific logging module - errors WILL NOT be captured in the logs' % logger.LOG_LANG
else:
print 'log language set to %s' % mylar.LOG_LANG
print 'log language set to %s' % logger.LOG_LANG
# Set up and gather command line arguments
parser = argparse.ArgumentParser(description='Automated Comic Book Downloader')
@ -109,22 +102,14 @@ def main():
mylar.MAINTENANCE = False
if args.verbose:
mylar.VERBOSE = True
#print 'Verbose/Debugging mode enabled...'
#mylar.LOG_LEVEL = 2
print 'Verbose/Debugging mode enabled...'
mylar.LOG_LEVEL = 2
elif args.quiet:
mylar.QUIET = True
#print 'Quiet mode enabled...'
#mylar.LOG_LEVEL = 0
print 'Quiet logging mode enabled...'
mylar.LOG_LEVEL = 0
else:
#print 'Normal logging mode enabled...'
#mylar.LOG_LEVEL = 1
mylar.VERBOSE = False
# Do an intial setup of the logger.
if mylar.LOG_LANG == 'en':
logger.initLogger(console=not mylar.QUIET, log_dir=False, init=True, verbose=mylar.VERBOSE)
#logger.mylar_log.initLogger(loglevel=mylar.LOG_LEVEL)
mylar.LOG_LEVEL = 1
if args.daemon:
if sys.platform == 'win32':

View File

@ -40,7 +40,8 @@
<legend>Configuration Options</legend>
<div>
<label><strong>Mylar Version: </strong> ${config['branch']}</br> -- ${config['br_type']} build ${config['br_version']}.</label></br>
<label><strong>Python Version :</strong> ${config['py_version']}</label></br></br>
<label><strong>Python Version :</strong> ${config['py_version']}</label></br>
<label><strong>Language :</strong> ${config['lang']}</label></br></br>
<legend>MYLAR PROGRAM OPTIONS</legend>
<label><strong>Mylar Data Directory :</strong> ${config['data_dir']}</label></br>
<label><strong>Mylar Program Directory :</strong> ${config['prog_dir']}</label></br>

View File

@ -8,9 +8,11 @@
<div id="subhead_container">
<div id="subhead_menu">
<a id="menu_link_delete" href="#" onclick="doAjaxCall('clearLogs',$(this),'table')" data-success="All logs cleared">Clear Log</a>
<a id="menu_link_edit" href="toggleVerbose">Toggle Debug Log
%if mylar.VERBOSE:
<a id="menu_link_edit" href="toggleVerbose">Toggle Debug Logging
%if mylar.LOG_LEVEL < 2:
ON
%else:
OFF
%endif
</a>
</div>

View File

@ -40,7 +40,6 @@ import cherrypy
from mylar import logger, versioncheckit, rsscheckit, searchit, weeklypullit, PostProcessor, updater, helpers
from mylar import versioncheck, logger
import mylar.config
#these are the globals that are runtime-based (ie. not config-valued at all)
@ -52,13 +51,15 @@ MAINTENANCE = False
LOG_DIR = None
LOGTYPE = 'log'
LOG_LANG = 'en'
LOG_CHARSET = 'UTF-8'
LOG_LEVEL = 1
LOGLIST = []
ARGS = None
SIGNAL = None
SYS_ENCODING = None
OS_DETECT = platform.system()
USER_AGENT = None
VERBOSE = False
#VERBOSE = False
DAEMON = False
PIDFILE= None
CREATEPID = False
@ -159,23 +160,17 @@ def initialize(config_file):
IMPORTBUTTON, IMPORT_FILES, IMPORT_TOTALFILES, IMPORT_CID_COUNT, IMPORT_PARSED_COUNT, IMPORT_FAILURE_COUNT, CHECKENABLED, CVURL, DEMURL, WWTURL, \
USE_SABNZBD, USE_NZBGET, USE_BLACKHOLE, USE_RTORRENT, USE_UTORRENT, USE_QBITTORRENT, USE_DELUGE, USE_TRANSMISSION, USE_WATCHDIR, SAB_PARAMS, \
PROG_DIR, DATA_DIR, CMTAGGER_PATH, DOWNLOAD_APIKEY, LOCAL_IP, STATIC_COMICRN_VERSION, STATIC_APC_VERSION, KEYS_32P, AUTHKEY_32P, FEED_32P, FEEDINFO_32P, \
MONITOR_STATUS, SEARCH_STATUS, RSS_STATUS, WEEKLY_STATUS, VERSION_STATUS, UPDATER_STATUS, DBUPDATE_INTERVAL, LOG_LEVEL, LOG_LANG, APILOCK, \
MONITOR_STATUS, SEARCH_STATUS, RSS_STATUS, WEEKLY_STATUS, VERSION_STATUS, UPDATER_STATUS, DBUPDATE_INTERVAL, LOG_LANG, LOG_CHARSET, APILOCK, LOG_LEVEL, \
SCHED_RSS_LAST, SCHED_WEEKLY_LAST, SCHED_MONITOR_LAST, SCHED_SEARCH_LAST, SCHED_VERSION_LAST, SCHED_DBUPDATE_LAST, COMICINFO
cc = mylar.config.Config(config_file)
CONFIG = cc.read()
CONFIG = cc.read(startup=True)
assert CONFIG is not None
if _INITIALIZED:
return False
# Start the logger, silence console logging if we need to
if mylar.LOG_LANG == 'en':
logger.initLogger(console=not QUIET, log_dir=CONFIG.LOG_DIR, verbose=VERBOSE)
else:
logger.mylar_log.initLogger(loglevel=mylar.LOG_LEVEL)
# Also sets INSTALL_TYPE variable to 'win', 'git' or 'source'
CURRENT_VERSION, CONFIG.GIT_BRANCH = versioncheck.getVersion()

View File

@ -115,7 +115,7 @@ _CONFIG_DEFINITIONS = OrderedDict({
'LOG_DIR' : (str, 'Logs', None),
'MAX_LOGSIZE' : (int, 'Logs', 10000000),
'MAX_LOGFILES': (int, 'Logs', 5),
'LOG_LEVEL': (int, 'Logs', 0),
'LOG_LEVEL': (int, 'Logs', 1),
'GIT_PATH' : (str, 'Git', None),
'GIT_USER' : (str, 'Git', 'evilhero'),
@ -479,7 +479,7 @@ class Config(object):
elif k == 'MINIMAL_INI':
config.set(v[1], k.lower(), str(self.MINIMAL_INI))
def read(self):
def read(self, startup=False):
self.config_vals()
setattr(self, 'EXTRA_NEWZNABS', self.get_extra_newznabs())
setattr(self, 'EXTRA_TORZNABS', self.get_extra_torznabs())
@ -498,6 +498,16 @@ class Config(object):
self.writeconfig()
else:
self.provider_sequence()
if startup is True:
# Start the logger, silence console logging if we need to
if logger.LOG_LANG.startswith('en'):
logger.initLogger(console=not mylar.QUIET, log_dir=self.LOG_DIR, max_logsize=self.MAX_LOGSIZE, max_logfiles=self.MAX_LOGFILES, loglevel=mylar.LOG_LEVEL)
else:
if self.LOG_LEVEL != mylar.LOG_LEVEL:
print('Logging level over-ridden by startup value. Changing from %s to %s' % (self.LOG_LEVEL, mylar.LOG_LEVEL))
logger.mylar_log.initLogger(loglevel=mylar.LOG_LEVEL, log_dir=self.LOG_DIR, max_logsize=self.MAX_LOGSIZE, max_logfiles=self.MAX_LOGFILES)
self.configure()
return self
@ -695,6 +705,7 @@ class Config(object):
logger.warn("Error writing configuration file: %s", e)
def configure(self, update=False):
try:
if not any([self.SAB_HOST is None, self.SAB_HOST == '', 'http://' in self.SAB_HOST[:7], 'https://' in self.SAB_HOST[:8]]):
self.SAB_HOST = 'http://' + self.SAB_HOST

View File

@ -15,24 +15,30 @@
import os
import sys
import logging
import inspect
import traceback
import threading
import platform
import locale
import mylar
from logging import getLogger, WARN, ERROR, INFO, DEBUG, StreamHandler, Formatter, Handler
from mylar import helpers
import logging
from logging import getLogger, WARN, ERROR, INFO, DEBUG, StreamHandler, Formatter, Handler
from lib.six import PY2
#setup logger for non-english (this doesnt carry thru, so check here too)
try:
if mylar.LOG_LANG == 'en':
pass
localeinfo = locale.getdefaultlocale()
language = localeinfo[0]
charset = localeinfo[1]
except:
mylar.LOG_LANG = 'en'
language = 'en'
charset = 'UTF-8'
LOG_LANG = language
LOG_CHARSET = charset
if mylar.LOG_LANG != 'en':
if not LOG_LANG.startswith('en'):
# Simple rotating log handler that uses RotatingFileHandler
class RotatingLogger(object):
@ -55,7 +61,7 @@ if mylar.LOG_LANG != 'en':
sys.__excepthook__(exc_type, exc_value, None)
return
def initLogger(self, loglevel=1):
def initLogger(self, loglevel=1, log_dir=None, max_logsize=None, max_logfiles=None):
import sys
sys.excepthook = RotatingLogger.handle_exception
@ -66,7 +72,7 @@ if mylar.LOG_LANG != 'en':
lg = logging.getLogger('mylar')
lg.setLevel(logging.DEBUG)
self.filename = os.path.join(mylar.CONFIG.LOG_DIR, self.filename)
self.filename = os.path.join(log_dir, self.filename)
#concurrentLogHandler/0.8.7 (to deal with windows locks)
#since this only happens on windows boxes, if it's nix/mac use the default logger.
@ -87,8 +93,8 @@ if mylar.LOG_LANG != 'en':
filehandler = RFHandler(
self.filename,
maxBytes=mylar.CONFIG.MAX_LOGSIZE,
backupCount=mylar.CONFIG.MAX_LOGFILES)
maxBytes=max_logsize,
backupCount=max_logfiles)
filehandler.setLevel(logging.DEBUG)
@ -130,7 +136,7 @@ if mylar.LOG_LANG != 'en':
message = safe_unicode(message)
message = message.encode(mylar.SYS_ENCODING)
if level != 'DEBUG' or mylar.LOG_LEVEL >= 2:
mylar.LOGLIST.insert(0, (helpers.now(), message, threadname))
mylar.LOGLIST.insert(0, (helpers.now(), message, level, threadname))
if len(mylar.LOGLIST) > 2500:
del mylar.LOGLIST[-1]
@ -189,8 +195,7 @@ else:
message = message.replace("\n", "<br />")
mylar.LOGLIST.insert(0, (helpers.now(), message, record.levelname, record.threadName))
def initLogger(console=False, log_dir=False, init=False, verbose=False):
def initLogger(console=False, log_dir=False, init=False, loglevel=1, max_logsize=None, max_logfiles=5):
#concurrentLogHandler/0.8.7 (to deal with windows locks)
#since this only happens on windows boxes, if it's nix/mac use the default logger.
if platform.system() == 'Windows':
@ -208,13 +213,11 @@ else:
mylar.LOGTYPE = 'log'
from logging.handlers import RotatingFileHandler as RFHandler
if init is True:
max_size = 1000000 #1 MB
if all([init is True, max_logsize is None]):
max_logsize = 1000000 #1 MB
else:
if mylar.CONFIG.MAX_LOGSIZE:
max_size = mylar.CONFIG.MAX_LOGSIZE
else:
max_size = 1000000 # 1 MB
if max_logsize is None:
max_logsize = 1000000 # 1 MB
"""
Setup logging for Mylar. It uses the logger instance with the name
@ -245,16 +248,13 @@ else:
# Configure the logger to accept all messages
logger.propagate = False
#1 is WARN level, 2 is ERROR
if init is True:
logger.setLevel(logging.DEBUG if verbose else logging.INFO)
logger.setLevel(logging.INFO)
else:
if mylar.CONFIG.LOG_LEVEL == 1:
logger.setLevel(logging.DEBUG if verbose else logging.WARN)
elif mylar.CONFIG.LOG_LEVEL == 2:
logger.setLevel(logging.DEBUG if verbose else logging.ERROR)
else:
logger.setLevel(logging.DEBUG if verbose else logging.INFO)
if loglevel == 1: #normal
logger.setLevel(logging.INFO)
elif loglevel >= 2: #verbose
logger.setLevel(logging.DEBUG)
# Add list logger
loglist_handler = LogListHandler()
@ -265,8 +265,11 @@ else:
if log_dir:
filename = os.path.join(log_dir, 'mylar.log')
file_formatter = Formatter('%(asctime)s - %(levelname)-7s :: %(name)s.%(funcName)s.%(lineno)s : %(threadName)s : %(message)s', '%d-%b-%Y %H:%M:%S')
file_handler = RFHandler(filename, "a", maxBytes=max_size, backupCount=mylar.CONFIG.MAX_LOGFILES)
file_handler.setLevel(logging.DEBUG)
file_handler = RFHandler(filename, "a", maxBytes=max_logsize, backupCount=max_logfiles)
if loglevel == 1: #normal
file_handler.setLevel(logging.INFO)
elif loglevel >= 2: #verbose
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
@ -276,7 +279,10 @@ else:
console_formatter = logging.Formatter('%(asctime)s - %(levelname)s :: %(name)s.%(funcName)s.%(lineno)s : %(threadName)s : %(message)s', '%d-%b-%Y %H:%M:%S')
console_handler = logging.StreamHandler()
console_handler.setFormatter(console_formatter)
console_handler.setLevel(logging.DEBUG)
if loglevel == 1: #normal
console_handler.setLevel(logging.INFO)
elif loglevel >= 2: #verbose
console_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)

View File

@ -3552,11 +3552,24 @@ class WebInterface(object):
clearLogs.exposed = True
def toggleVerbose(self):
mylar.VERBOSE = not mylar.VERBOSE
logger.initLogger(console=not mylar.QUIET,
log_dir=mylar.CONFIG.LOG_DIR, verbose=mylar.VERBOSE)
logger.info("Verbose toggled, set to %s", mylar.VERBOSE)
logger.debug("If you read this message, debug logging is available")
if mylar.LOG_LEVEL != 2:
mylar.LOG_LEVEL = 2
else:
mylar.LOG_LEVEL = 1
if logger.LOG_LANG.startswith('en'):
logger.initLogger(console=not mylar.QUIET, log_dir=mylar.CONFIG.LOG_DIR, max_logsize=mylar.CONFIG.MAX_LOGSIZE, max_logfiles=mylar.CONFIG.MAX_LOGFILES, loglevel=mylar.LOG_LEVEL)
else:
logger.mylar_log.stopLogger()
logger.mylar_log.initLogger(loglevel=mylar.LOG_LEVEL, log_dir=mylar.CONFIG.LOG_DIR, max_logsize=mylar.CONFIG.MAX_LOGSIZE, max_logfiles=mylar.CONFIG.MAX_LOGFILES)
#mylar.VERBOSE = not mylar.VERBOSE
#logger.initLogger(console=not mylar.QUIET,
# log_dir=mylar.CONFIG.LOG_DIR, verbose=mylar.VERBOSE)
if mylar.LOG_LEVEL == 2:
logger.info("Verbose (DEBUG) logging is enabled")
logger.debug("If you can read this message, debug logging is now working")
else:
logger.info("normal (INFO) logging is now enabled")
raise cherrypy.HTTPRedirect("logs")
toggleVerbose.exposed = True
@ -4602,6 +4615,7 @@ class WebInterface(object):
"prog_dir": mylar.PROG_DIR,
"cache_dir": mylar.CONFIG.CACHE_DIR,
"config_file": mylar.CONFIG_FILE,
"lang": '%s.%s' % (logger.LOG_LANG,logger.LOG_CHARSET),
"branch_history": 'None',
# "branch_history" : br_hist,
"log_dir": mylar.CONFIG.LOG_DIR,