Skip to content

Commit

Permalink
last major release
Browse files Browse the repository at this point in the history
- preparing to migrate library to support wider `kv` databases
  • Loading branch information
trisongz committed Jan 18, 2024
1 parent 75f2cdd commit 9ffc548
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
101 changes: 52 additions & 49 deletions aiokeydb/utils/logs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from lazyops.utils.logs import Logger, get_logger, STATUS_COLOR, COLORED_MESSAGE_MAP, FALLBACK_STATUS_COLOR
from lazyops.utils.logs import Logger, get_logger

# , STATUS_COLOR, COLORED_MESSAGE_MAP, FALLBACK_STATUS_COLOR


class ColorMap:
Expand All @@ -15,63 +17,64 @@ class ColorMap:



class CustomizeLogger:

@classmethod
def worker_logger_formatter(cls, record: dict) -> str:
"""
Formats the log message for the worker.
"""
extra = ''
if record['extra'].get('job_id') and record['extra'].get('queue_name') and record['extra'].get('kind'):
status = record['extra'].get('status')
color = STATUS_COLOR.get(status, FALLBACK_STATUS_COLOR)
kind_color = STATUS_COLOR.get(record.get('extra', {}).get('kind'), FALLBACK_STATUS_COLOR)
if not record['extra'].get('worker_name'):
record['extra']['worker_name'] = ''
extra = f'<b><{kind_color}>' + '{extra[kind]}</></>:'
extra += '<b><magenta>{extra[worker_name]}</></>:<cyan>{extra[queue_name]}</>:'
extra += '<light-blue>{extra[job_id]}</>'
if status:
extra += f':<b><{color}>' + '{extra[status]}</></>'
extra += ': '

elif record['extra'].get('kind') and record['extra'].get('queue_name'):
if not record['extra'].get('worker_name'):
record['extra']['worker_name'] = ''
kind_color = STATUS_COLOR.get(record.get('extra', {}).get('kind'), FALLBACK_STATUS_COLOR)
extra = f'<b><{kind_color}>' + '{extra[kind]}</></>:'
extra += '<b><magenta>{extra[worker_name]}</></>:<cyan>{extra[queue_name]:<18}</> '
# class CustomizeLogger:

# @classmethod
# def worker_logger_formatter(cls, record: dict) -> str:
# """
# Formats the log message for the worker.
# """
# extra = ''
# if record['extra'].get('job_id') and record['extra'].get('queue_name') and record['extra'].get('kind'):
# status = record['extra'].get('status')
# color = STATUS_COLOR.get(status, FALLBACK_STATUS_COLOR)
# kind_color = STATUS_COLOR.get(record.get('extra', {}).get('kind'), FALLBACK_STATUS_COLOR)
# if not record['extra'].get('worker_name'):
# record['extra']['worker_name'] = ''
# extra = f'<b><{kind_color}>' + '{extra[kind]}</></>:'
# extra += '<b><magenta>{extra[worker_name]}</></>:<cyan>{extra[queue_name]}</>:'
# extra += '<light-blue>{extra[job_id]}</>'
# if status:
# extra += f':<b><{color}>' + '{extra[status]}</></>'
# extra += ': '

# elif record['extra'].get('kind') and record['extra'].get('queue_name'):
# if not record['extra'].get('worker_name'):
# record['extra']['worker_name'] = ''
# kind_color = STATUS_COLOR.get(record.get('extra', {}).get('kind'), FALLBACK_STATUS_COLOR)
# extra = f'<b><{kind_color}>' + '{extra[kind]}</></>:'
# extra += '<b><magenta>{extra[worker_name]}</></>:<cyan>{extra[queue_name]:<18}</> '

return extra
# return extra


@classmethod
def logger_formatter(cls, record: dict) -> str:
"""
To add a custom format for a module, add another `elif` clause with code to determine `extra` and `level`.
# @classmethod
# def logger_formatter(cls, record: dict) -> str:
# """
# To add a custom format for a module, add another `elif` clause with code to determine `extra` and `level`.

From that module and all submodules, call logger with `logger.bind(foo='bar').info(msg)`.
Then you can access it with `record['extra'].get('foo')`.
"""
extra = '<cyan>{name}</>:<cyan>{function}</>: '
# From that module and all submodules, call logger with `logger.bind(foo='bar').info(msg)`.
# Then you can access it with `record['extra'].get('foo')`.
# """
# extra = '<cyan>{name}</>:<cyan>{function}</>: '

if record.get('extra'):
if record['extra'].get('request_id'):
extra = '<cyan>{name}</>:<cyan>{function}</>:<green>request_id: {extra[request_id]}</> '
# if record.get('extra'):
# if record['extra'].get('request_id'):
# extra = '<cyan>{name}</>:<cyan>{function}</>:<green>request_id: {extra[request_id]}</> '

elif (record['extra'].get('queue_name') or record['extra'].get('worker_name')) and record['extra'].get('kind'):
extra = cls.worker_logger_formatter(record)
# elif (record['extra'].get('queue_name') or record['extra'].get('worker_name')) and record['extra'].get('kind'):
# extra = cls.worker_logger_formatter(record)

if 'result=tensor([' not in str(record['message']):
return "<level>{level: <8}</> <green>{time:YYYY-MM-DD HH:mm:ss.SSS}</>: "\
+ extra + "<level>{message}</level>\n"
# if 'result=tensor([' not in str(record['message']):
# return "<level>{level: <8}</> <green>{time:YYYY-MM-DD HH:mm:ss.SSS}</>: "\
# + extra + "<level>{message}</level>\n"

msg = str(record['message'])[:100].replace('{', '(').replace('}', ')')
return "<level>{level: <8}</> <green>{time:YYYY-MM-DD HH:mm:ss.SSS}</>: "\
+ extra + "<level>" + msg + f"</level>{STATUS_COLOR['reset']}\n"
# msg = str(record['message'])[:100].replace('{', '(').replace('}', ')')
# return "<level>{level: <8}</> <green>{time:YYYY-MM-DD HH:mm:ss.SSS}</>: "\
# + extra + "<level>" + msg + f"</level>{STATUS_COLOR['reset']}\n"


logger: Logger = get_logger(
format = CustomizeLogger.logger_formatter,
__name__,
# format = CustomizeLogger.logger_formatter,
)
2 changes: 1 addition & 1 deletion aiokeydb/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.2.0rc9'
VERSION = '0.2.1'

0 comments on commit 9ffc548

Please sign in to comment.