move logging-setup into __init__.py and use one central logger-object
This commit is contained in:
parent
1bc8d80d1d
commit
d836b807be
|
@ -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)
|
|
@ -1,5 +1,6 @@
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
from os.path import abspath, exists
|
from os.path import abspath, exists
|
||||||
|
|
||||||
from tsstats.config import parse_config
|
from tsstats.config import parse_config
|
||||||
|
@ -7,9 +8,10 @@ from tsstats.exceptions import ConfigNotFound
|
||||||
from tsstats.log import parse_logs
|
from tsstats.log import parse_logs
|
||||||
from tsstats.template import render_template
|
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
|
# check cmdline-args
|
||||||
config_path = abspath(config_path)
|
config_path = abspath(config_path)
|
||||||
id_map_path = abspath(id_map_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 = {}
|
id_map = {}
|
||||||
|
|
||||||
log_path, output_path = parse_config(config_path)
|
log_path, output_path = parse_config(config_path)
|
||||||
clients = parse_logs(log_path, ident_map=id_map, file_log=debugfile)
|
clients = parse_logs(log_path, ident_map=id_map)
|
||||||
render_template(clients, output=output_path, debug=debug)
|
render_template(clients, output=output_path)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -41,8 +43,7 @@ if __name__ == '__main__':
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--debug', help='debug mode', action='store_true'
|
'--debug', help='debug mode', action='store_true'
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
|
||||||
'--debugfile', help='write debug-log to file', action='store_true'
|
|
||||||
)
|
|
||||||
args = parser.parse_args()
|
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)
|
||||||
|
|
|
@ -2,6 +2,8 @@ import logging
|
||||||
|
|
||||||
from tsstats.exceptions import InvalidLog
|
from tsstats.exceptions import InvalidLog
|
||||||
|
|
||||||
|
logger = logging.getLogger('tsstats')
|
||||||
|
|
||||||
|
|
||||||
class Clients:
|
class Clients:
|
||||||
|
|
||||||
|
@ -65,7 +67,7 @@ class Client:
|
||||||
'''
|
'''
|
||||||
client connects at "timestamp"
|
client connects at "timestamp"
|
||||||
'''
|
'''
|
||||||
logging.debug('CONNECT {}'.format(str(self)))
|
logger.debug('CONNECT {}'.format(str(self)))
|
||||||
self.connected += 1
|
self.connected += 1
|
||||||
self._last_connect = timestamp
|
self._last_connect = timestamp
|
||||||
|
|
||||||
|
@ -73,9 +75,9 @@ class Client:
|
||||||
'''
|
'''
|
||||||
client disconnects at "timestamp"
|
client disconnects at "timestamp"
|
||||||
'''
|
'''
|
||||||
logging.debug('DISCONNECT {}'.format(str(self)))
|
logger.debug('DISCONNECT {}'.format(str(self)))
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
logging.debug('^ disconnect before connect')
|
logger.debug('^ disconnect before connect')
|
||||||
raise InvalidLog('disconnect before connect!')
|
raise InvalidLog('disconnect before connect!')
|
||||||
self.connected -= 1
|
self.connected -= 1
|
||||||
session_time = timestamp - self._last_connect
|
session_time = timestamp - self._last_connect
|
||||||
|
@ -86,7 +88,7 @@ class Client:
|
||||||
'''
|
'''
|
||||||
client kicks "target" (Client-obj)
|
client kicks "target" (Client-obj)
|
||||||
'''
|
'''
|
||||||
logging.debug('KICK {} -> {}'.format(str(self), str(target)))
|
logger.debug('KICK {} -> {}'.format(str(self), str(target)))
|
||||||
target.pkicks += 1
|
target.pkicks += 1
|
||||||
self.kicks += 1
|
self.kicks += 1
|
||||||
|
|
||||||
|
@ -94,7 +96,7 @@ class Client:
|
||||||
'''
|
'''
|
||||||
client bans "target" (Client-obj)
|
client bans "target" (Client-obj)
|
||||||
'''
|
'''
|
||||||
logging.debug('BAN {} -> {}'.format(str(self), str(target)))
|
logger.debug('BAN {} -> {}'.format(str(self), str(target)))
|
||||||
target.pbans += 1
|
target.pbans += 1
|
||||||
self.bans += 1
|
self.bans += 1
|
||||||
|
|
||||||
|
|
|
@ -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)
|
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
|
# find all log-files and open them TODO: move this into main
|
||||||
file_paths = sorted([file_path for file_path in glob(log_path)])
|
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:
|
for file_path in file_paths:
|
||||||
log_file = open(file_path)
|
log_file = open(file_path)
|
||||||
# process lines
|
# 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:
|
for line in log_file:
|
||||||
parts = line.split('|')
|
parts = line.split('|')
|
||||||
log_format = '%Y-%m-%d %H:%M:%S.%f'
|
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)
|
invoker.ban(client)
|
||||||
else:
|
else:
|
||||||
invoker.kick(client)
|
invoker.kick(client)
|
||||||
logging.debug('Finished parsing of {}'.format(log_file.name))
|
logger.debug('Finished parsing of {}'.format(log_file.name))
|
||||||
return clients
|
return clients
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
from os.path import abspath
|
from os.path import abspath
|
||||||
from time import localtime, strftime
|
from time import localtime, strftime
|
||||||
|
|
||||||
|
@ -5,9 +6,11 @@ from jinja2 import Environment, FileSystemLoader
|
||||||
|
|
||||||
from tsstats.utils import seconds_to_text, sort_clients
|
from tsstats.utils import seconds_to_text, sort_clients
|
||||||
|
|
||||||
|
logger = logging.getLogger('tsstats')
|
||||||
|
|
||||||
|
|
||||||
def render_template(clients, output, template_name='tsstats/template.html',
|
def render_template(clients, output, template_name='tsstats/template.html',
|
||||||
title='TeamspeakStats', debug=False):
|
title='TeamspeakStats'):
|
||||||
# prepare clients
|
# prepare clients
|
||||||
clients_onlinetime_ = sort_clients(clients.clients_by_id, 'onlinetime')
|
clients_onlinetime_ = sort_clients(clients.clients_by_id, 'onlinetime')
|
||||||
clients_onlinetime = [
|
clients_onlinetime = [
|
||||||
|
@ -32,4 +35,5 @@ def render_template(clients, output, template_name='tsstats/template.html',
|
||||||
template_env.filters['frmttime'] = fmttime
|
template_env.filters['frmttime'] = fmttime
|
||||||
template = template_env.get_template(template_name)
|
template = template_env.get_template(template_name)
|
||||||
with open(output, 'w') as f:
|
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))
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
from os import remove
|
from os import remove
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -9,6 +10,9 @@ from tsstats.log import parse_logs
|
||||||
clients = parse_logs('tsstats/tests/res/test.log')
|
clients = parse_logs('tsstats/tests/res/test.log')
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger('tsstats')
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def output(request):
|
def output(request):
|
||||||
def clean():
|
def clean():
|
||||||
|
@ -67,7 +71,9 @@ def test_client_repr():
|
||||||
|
|
||||||
|
|
||||||
def test_debug_log():
|
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')
|
open('debug.txt')
|
||||||
remove('debug.txt')
|
remove('debug.txt')
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
from os import remove
|
from os import remove
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -10,6 +11,8 @@ from tsstats.utils import seconds_to_text
|
||||||
output_path = 'tsstats/tests/res/output.html'
|
output_path = 'tsstats/tests/res/output.html'
|
||||||
clients = parse_logs('tsstats/tests/res/test.log')
|
clients = parse_logs('tsstats/tests/res/test.log')
|
||||||
|
|
||||||
|
logger = logging.getLogger('tsstats')
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def output(request):
|
def output(request):
|
||||||
|
@ -19,7 +22,9 @@ def output(request):
|
||||||
|
|
||||||
|
|
||||||
def test_debug(output):
|
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')
|
soup = BeautifulSoup(open(output_path), 'html.parser')
|
||||||
# check red label
|
# check red label
|
||||||
assert soup.find_all(class_='alert alert-danger')
|
assert soup.find_all(class_='alert alert-danger')
|
||||||
|
|
Loading…
Reference in New Issue