Moved StructuredMessage and Logger to separate file; refactored pisa-cli logging using new format

This commit is contained in:
Salvatore Ingala
2019-10-09 10:20:39 +07:00
parent c524319027
commit dee93e5c62
14 changed files with 26 additions and 46 deletions

View File

@@ -1,6 +1,4 @@
import logging
import json
import time
from pisa.utils.auth_proxy import AuthServiceProxy
import pisa.conf as conf
@@ -8,33 +6,6 @@ import pisa.conf as conf
HOST = 'localhost'
PORT = 9814
class StructuredMessage(object):
def __init__(self, message, **kwargs):
self.message = message
self.time = time.asctime()
self.kwargs = kwargs
def __str__(self):
return json.dumps({**self.kwargs, "message": self.message, "time": self.time})
class Logger(object):
def __init__(self, actor=None):
self.actor = actor
def _add_prefix(self, msg):
return msg if self.actor is None else "[{}] {}".format(self.actor, msg)
def info(self, msg, **kwargs):
logging.info(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs))
def debug(self, msg, **kwargs):
logging.debug(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs))
def error(self, msg, **kwargs):
logging.error(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs))
# Configure logging
logging.basicConfig(format='%(message)s', level=logging.INFO, handlers=[
logging.FileHandler(conf.SERVER_LOG_FILE),