Skip to content

Commit

Permalink
Improve logger
Browse files Browse the repository at this point in the history
  • Loading branch information
JINWOO-J committed Nov 25, 2024
1 parent 50cff9d commit 36286be
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# temp directory for testing
last_blockheight.txt
config.ini
http_config.ini
tests/temp/*
Expand Down
4 changes: 2 additions & 2 deletions BUILD_ARGS
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

--build-arg BASE_IMAGE=python:3.9.13-slim-buster
--build-arg BUILD_DATE=2024-11-25T05:01:50UTC
--build-arg BUILD_DATE=2024-11-25T05:53:51UTC
--build-arg DOCKER_BUILD_OPTION=--no-cache --rm=true
--build-arg ECHO_OPTION=
--build-arg GIT_USER=JINWOO-J
Expand All @@ -11,4 +11,4 @@
--build-arg REPO_HUB=jinwoo
--build-arg SED_OPTION=''
--build-arg UNAME_S=Darwin
--build-arg VERSION=2.0.76
--build-arg VERSION=2.0.78
1 change: 0 additions & 1 deletion last_blockheight.txt

This file was deleted.

2 changes: 1 addition & 1 deletion pawnlib/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__title__ = 'pawnlib'
__description__ = 'pawnlib is a collection of libraries for IaC.'
__url__ = 'https://github.com/jinwoo-j/pawnlib'
__version__ = '2.0.77'
__version__ = '2.0.78'
__author__ = 'Jinwoo Jeong'
__author_email__ = 'jinwoo@parametacorp.com'
__license__ = 'MIT'
Expand Down
4 changes: 3 additions & 1 deletion pawnlib/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
ConsoleLoggerAdapter,
setup_logger,
getPawnLogger,
setup_app_logger
setup_app_logger,
add_logger,
LoggerMixin,
)
20 changes: 20 additions & 0 deletions pawnlib/config/logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,3 +1022,23 @@ def getPawnLogger(name=None, verbose=0):
new_logger = ConsoleLoggerAdapter(logger_name=name, verbose=verbose)
ConsoleLoggerAdapter._global_registry[name] = new_logger
return new_logger


def add_logger(cls):
"""
Decorator to add a logger attribute to a class.
"""
class Wrapped(cls):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.logger = logging.getLogger(f"{self.__module__}.{self.__class__.__name__}")
return Wrapped


class LoggerMixin:
def get_logger(self):
"""
Returns a logger instance with a name in the format 'module_name.ClassName'.
"""
return logging.getLogger(f"{self.__module__}.{self.__class__.__name__}")

9 changes: 5 additions & 4 deletions pawnlib/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pawnlib.exceptions.notifier import notify_exception

from pawnlib.config.globalconfig import pawnlib_config as pawn, global_verbose, pconf, SimpleNamespace, Null
from pawnlib.config.logging_config import ConsoleLoggerAdapter, setup_logger
from pawnlib.config.logging_config import ConsoleLoggerAdapter, setup_logger, LoggerMixin
from pawnlib.output import (
NoTraceBackException,
dump, syntax_highlight, kvPrint, debug_logging,
Expand Down Expand Up @@ -3030,7 +3030,7 @@ def get_tx_result(self, tx_hash, max_attempts=None):
return resp


class AsyncCallWebsocket:
class AsyncCallWebsocket(LoggerMixin):
def __init__(
self,
url: str,
Expand Down Expand Up @@ -3072,7 +3072,8 @@ def __init__(
self.on_last_status = ""

# self.logger = ConsoleLoggerAdapter(logger, "AsyncCallWebsocket", verbose > 0)
self.logger = setup_logger(logger, "pawnlib.http.AsyncCallWebsocket", verbose)
# self.logger = setup_logger(logger, "pawnlib.http.AsyncCallWebsocket", verbose)
self.logger = logger or self.get_logger()

if ssl_options is None:
self.ssl_options = ssl.create_default_context()
Expand Down Expand Up @@ -3357,7 +3358,7 @@ def __init__(
self.check_tx_result_enabled = check_tx_result_enabled

# self.logger = setup_logger(logger, "pawnlib.http.AsyncGoloopWebsocket", verbose)
self.logger = logger or logging.getLogger(__name__)
self.logger = logger or self.get_logger()

self.logger.info("Start AsyncGoloopWebsocket")

Expand Down

0 comments on commit 36286be

Please sign in to comment.