risotto/src/risotto/logger.py

58 lines
1.6 KiB
Python
Raw Normal View History

2019-11-28 16:51:56 +01:00
from typing import Dict
from .context import Context
2019-11-28 14:50:53 +01:00
from .utils import _
2019-12-07 16:21:20 +01:00
from .config import DEBUG
2019-11-28 14:50:53 +01:00
class Logger:
2019-11-28 16:51:56 +01:00
""" An object to manager log
FIXME should add event to a database
"""
def _get_message_paths(self,
2019-12-02 10:29:40 +01:00
risotto_context: Context):
2019-11-28 14:50:53 +01:00
paths = risotto_context.paths
2019-12-02 10:29:40 +01:00
if risotto_context.type:
paths_msg = f' {risotto_context.type} '
else:
paths_msg = ' '
2019-11-28 14:50:53 +01:00
if len(paths) == 1:
2019-12-02 10:29:40 +01:00
paths_msg += f'message: {paths[0]}'
2019-11-28 14:50:53 +01:00
else:
2019-12-02 10:29:40 +01:00
paths_msg += f'sub-messages: '
2019-11-28 14:50:53 +01:00
paths_msg += ' > '.join(paths)
2019-12-02 10:29:40 +01:00
paths_msg += ':'
2019-11-28 14:50:53 +01:00
return paths_msg
2019-11-28 16:51:56 +01:00
def error_msg(self,
risotto_context: Context,
arguments,
error: str,
msg: str=''):
""" send message when an error append
"""
2019-12-02 10:29:40 +01:00
paths_msg = self._get_message_paths(risotto_context)
2019-12-07 16:21:20 +01:00
# if DEBUG:
2019-12-02 10:29:40 +01:00
print(_(f'{risotto_context.username}: ERROR: {error} ({paths_msg} with arguments "{arguments}": {msg})'))
2019-11-28 14:50:53 +01:00
2019-11-28 16:51:56 +01:00
def info_msg(self,
risotto_context: Context,
arguments: Dict,
msg: str=''):
""" send message with common information
"""
2019-11-29 09:13:16 +01:00
if risotto_context.paths:
2019-12-02 10:29:40 +01:00
paths_msg = self._get_message_paths(risotto_context)
2019-11-29 09:13:16 +01:00
else:
paths_msg = ''
tmsg = _(f'{risotto_context.username}: INFO:{paths_msg}')
if arguments:
tmsg += _(f' with arguments "{arguments}"')
if msg:
tmsg += f' {msg}'
2019-12-07 16:21:20 +01:00
if DEBUG:
print(tmsg)
2019-11-28 14:50:53 +01:00
log = Logger()