Commit b8306064 authored by Justin Hornosty's avatar Justin Hornosty

Use dict config for logging.

- Allow user to include a logging_config.py in the gajim user dir
- Support --logging-config to allow user to pass a custom logging config
- provide a default logging configuration: 'logging_config.py'
parent 55eef518
Pipeline #2555 passed with stages
in 5 minutes and 14 seconds
......@@ -140,6 +140,12 @@ class GajimApplication(Gtk.Application):
GLib.OptionArg.NONE,
_('Start a new chat'))
self.add_main_option(
'logging-config', 0, GLib.OptionFlags.NONE,
GLib.OptionArg.STRING,
_('Set a custom logging_config.py file'),
'FILE')
self.add_main_option(
'simulate-network-lost',
0,
......@@ -329,7 +335,10 @@ class GajimApplication(Gtk.Application):
logging_helpers.init()
logging_helpers.set_verbose()
else:
logging_helpers.init()
if options.contains('logging-config'):
logging_helpers.init(options.lookup_value('logging-config').get_string())
else:
logging_helpers.init()
if options.contains('quiet'):
logging_helpers.set_quiet()
......
......@@ -204,6 +204,7 @@ class ConfigPaths:
('PLUGINS_CONFIG_DIR',
'pluginsconfig', PathLocation.CONFIG, PathType.FOLDER),
('MY_CERT', 'localcerts', PathLocation.CONFIG, PathType.FOLDER),
('LOG_DIR', 'logs', PathLocation.DATA, PathType.FOLDER),
]
for path in unique_profile_paths:
......
import gajim.common.logging_helpers
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard' : {
'format' : '%(asctime)s %(levelname)-2s %(name)-32s %(message)s'
},
'error': {
'format' : '%(asctime)s %(levelname)-2s %(name)-32s %(message)s'
},
'fancyformatter' : {
'fmt' : '%(asctime)s %(levelname)-2s %(name)-32s %(message)s',
'datefmt' : '%x %H:%M:%S',
'use_color' : True,
'()': gajim.common.logging_helpers.FancyFormatter
}
},
'handlers' : {
'console' : {
'class' : 'logging.StreamHandler',
'level': 'DEBUG',
'formatter': 'fancyformatter'
},
'info_file_handler' : {
'class' : 'logging.handlers.RotatingFileHandler',
'level' : 'INFO',
'formatter' : 'standard',
'filename' : gajim.common.configpaths.get('LOG_DIR')+'/info.log',
'maxBytes' : 10485760, # 10MB
'backupCount' : 20,
'encoding' : 'utf8'
},
'error_file_handler' : {
'class' : 'logging.handlers.RotatingFileHandler',
'level' : 'ERROR',
'formatter' : 'error',
'filename' : gajim.common.configpaths.get('LOG_DIR')+'/errors.log',
'maxBytes' : 10485760, # 10MB
'backupCount' : 20,
'encoding' : 'utf8'
},
'debug_file_handler' : {
'class' : 'logging.handlers.RotatingFileHandler',
'level' : 'DEBUG',
'formatter' : 'standard',
'filename' : gajim.common.configpaths.get('LOG_DIR')+'/debug.log',
'maxBytes' : 10485760, # 10MB
'backupCount' : 20,
'encoding' : 'utf8'
},
'critical_file_handler' : {
'class' : 'logging.handlers.RotatingFileHandler',
'level' : 'CRITICAL',
'formatter' : 'standard',
'filename' : gajim.common.configpaths.get('LOG_DIR')+'/critical.log',
'maxBytes' : 10485760, # 10MB
'backupCount' : 20,
'encoding' : 'utf8'
},
'warn_file_handler' : {
'class' : 'logging.handlers.RotatingFileHandler',
'level' : 'WARN',
'formatter' : 'standard',
'filename' : gajim.common.configpaths.get('LOG_DIR')+'/warn.log',
'maxBytes' : 10485760, # 10MB
'backupCount' : 20,
'encoding' : 'utf8'
}
},
'loggers' : {
'gajim' : {
'level' : 'WARNING',
'propagate' : False,
'handlers' : [
'console',
'info_file_handler',
'error_file_handler',
'critical_file_handler',
'debug_file_handler',
'warn_file_handler'
]
},
'nbxmpp' : {
'level' : 'WARNING',
'propagate' : False,
'handlers' : [
'console',
'info_file_handler',
'error_file_handler',
'critical_file_handler',
'debug_file_handler',
'warn_file_handler'
]
}
}
}
......@@ -14,9 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
import logging
import os
import sys
import logging.config
from gajim.common.i18n import _
......@@ -130,35 +128,50 @@ class FancyFormatter(logging.Formatter):
return logging.Formatter.format(self, record)
def load_default_dict_logging():
import gajim.common.logging_config
return gajim.common.logging_config.LOGGING
def init():
def load_logging_dict_from_file(logging_config_file):
try:
import importlib.util
spec = importlib.util.spec_from_file_location("logging_config", logging_config_file)
logging_config = importlib.util.module_from_spec(spec)
spec.loader.exec_module(logging_config)
return logging_config.LOGGING
except Exception as e:
logging.error("%s - using default logging" % e)
return load_default_dict_logging()
def init(custom_logging_config_file=None):
"""
Iinitialize the logging system
Initialize the logging system:
priority of what logging config is used follows
1) --logging-config=somefile.py
2) config-path/logging_config.py
3) default logging configuration
any errors in the config files will default back to the gajim default
"""
use_color = False
if os.name != 'nt':
use_color = sys.stderr.isatty()
consoleloghandler = logging.StreamHandler()
consoleloghandler.setFormatter(
FancyFormatter(
'%(asctime)s %(levelname)s %(name)s %(message)s',
'%x %H:%M:%S',
use_color
)
)
# fake the root logger so we have 'gajim' root name instead of 'root'
root_log = logging.getLogger('gajim')
root_log.setLevel(logging.WARNING)
root_log.addHandler(consoleloghandler)
root_log.propagate = False
# handle nbxmpp logs too
root_log = logging.getLogger('nbxmpp')
root_log.setLevel(logging.WARNING)
root_log.addHandler(consoleloghandler)
root_log.propagate = False
from gajim.common import configpaths
from pathlib import Path
log_dict_config = None
from gajim.common.configpaths import create_paths
create_paths()
user_logging_config_file = Path(configpaths.get('MY_DATA')+"/logging_config.py")
if custom_logging_config_file and Path(custom_logging_config_file).is_file(): # --logging-config=...
log_dict_config = load_logging_dict_from_file(custom_logging_config_file)
elif user_logging_config_file.is_file(): # config-path/logging_config.py
log_dict_config = load_logging_dict_from_file(user_logging_config_file)
else: # use default
log_dict_config = load_default_dict_logging()
logging.config.dictConfig(log_dict_config)
def set_loglevels(loglevels_string):
parseAndSetLogLevels(loglevels_string)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment