setup a default logging handler at app startup, before parsing args (-vdq)
This commit is contained in:
parent
75884a8d06
commit
5bd0aeed41
1 changed files with 28 additions and 12 deletions
|
|
@ -171,7 +171,7 @@ class BaseApplication(object):
|
||||||
|
|
||||||
if path is None:
|
if path is None:
|
||||||
path = os.path.join(self.CONFDIR, self.APPNAME + '.storage')
|
path = os.path.join(self.CONFDIR, self.APPNAME + '.storage')
|
||||||
elif not path.startswith('/'):
|
elif not os.path.sep in path:
|
||||||
path = os.path.join(self.CONFDIR, path)
|
path = os.path.join(self.CONFDIR, path)
|
||||||
|
|
||||||
storage = klass(path)
|
storage = klass(path)
|
||||||
|
|
@ -197,7 +197,7 @@ class BaseApplication(object):
|
||||||
|
|
||||||
if path is None:
|
if path is None:
|
||||||
path = os.path.join(self.CONFDIR, self.APPNAME)
|
path = os.path.join(self.CONFDIR, self.APPNAME)
|
||||||
elif not path.startswith('/'):
|
elif not os.path.sep in path:
|
||||||
path = os.path.join(self.CONFDIR, path)
|
path = os.path.join(self.CONFDIR, path)
|
||||||
|
|
||||||
self.config = klass(path)
|
self.config = klass(path)
|
||||||
|
|
@ -303,31 +303,45 @@ class BaseApplication(object):
|
||||||
else:
|
else:
|
||||||
level = logging.WARNING
|
level = logging.WARNING
|
||||||
|
|
||||||
logging.root.setLevel(level)
|
handlers = []
|
||||||
|
|
||||||
if self.options.save_responses:
|
if self.options.save_responses:
|
||||||
responses_dirname = tempfile.mkdtemp(prefix='weboob_session_')
|
responses_dirname = tempfile.mkdtemp(prefix='weboob_session_')
|
||||||
print >>sys.stderr, 'Debug data will be saved in this directory: %s' % responses_dirname
|
print >>sys.stderr, 'Debug data will be saved in this directory: %s' % responses_dirname
|
||||||
StandardBrowser.SAVE_RESPONSES = True
|
StandardBrowser.SAVE_RESPONSES = True
|
||||||
StandardBrowser.responses_dirname = responses_dirname
|
StandardBrowser.responses_dirname = responses_dirname
|
||||||
self.add_logging_file_handler(os.path.join(responses_dirname, 'debug.log'))
|
handlers.append(self.create_logging_file_handler(os.path.join(responses_dirname, 'debug.log')))
|
||||||
|
|
||||||
# file logger
|
# file logger
|
||||||
if self.options.logging_file:
|
if self.options.logging_file:
|
||||||
self.add_logging_file_handler(self.options.logging_file)
|
handlers.append(self.create_logging_file_handler(self.options.logging_file))
|
||||||
else:
|
else:
|
||||||
# stdout logger
|
handlers.append(self.create_default_logger())
|
||||||
format = '%(asctime)s:%(levelname)s:%(name)s:%(filename)s:%(lineno)d:%(funcName)s %(message)s'
|
|
||||||
handler = logging.StreamHandler(sys.stdout)
|
self.setup_logging(level, handlers)
|
||||||
handler.setFormatter(createColoredFormatter(sys.stdout, format))
|
|
||||||
logging.root.addHandler(handler)
|
|
||||||
|
|
||||||
self._handle_options()
|
self._handle_options()
|
||||||
self.handle_application_options()
|
self.handle_application_options()
|
||||||
|
|
||||||
return args
|
return args
|
||||||
|
|
||||||
def add_logging_file_handler(self, filename):
|
@classmethod
|
||||||
|
def create_default_logger(klass):
|
||||||
|
# stdout logger
|
||||||
|
format = '%(asctime)s:%(levelname)s:%(name)s:%(filename)s:%(lineno)d:%(funcName)s %(message)s'
|
||||||
|
handler = logging.StreamHandler(sys.stdout)
|
||||||
|
handler.setFormatter(createColoredFormatter(sys.stdout, format))
|
||||||
|
return handler
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setup_logging(klass, level, handlers):
|
||||||
|
logging.root.handlers = []
|
||||||
|
|
||||||
|
logging.root.setLevel(level)
|
||||||
|
for handler in handlers:
|
||||||
|
logging.root.addHandler(handler)
|
||||||
|
|
||||||
|
def create_logging_file_handler(self, filename):
|
||||||
try:
|
try:
|
||||||
stream = open(filename, 'w')
|
stream = open(filename, 'w')
|
||||||
except IOError, e:
|
except IOError, e:
|
||||||
|
|
@ -337,7 +351,7 @@ class BaseApplication(object):
|
||||||
format = '%(asctime)s:%(levelname)s:%(name)s:%(pathname)s:%(lineno)d:%(funcName)s %(message)s'
|
format = '%(asctime)s:%(levelname)s:%(name)s:%(pathname)s:%(lineno)d:%(funcName)s %(message)s'
|
||||||
handler = logging.StreamHandler(stream)
|
handler = logging.StreamHandler(stream)
|
||||||
handler.setFormatter(logging.Formatter(format))
|
handler.setFormatter(logging.Formatter(format))
|
||||||
logging.root.addHandler(handler)
|
return handler
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def run(klass, args=None):
|
def run(klass, args=None):
|
||||||
|
|
@ -355,6 +369,8 @@ class BaseApplication(object):
|
||||||
>>> MyApplication.run()
|
>>> MyApplication.run()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
klass.setup_logging(logging.INFO, [klass.create_default_logger()])
|
||||||
|
|
||||||
if args is None:
|
if args is None:
|
||||||
args = [(sys.stdin.encoding and arg.decode(sys.stdin.encoding) or arg) for arg in sys.argv]
|
args = [(sys.stdin.encoding and arg.decode(sys.stdin.encoding) or arg) for arg in sys.argv]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue