mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-18 22:34:21 +01:00
Rewrite bfxapi/utils/logger.py with new ColoredLogger.
This commit is contained in:
@@ -22,7 +22,7 @@ class Client(object):
|
||||
API_KEY: Optional[str] = None,
|
||||
API_SECRET: Optional[str] = None,
|
||||
filter: Optional[List[str]] = None,
|
||||
log_level: str = "WARNING"
|
||||
log_level: str = "INFO"
|
||||
):
|
||||
credentials = {
|
||||
"API_KEY": API_KEY,
|
||||
|
||||
@@ -1,99 +1,52 @@
|
||||
"""
|
||||
Module used to describe all of the different data types
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
|
||||
|
||||
RESET_SEQ = "\033[0m"
|
||||
|
||||
COLOR_SEQ = "\033[1;%dm"
|
||||
ITALIC_COLOR_SEQ = "\033[3;%dm"
|
||||
UNDERLINE_COLOR_SEQ = "\033[4;%dm"
|
||||
|
||||
BOLD_SEQ = "\033[1m"
|
||||
UNDERLINE_SEQ = "\033[04m"
|
||||
|
||||
YELLOW = '\033[93m'
|
||||
WHITE = '\33[37m'
|
||||
BLUE = '\033[34m'
|
||||
LIGHT_BLUE = '\033[94m'
|
||||
RED = '\033[91m'
|
||||
GREY = '\33[90m'
|
||||
|
||||
KEYWORD_COLORS = {
|
||||
'WARNING': YELLOW,
|
||||
'INFO': LIGHT_BLUE,
|
||||
'DEBUG': WHITE,
|
||||
'CRITICAL': YELLOW,
|
||||
'ERROR': RED,
|
||||
'TRADE': '\33[102m\33[30m'
|
||||
}
|
||||
|
||||
def formatter_message(message, use_color = True):
|
||||
"""
|
||||
Syntax highlight certain keywords
|
||||
"""
|
||||
if use_color:
|
||||
message = message.replace("$RESET", RESET_SEQ).replace("$BOLD", BOLD_SEQ)
|
||||
else:
|
||||
message = message.replace("$RESET", "").replace("$BOLD", "")
|
||||
return message
|
||||
|
||||
def format_word(message, word, color_seq, bold=False, underline=False):
|
||||
"""
|
||||
Surround the given word with a sequence
|
||||
"""
|
||||
replacer = color_seq + word + RESET_SEQ
|
||||
if underline:
|
||||
replacer = UNDERLINE_SEQ + replacer
|
||||
if bold:
|
||||
replacer = BOLD_SEQ + replacer
|
||||
return message.replace(word, replacer)
|
||||
COLORS = {
|
||||
"DEBUG": CYAN,
|
||||
"INFO": BLUE,
|
||||
"WARNING": YELLOW,
|
||||
"ERROR": RED
|
||||
}
|
||||
|
||||
class Formatter(logging.Formatter):
|
||||
"""
|
||||
This Formatted simply colors in the levelname i.e 'INFO', 'DEBUG'
|
||||
"""
|
||||
def __init__(self, msg, use_color = True):
|
||||
logging.Formatter.__init__(self, msg)
|
||||
self.use_color = use_color
|
||||
class _ColoredFormatter(logging.Formatter):
|
||||
def __init__(self, msg, use_color = True):
|
||||
logging.Formatter.__init__(self, msg, "%d-%m-%Y %H:%M:%S")
|
||||
self.use_color = use_color
|
||||
|
||||
def format(self, record):
|
||||
"""
|
||||
Format and highlight certain keywords
|
||||
"""
|
||||
levelname = record.levelname
|
||||
if self.use_color and levelname in KEYWORD_COLORS:
|
||||
levelname_color = KEYWORD_COLORS[levelname] + levelname + RESET_SEQ
|
||||
record.levelname = levelname_color
|
||||
record.name = GREY + record.name + RESET_SEQ
|
||||
return logging.Formatter.format(self, record)
|
||||
def format(self, record):
|
||||
levelname = record.levelname
|
||||
if self.use_color and levelname in COLORS:
|
||||
levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + levelname + RESET_SEQ
|
||||
record.levelname = levelname_color
|
||||
record.name = ITALIC_COLOR_SEQ % (30 + BLACK) + record.name + RESET_SEQ
|
||||
return logging.Formatter.format(self, record)
|
||||
|
||||
class CustomLogger(logging.Logger):
|
||||
"""
|
||||
This adds extra logging functions such as logger.trade and also
|
||||
sets the logger to use the custom formatter
|
||||
"""
|
||||
FORMAT = "[$BOLD%(name)s$RESET] [%(levelname)s] %(message)s"
|
||||
class ColoredLogger(logging.Logger):
|
||||
FORMAT = "[$BOLD%(name)s$RESET] [%(asctime)s] [%(levelname)s] %(message)s"
|
||||
|
||||
COLOR_FORMAT = formatter_message(FORMAT, True)
|
||||
TRADE = 50
|
||||
|
||||
def __init__(self, name, level):
|
||||
logging.Logger.__init__(self, name, level)
|
||||
|
||||
def __init__(self, name, logLevel='DEBUG'):
|
||||
logging.Logger.__init__(self, name, logLevel)
|
||||
color_formatter = Formatter(self.COLOR_FORMAT)
|
||||
colored_formatter = _ColoredFormatter(self.COLOR_FORMAT)
|
||||
console = logging.StreamHandler()
|
||||
console.setFormatter(color_formatter)
|
||||
self.addHandler(console)
|
||||
logging.addLevelName(self.TRADE, "TRADE")
|
||||
return
|
||||
console.setFormatter(colored_formatter)
|
||||
|
||||
def set_level(self, level):
|
||||
logging.Logger.setLevel(self, level)
|
||||
|
||||
def trade(self, message, *args, **kws):
|
||||
"""
|
||||
Print a syntax highlighted trade signal
|
||||
"""
|
||||
if self.isEnabledFor(self.TRADE):
|
||||
message = format_word(message, 'CLOSED ', YELLOW, bold=True)
|
||||
message = format_word(message, 'OPENED ', LIGHT_BLUE, bold=True)
|
||||
message = format_word(message, 'UPDATED ', BLUE, bold=True)
|
||||
message = format_word(message, 'CLOSED_ALL ', RED, bold=True)
|
||||
# Yes, logger takes its '*args' as 'args'.
|
||||
self._log(self.TRADE, message, args, **kws)
|
||||
self.addHandler(console)
|
||||
@@ -16,7 +16,7 @@ from ..exceptions import WebsocketAuthenticationRequired, InvalidAuthenticationC
|
||||
|
||||
from ...utils.JSONEncoder import JSONEncoder
|
||||
|
||||
from ...utils.logger import Formatter, CustomLogger
|
||||
from ...utils.logger import ColoredLogger
|
||||
|
||||
def _require_websocket_authentication(function: F) -> F:
|
||||
async def wrapper(self, *args, **kwargs):
|
||||
@@ -38,7 +38,7 @@ class BfxWebsocketClient(object):
|
||||
*AuthenticatedChannelsHandler.EVENTS
|
||||
]
|
||||
|
||||
def __init__(self, host, credentials = None, log_level = "WARNING"):
|
||||
def __init__(self, host, credentials = None, log_level = "INFO"):
|
||||
self.websocket = None
|
||||
|
||||
self.host, self.credentials, self.event_emitter = host, credentials, AsyncIOEventEmitter()
|
||||
@@ -47,7 +47,7 @@ class BfxWebsocketClient(object):
|
||||
|
||||
self.handler = AuthenticatedChannelsHandler(event_emitter=self.event_emitter)
|
||||
|
||||
self.logger = CustomLogger("BfxWebsocketClient", logLevel=log_level)
|
||||
self.logger = ColoredLogger("BfxWebsocketClient", level=log_level)
|
||||
|
||||
self.event_emitter.add_listener("error",
|
||||
lambda exception: self.logger.error(f"{type(exception).__name__}: {str(exception)}" + "\n" +
|
||||
@@ -85,9 +85,9 @@ class BfxWebsocketClient(object):
|
||||
|
||||
async with websockets.connect(self.host) as websocket:
|
||||
if reconnection.status == True:
|
||||
self.logger.info(f"Reconnect attempt successful (attempt N°{reconnection.attempts}): The " +
|
||||
self.logger.info(f"Reconnect attempt successful (attempt no.{reconnection.attempts}): The " +
|
||||
f"client has been offline for a total of {datetime.now() - reconnection.timestamp} " +
|
||||
f"(first reconnection attempt: {reconnection.timestamp:%d-%m-%Y at %H:%M:%S}).")
|
||||
f"(connection lost at: {reconnection.timestamp:%d-%m-%Y at %H:%M:%S}).")
|
||||
|
||||
reconnection = Reconnection(status=False, attempts=0, timestamp=None)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user