2018-04-08 17:28:08 +02:00
|
|
|
"""
|
2013-06-29 18:27:40 +02:00
|
|
|
Logging and debuging facility
|
|
|
|
=============================
|
|
|
|
|
|
|
|
Levels:
|
2018-05-26 14:43:21 +02:00
|
|
|
|
|
|
|
DEBUG
|
|
|
|
Detailed information, typically of interest only when diagnosing problems.
|
|
|
|
INFO
|
|
|
|
Confirmation that things are working as expected.
|
|
|
|
WARNING
|
|
|
|
An indication that something unexpected happened, or indicative of some problem in the
|
2018-06-27 10:24:09 +02:00
|
|
|
near future (e.g. 'disk space low'). The software is still working as expected.
|
2018-05-26 14:43:21 +02:00
|
|
|
ERROR
|
|
|
|
Due to a more serious problem, the software has not been able to perform some function.
|
|
|
|
CRITICAL
|
|
|
|
A serious error, indicating that the program itself may be unable to continue running.
|
2013-06-29 18:27:40 +02:00
|
|
|
|
|
|
|
There are three loggers: `console_only`, `file_only` and `both`.
|
|
|
|
|
2018-05-26 14:43:21 +02:00
|
|
|
Use: `from debug import logger` to import this facility into whatever module you wish to log messages from.
|
|
|
|
Logging is thread-safe so you don't have to worry about locks, just import and log.
|
|
|
|
|
2018-04-08 17:28:08 +02:00
|
|
|
"""
|
2018-05-26 14:43:21 +02:00
|
|
|
|
2018-07-21 11:16:22 +02:00
|
|
|
import ConfigParser
|
2013-06-29 18:27:40 +02:00
|
|
|
import logging
|
|
|
|
import logging.config
|
2015-11-18 16:22:17 +01:00
|
|
|
import os
|
2014-04-19 20:45:37 +02:00
|
|
|
import sys
|
2014-10-13 07:59:16 +02:00
|
|
|
import helper_startup
|
2017-01-11 17:00:00 +01:00
|
|
|
import state
|
2018-04-08 17:28:08 +02:00
|
|
|
|
2014-10-13 07:59:16 +02:00
|
|
|
helper_startup.loadConfig()
|
2018-04-08 17:28:08 +02:00
|
|
|
# Now can be overriden from a config file, which uses standard python
|
|
|
|
# logging.config.fileConfig interface
|
|
|
|
# examples are here:
|
|
|
|
# https://bitmessage.org/forum/index.php/topic,4820.msg11163.html#msg11163
|
2016-04-30 12:45:01 +02:00
|
|
|
log_level = 'WARNING'
|
2013-06-29 18:27:40 +02:00
|
|
|
|
2018-04-08 17:28:08 +02:00
|
|
|
|
2015-11-18 16:22:17 +01:00
|
|
|
def log_uncaught_exceptions(ex_cls, ex, tb):
|
2017-11-30 19:44:03 +01:00
|
|
|
logging.critical('Unhandled exception', exc_info=(ex_cls, ex, tb))
|
2015-11-18 16:22:17 +01:00
|
|
|
|
2018-04-08 17:28:08 +02:00
|
|
|
|
2013-07-15 21:45:03 +02:00
|
|
|
def configureLogging():
|
2018-07-21 11:16:22 +02:00
|
|
|
fail_msg = ''
|
2015-11-18 16:22:17 +01:00
|
|
|
try:
|
2018-07-21 11:16:22 +02:00
|
|
|
logging_config = os.path.join(state.appdata, 'logging.dat')
|
|
|
|
logging.config.fileConfig(logging_config)
|
|
|
|
return (
|
|
|
|
False,
|
|
|
|
'Loaded logger configuration from %s' % logging_config
|
|
|
|
)
|
|
|
|
except (OSError, ConfigParser.NoSectionError):
|
|
|
|
if os.path.isfile(logging_config):
|
|
|
|
fail_msg = \
|
|
|
|
'Failed to load logger configuration from %s, using default' \
|
|
|
|
' logging config\n%s' % \
|
|
|
|
(logging_config, sys.exc_info())
|
2016-08-29 05:56:21 +02:00
|
|
|
else:
|
2018-05-26 14:43:21 +02:00
|
|
|
# no need to confuse the user if the logger config is missing entirely
|
2018-07-21 11:16:22 +02:00
|
|
|
fail_msg = 'Using default logger configuration'
|
2018-04-08 17:28:08 +02:00
|
|
|
|
2015-11-18 16:22:17 +01:00
|
|
|
sys.excepthook = log_uncaught_exceptions
|
|
|
|
|
2013-07-15 21:45:03 +02:00
|
|
|
logging.config.dictConfig({
|
|
|
|
'version': 1,
|
|
|
|
'formatters': {
|
|
|
|
'default': {
|
2018-01-25 08:11:42 +01:00
|
|
|
'format': u'%(asctime)s - %(levelname)s - %(message)s',
|
2013-07-15 21:45:03 +02:00
|
|
|
},
|
2013-06-29 18:27:40 +02:00
|
|
|
},
|
2013-07-15 21:45:03 +02:00
|
|
|
'handlers': {
|
|
|
|
'console': {
|
|
|
|
'class': 'logging.StreamHandler',
|
|
|
|
'formatter': 'default',
|
|
|
|
'level': log_level,
|
2017-09-22 23:59:14 +02:00
|
|
|
'stream': 'ext://sys.stderr'
|
2013-07-15 21:45:03 +02:00
|
|
|
},
|
|
|
|
'file': {
|
|
|
|
'class': 'logging.handlers.RotatingFileHandler',
|
|
|
|
'formatter': 'default',
|
|
|
|
'level': log_level,
|
2018-07-21 11:16:22 +02:00
|
|
|
'filename': os.path.join(state.appdata, 'debug.log'),
|
2018-04-08 17:28:08 +02:00
|
|
|
'maxBytes': 2097152, # 2 MiB
|
2013-07-15 21:45:03 +02:00
|
|
|
'backupCount': 1,
|
2016-12-06 16:15:37 +01:00
|
|
|
'encoding': 'UTF-8',
|
2013-07-15 21:45:03 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'loggers': {
|
|
|
|
'console_only': {
|
|
|
|
'handlers': ['console'],
|
2018-04-08 17:28:08 +02:00
|
|
|
'propagate': 0
|
2013-07-15 21:45:03 +02:00
|
|
|
},
|
|
|
|
'file_only': {
|
|
|
|
'handlers': ['file'],
|
2018-04-08 17:28:08 +02:00
|
|
|
'propagate': 0
|
2013-07-15 21:45:03 +02:00
|
|
|
},
|
|
|
|
'both': {
|
|
|
|
'handlers': ['console', 'file'],
|
2018-04-08 17:28:08 +02:00
|
|
|
'propagate': 0
|
2013-07-15 21:45:03 +02:00
|
|
|
},
|
2013-06-29 18:27:40 +02:00
|
|
|
},
|
2013-07-15 21:45:03 +02:00
|
|
|
'root': {
|
2013-06-29 18:27:40 +02:00
|
|
|
'level': log_level,
|
|
|
|
'handlers': ['console'],
|
|
|
|
},
|
2013-07-15 21:45:03 +02:00
|
|
|
})
|
2015-11-18 16:22:17 +01:00
|
|
|
|
2018-07-21 11:16:22 +02:00
|
|
|
return True, fail_msg
|
2018-04-08 17:28:08 +02:00
|
|
|
|
2018-05-26 14:43:21 +02:00
|
|
|
|
2018-06-27 10:24:09 +02:00
|
|
|
def initLogging():
|
2018-07-21 11:16:22 +02:00
|
|
|
preconfigured, msg = configureLogging()
|
|
|
|
if preconfigured:
|
2018-05-26 14:43:21 +02:00
|
|
|
if '-c' in sys.argv:
|
|
|
|
logger = logging.getLogger('file_only')
|
|
|
|
else:
|
|
|
|
logger = logging.getLogger('both')
|
2015-11-18 16:22:17 +01:00
|
|
|
else:
|
2018-05-26 14:43:21 +02:00
|
|
|
logger = logging.getLogger('default')
|
2018-07-21 11:16:22 +02:00
|
|
|
|
|
|
|
if msg:
|
|
|
|
logger.log(logging.WARNING if preconfigured else logging.INFO, msg)
|
2018-06-27 10:24:09 +02:00
|
|
|
return logger
|
|
|
|
|
2013-07-15 21:45:03 +02:00
|
|
|
|
2018-07-21 11:16:22 +02:00
|
|
|
def resetLogging():
|
2013-07-15 21:45:03 +02:00
|
|
|
global logger
|
2018-07-21 11:16:22 +02:00
|
|
|
for i in logger.handlers.iterkeys():
|
2013-07-15 21:45:03 +02:00
|
|
|
logger.removeHandler(i)
|
|
|
|
i.flush()
|
|
|
|
i.close()
|
2018-06-27 10:24:09 +02:00
|
|
|
logger = initLogging()
|
|
|
|
|
2018-05-26 14:43:21 +02:00
|
|
|
|
2018-06-27 10:24:09 +02:00
|
|
|
# !
|
|
|
|
logger = initLogging()
|