risotto/src/risotto/logger.py

58 lines
1.6 KiB
Python

from typing import Dict
from .context import Context
from .utils import _
from .config import DEBUG
class Logger:
""" An object to manager log
FIXME should add event to a database
"""
def _get_message_paths(self,
risotto_context: Context):
paths = risotto_context.paths
if risotto_context.type:
paths_msg = f' {risotto_context.type} '
else:
paths_msg = ' '
if len(paths) == 1:
paths_msg += f'message: {paths[0]}'
else:
paths_msg += f'sub-messages: '
paths_msg += ' > '.join(paths)
paths_msg += ':'
return paths_msg
def error_msg(self,
risotto_context: Context,
arguments,
error: str,
msg: str=''):
""" send message when an error append
"""
paths_msg = self._get_message_paths(risotto_context)
# if DEBUG:
print(_(f'{risotto_context.username}: ERROR: {error} ({paths_msg} with arguments "{arguments}": {msg})'))
def info_msg(self,
risotto_context: Context,
arguments: Dict,
msg: str=''):
""" send message with common information
"""
if risotto_context.paths:
paths_msg = self._get_message_paths(risotto_context)
else:
paths_msg = ''
tmsg = _(f'{risotto_context.username}: INFO:{paths_msg}')
if arguments:
tmsg += _(f' with arguments "{arguments}"')
if msg:
tmsg += f' {msg}'
if DEBUG:
print(tmsg)
log = Logger()