mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 22:24:23 +01:00
Sets a basic way of dealing with #48
Splits the logging in two loggers (console and file) so the logs can be formatted separately. A more complete console logging is required though. Right now it only should the message, but it would be nice to include also the parametters in a human-redable and friendly way. Fixing it using a single Logger will also be nice, but not worth spending too much on.
This commit is contained in:
@@ -6,8 +6,22 @@ import pisa.conf as conf
|
||||
HOST = 'localhost'
|
||||
PORT = 9814
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(format='%(message)s', level=logging.INFO, handlers=[
|
||||
logging.FileHandler(conf.SERVER_LOG_FILE),
|
||||
logging.StreamHandler()
|
||||
])
|
||||
# Create the file logger
|
||||
f_logger = logging.getLogger('pisa_file_log')
|
||||
f_logger.setLevel(logging.INFO)
|
||||
|
||||
fh = logging.FileHandler(conf.SERVER_LOG_FILE)
|
||||
fh.setLevel(logging.INFO)
|
||||
fh_formatter = logging.Formatter('%(message)s')
|
||||
fh.setFormatter(fh_formatter)
|
||||
f_logger.addHandler(fh)
|
||||
|
||||
# Create the console logger
|
||||
c_logger = logging.getLogger('pisa_console_log')
|
||||
c_logger.setLevel(logging.INFO)
|
||||
|
||||
ch = logging.StreamHandler()
|
||||
ch.setLevel(logging.INFO)
|
||||
ch_formatter = logging.Formatter('%(asctime)s %(message)s', '%Y-%m-%d %H:%M:%S')
|
||||
ch.setFormatter(ch_formatter)
|
||||
c_logger.addHandler(ch)
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import logging
|
||||
import time
|
||||
import json
|
||||
|
||||
from pisa import f_logger, c_logger
|
||||
|
||||
|
||||
class StructuredMessage(object):
|
||||
def __init__(self, message, **kwargs):
|
||||
@@ -10,7 +11,7 @@ class StructuredMessage(object):
|
||||
self.kwargs = kwargs
|
||||
|
||||
def __str__(self):
|
||||
return json.dumps({**self.kwargs, "message": self.message, "time": self.time})
|
||||
return {**self.kwargs, "message": self.message, "time": self.time}
|
||||
|
||||
|
||||
class Logger(object):
|
||||
@@ -20,14 +21,24 @@ class Logger(object):
|
||||
def _add_prefix(self, msg):
|
||||
return msg if self.actor is None else "[{}] {}".format(self.actor, msg)
|
||||
|
||||
def create_console_message(self, msg, **kwargs):
|
||||
return StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs).message
|
||||
|
||||
def create_file_message(self, msg, **kwargs):
|
||||
return json.dumps(StructuredMessage(msg, actor=self.actor, **kwargs).__str__())
|
||||
|
||||
def info(self, msg, **kwargs):
|
||||
logging.info(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs))
|
||||
f_logger.info(self.create_file_message(msg, **kwargs))
|
||||
c_logger.info(self.create_console_message(msg, **kwargs))
|
||||
|
||||
def debug(self, msg, **kwargs):
|
||||
logging.debug(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs))
|
||||
f_logger.debug(self.create_file_message(msg, **kwargs))
|
||||
c_logger.debug(self.create_console_message(msg, **kwargs))
|
||||
|
||||
def error(self, msg, **kwargs):
|
||||
logging.error(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs))
|
||||
f_logger.error(self.create_file_message(msg, **kwargs))
|
||||
c_logger.error(self.create_console_message(msg, **kwargs))
|
||||
|
||||
def warning(self, msg, **kwargs):
|
||||
logging.warning(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs))
|
||||
f_logger.warning(self.create_file_message(msg, **kwargs))
|
||||
c_logger.warning(self.create_console_message(msg, **kwargs))
|
||||
|
||||
Reference in New Issue
Block a user