move logging-setup into __init__.py and use one central logger-object

This commit is contained in:
Thor77 2016-05-10 22:50:34 +02:00
parent 1bc8d80d1d
commit d836b807be
7 changed files with 54 additions and 33 deletions

View File

@ -0,0 +1,13 @@
import logging
logger = logging.getLogger('tsstats')
logger.setLevel(logging.INFO)
fh = logging.FileHandler('debug.txt', 'w')
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
logger.addHandler(fh)
logger.addHandler(ch)

View File

@ -1,5 +1,6 @@
import argparse
import json
import logging
from os.path import abspath, exists
from tsstats.config import parse_config
@ -7,9 +8,10 @@ from tsstats.exceptions import ConfigNotFound
from tsstats.log import parse_logs
from tsstats.template import render_template
logger = logging.getLogger('tsstats')
def main(config_path='config.ini', id_map_path='id_map.json',
debug=False, debugfile=False):
def main(config_path='config.ini', id_map_path='id_map.json'):
# check cmdline-args
config_path = abspath(config_path)
id_map_path = abspath(id_map_path)
@ -24,8 +26,8 @@ def main(config_path='config.ini', id_map_path='id_map.json',
id_map = {}
log_path, output_path = parse_config(config_path)
clients = parse_logs(log_path, ident_map=id_map, file_log=debugfile)
render_template(clients, output=output_path, debug=debug)
clients = parse_logs(log_path, ident_map=id_map)
render_template(clients, output=output_path)
if __name__ == '__main__':
@ -41,8 +43,7 @@ if __name__ == '__main__':
parser.add_argument(
'--debug', help='debug mode', action='store_true'
)
parser.add_argument(
'--debugfile', help='write debug-log to file', action='store_true'
)
args = parser.parse_args()
main(args.config, args.idmap, args.debug, args.debugfile)
if args.debug:
logger.setLevel(logging.DEBUG)
main(args.config, args.idmap)

View File

@ -2,6 +2,8 @@ import logging
from tsstats.exceptions import InvalidLog
logger = logging.getLogger('tsstats')
class Clients:
@ -65,7 +67,7 @@ class Client:
'''
client connects at "timestamp"
'''
logging.debug('CONNECT {}'.format(str(self)))
logger.debug('CONNECT {}'.format(str(self)))
self.connected += 1
self._last_connect = timestamp
@ -73,9 +75,9 @@ class Client:
'''
client disconnects at "timestamp"
'''
logging.debug('DISCONNECT {}'.format(str(self)))
logger.debug('DISCONNECT {}'.format(str(self)))
if not self.connected:
logging.debug('^ disconnect before connect')
logger.debug('^ disconnect before connect')
raise InvalidLog('disconnect before connect!')
self.connected -= 1
session_time = timestamp - self._last_connect
@ -86,7 +88,7 @@ class Client:
'''
client kicks "target" (Client-obj)
'''
logging.debug('KICK {} -> {}'.format(str(self), str(target)))
logger.debug('KICK {} -> {}'.format(str(self), str(target)))
target.pkicks += 1
self.kicks += 1
@ -94,7 +96,7 @@ class Client:
'''
client bans "target" (Client-obj)
'''
logging.debug('BAN {} -> {}'.format(str(self), str(target)))
logger.debug('BAN {} -> {}'.format(str(self), str(target)))
target.pbans += 1
self.bans += 1

View File

@ -11,21 +11,11 @@ re_disconnect_invoker = re.compile(
)
def parse_logs(log_path, ident_map={}, file_log=False):
logger = logging.getLogger('tsstats')
def parse_logs(log_path, ident_map={}):
clients = Clients(ident_map)
# setup logging
log = logging.getLogger()
log.setLevel(logging.DEBUG)
if file_log:
# file logger
file_handler = logging.FileHandler('debug.txt', 'w', 'UTF-8')
file_handler.setFormatter(logging.Formatter('%(message)s'))
file_handler.setLevel(logging.DEBUG)
log.addHandler(file_handler)
# stream logger (unused)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
log.addHandler(stream_handler)
# find all log-files and open them TODO: move this into main
file_paths = sorted([file_path for file_path in glob(log_path)])
@ -33,7 +23,7 @@ def parse_logs(log_path, ident_map={}, file_log=False):
for file_path in file_paths:
log_file = open(file_path)
# process lines
logging.debug('Started parsing of {}'.format(log_file.name))
logger.debug('Started parsing of {}'.format(log_file.name))
for line in log_file:
parts = line.split('|')
log_format = '%Y-%m-%d %H:%M:%S.%f'
@ -61,5 +51,5 @@ def parse_logs(log_path, ident_map={}, file_log=False):
invoker.ban(client)
else:
invoker.kick(client)
logging.debug('Finished parsing of {}'.format(log_file.name))
logger.debug('Finished parsing of {}'.format(log_file.name))
return clients

View File

@ -1,3 +1,4 @@
import logging
from os.path import abspath
from time import localtime, strftime
@ -5,9 +6,11 @@ from jinja2 import Environment, FileSystemLoader
from tsstats.utils import seconds_to_text, sort_clients
logger = logging.getLogger('tsstats')
def render_template(clients, output, template_name='tsstats/template.html',
title='TeamspeakStats', debug=False):
title='TeamspeakStats'):
# prepare clients
clients_onlinetime_ = sort_clients(clients.clients_by_id, 'onlinetime')
clients_onlinetime = [
@ -32,4 +35,5 @@ def render_template(clients, output, template_name='tsstats/template.html',
template_env.filters['frmttime'] = fmttime
template = template_env.get_template(template_name)
with open(output, 'w') as f:
f.write(template.render(title=title, objs=objs, debug=debug))
f.write(template.render(title=title, objs=objs,
debug=logger.level <= logging.DEBUG))

View File

@ -1,3 +1,4 @@
import logging
from os import remove
import pytest
@ -9,6 +10,9 @@ from tsstats.log import parse_logs
clients = parse_logs('tsstats/tests/res/test.log')
logger = logging.getLogger('tsstats')
@pytest.fixture
def output(request):
def clean():
@ -67,7 +71,9 @@ def test_client_repr():
def test_debug_log():
parse_logs('tsstats/tests/res/test.log', file_log=True)
logger.setLevel(logging.DEBUG)
parse_logs('tsstats/tests/res/test.log')
logger.setLevel(logging.INFO)
open('debug.txt')
remove('debug.txt')

View File

@ -1,3 +1,4 @@
import logging
from os import remove
import pytest
@ -10,6 +11,8 @@ from tsstats.utils import seconds_to_text
output_path = 'tsstats/tests/res/output.html'
clients = parse_logs('tsstats/tests/res/test.log')
logger = logging.getLogger('tsstats')
@pytest.fixture
def output(request):
@ -19,7 +22,9 @@ def output(request):
def test_debug(output):
render_template(clients, output_path, debug=True)
logger.setLevel(logging.DEBUG)
render_template(clients, output_path)
logger.setLevel(logging.INFO)
soup = BeautifulSoup(open(output_path), 'html.parser')
# check red label
assert soup.find_all(class_='alert alert-danger')