Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new formatter #195

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions navconfig/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
"""
logstash_logging = config.getboolean(
'logstash_enabled', section='logging', fallback=False)
logging_echo = config.getboolean(
'logging_echo', section='logging', fallback=False)

logging_disable_other = config.getboolean(
'logging_disable_other', section='logging', fallback=False
)
Expand All @@ -40,7 +39,8 @@
logging_enable_mailer = config.getboolean(
'logging_enable_mailer', section='logging', fallback=False
)

logging_echo = config.getboolean(
'logging_echo', section='logging', fallback=False)
## can disable the rotating file handler
logging_enable_filehandler = config.getboolean(
'logging_enable_filehandler', section='logging', fallback=False
Expand Down Expand Up @@ -202,4 +202,15 @@
logger = Logger(name=__name__, config=logging_config)

# alias for debug printing
dprint = logger.debug
class dprint:

instance: object = None

def __new__(cls, *args, **kwargs):
if not cls.instance:
cls.instance = Logger(name='DEBUG', config=logging_config)
cls.instance.addConsole()
cls.instance.debug(*args, **kwargs)

def __call__(self, *args, **kwargs):
self.instance.debug(*args, **kwargs)
6 changes: 6 additions & 0 deletions navconfig/logging/logger.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Any


class Logger:
Expand All @@ -7,3 +8,8 @@ class Logger:
def info(self, message, *args, serialize = True, **kwargs) -> None: ...
def debug(self, message, *args, serialize = True, **kwargs) -> None: ...
def warning(self, message, *args, **kwargs) -> None: ...
def setLevel(self, level) -> None: ...
def addHandler(self, handler) -> None: ...
def error(self, message, *args, serialize = False, **kwargs) -> None: ...
def critical(self, message, *args, serialize = False, stacktrace = False, **kwargs) -> None: ...
def logger(self) -> Any: ...
23 changes: 21 additions & 2 deletions navconfig/logging/logger.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# cython: language_level=3, embedsignature=True, boundscheck=False, wraparound=True, initializedcheck=False
# Copyright (C) 2018-present Jesus Lara
#
import sys
import asyncio
from typing import Optional, Union
import logging
Expand All @@ -19,7 +20,7 @@ else:


cdef class Logger(object):
__slots__ = ['name', '_debug', '_logger']
__slots__ = ['name', '_debug', '_logger', 'logger']
cdef bool_t _debug
cdef str name
cdef object _logger
Expand All @@ -37,15 +38,33 @@ cdef class Logger(object):
self._logger = logging.getLogger(self.name)
self._logger.setLevel(LOGLEVEL)

def addConsole(self) -> None:
# create a stream handler with sys.stdout as the stream
ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(logging.DEBUG)
ch.setFormatter(ColoredFormatter())
self._logger.addHandler(ch)
## also, changing the logLevel of root logger
self._logger.setLevel(logging.DEBUG)

@property
def logger(self):
return self._logger

def disable(self, name: str, loglevel = logging.CRITICAL):
## disable logger
aio = logging.getLogger(name)
aio.setLevel(loglevel)

def setLevel(self, level) -> None:
self._logger.setLevel(level)

def addHandler(self, handler) -> None:
self._logger.addHandler(handler)

def setName(self, name: str):
## change the logger:
logging.Logger.manager.loggerDict[self.name].name = name
# self._logger = logging.getLogger(name)
self.name = name

def info(self, message, *args, serialize = False, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion navconfig/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__title__ = 'navconfig'
__description__ = ('Configuration tool for all Navigator Services '
'Tool for accessing Config info from different sources.')
__version__ = '1.1.1'
__version__ = '1.1.2'
__author__ = 'Jesus Lara'
__author_email__ = 'jesuslarag@gmail.com'
__license__ = 'MIT'