2015-12-07 02:40:13 +01:00
|
|
|
"""
|
|
|
|
log.py - PyLink logging module.
|
|
|
|
|
|
|
|
This module contains the logging portion of the PyLink framework. Plugins can
|
|
|
|
access the global logger object by importing "log" from this module
|
|
|
|
(from log import log).
|
|
|
|
"""
|
|
|
|
|
2015-07-05 22:22:17 +02:00
|
|
|
import logging
|
2015-07-07 21:14:55 +02:00
|
|
|
import sys
|
2015-07-19 05:11:29 +02:00
|
|
|
import os
|
|
|
|
|
2015-08-04 04:27:19 +02:00
|
|
|
from conf import conf, confname
|
2015-07-07 21:14:55 +02:00
|
|
|
|
2015-07-19 05:11:29 +02:00
|
|
|
level = conf['bot'].get('loglevel') or 'DEBUG'
|
2015-07-07 21:14:55 +02:00
|
|
|
try:
|
2015-07-19 05:11:29 +02:00
|
|
|
level = getattr(logging, level.upper())
|
2015-07-07 21:14:55 +02:00
|
|
|
except AttributeError:
|
|
|
|
print('ERROR: Invalid log level %r specified in config.' % level)
|
|
|
|
sys.exit(3)
|
|
|
|
|
2015-07-19 05:11:29 +02:00
|
|
|
curdir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
logdir = os.path.join(curdir, 'log')
|
|
|
|
# Make sure our log/ directory exists
|
|
|
|
os.makedirs(logdir, exist_ok=True)
|
|
|
|
|
|
|
|
_format = '%(asctime)s [%(levelname)s] %(message)s'
|
|
|
|
logging.basicConfig(level=level, format=_format)
|
|
|
|
|
|
|
|
# Set log file to $CURDIR/log/pylink
|
|
|
|
logformat = logging.Formatter(_format)
|
2015-08-04 04:27:19 +02:00
|
|
|
logfile = logging.FileHandler(os.path.join(logdir, '%s.log' % confname), mode='w')
|
2015-07-19 05:11:29 +02:00
|
|
|
logfile.setFormatter(logformat)
|
2015-07-05 22:22:17 +02:00
|
|
|
|
|
|
|
global log
|
|
|
|
log = logging.getLogger()
|
2015-07-19 05:11:29 +02:00
|
|
|
log.addHandler(logfile)
|