Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
GinTR1k committed Aug 12, 2020
1 parent 52a5b21 commit 6a902d9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ansq/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.14'
__version__ = '0.0.16'
13 changes: 7 additions & 6 deletions ansq/tcp/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def connect(self) -> bool:

self._writer.write(NSQCommands.MAGIC_V2)
self._status = ConnectionStatus.CONNECTED
self.logger.info('Connect to {} established'.format(self.endpoint))
self.logger.debug('Connect to {} established'.format(self.endpoint))

self._reader_task = self._loop.create_task(self._read_data_task())

Expand Down Expand Up @@ -61,7 +61,7 @@ async def reconnect(self, raise_error: bool = True) -> bool:
await self._do_close(e)
return False

self.logger.info('Reconnected to {}'.format(self.endpoint))
self.logger.debug('Reconnected to {}'.format(self.endpoint))
self._status = ConnectionStatus.CONNECTED
return True

Expand Down Expand Up @@ -117,7 +117,7 @@ async def _do_close(

if change_status:
self._status = ConnectionStatus.CLOSED
self.logger.info('Connection {} is closed'.format(self.endpoint))
self.logger.debug('Connection {} is closed'.format(self.endpoint))

async def execute(
self, command: Union[str, bytes], *args, data: Any = None,
Expand Down Expand Up @@ -160,7 +160,8 @@ async def execute(
self._cmd_waiters.append((future, callback))

command_raw = self._parser.encode_command(command, *args, data=data)
self.logger.debug('NSQ: Executing command %s' % command_raw)
if command != NSQCommands.NOP:
self.logger.debug('NSQ: Executing command %s' % command_raw)
self._writer.write(command_raw)

# track all processed and requeued messages
Expand Down Expand Up @@ -251,12 +252,12 @@ async def _parse_data(self) -> bool:
if response is None:
return False

self.logger.debug('NSQ: Got data: %s', response)

if response.is_heartbeat:
await self._pulse()
return True

self.logger.debug('NSQ: Got data: %s', response)

if response.is_message:
# track number in flight messages
self._in_flight += 1
Expand Down
8 changes: 7 additions & 1 deletion ansq/tcp/types/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@


class TCPConnection(abc.ABC):
instances_count = 0

def __init__(
self, host: str = 'localhost', port: int = 4150, *,
message_queue: asyncio.Queue = None, on_message: Callable = None,
Expand All @@ -27,10 +29,14 @@ def __init__(
deflate_level: int = 6, sample_rate: int = 0,
debug: bool = False, logger: logging.Logger = None
):
self.instance_number = self.__class__.instances_count
self.__class__.instances_count += 1

self._host, self._port = host, port
self._loop: AbstractEventLoop = loop or asyncio.get_event_loop()
self._debug = debug
self.logger = logger or get_logger(debug)
self.logger = logger or get_logger(debug, '{}:{}.{}'.format(
self._host, self._port, self.instance_number))

self._message_queue: asyncio.Queue[Optional[NSQMessage]] = \
message_queue or asyncio.Queue()
Expand Down
7 changes: 5 additions & 2 deletions ansq/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,16 @@ def _(value: datetime) -> str:
return value.isoformat()


def get_logger(debug: bool = False):
def get_logger(debug: bool = False, unique_name: str = None):
"""Get the ansq logger.
:params debug: Set up debug level.
:type debug: :class:`bool`
:params unique_name: Used to make all loggers unique.
:type unique_name: :class:`str`
"""
logger = logging.getLogger('ansq')
logger = logging.getLogger(
'ansq {}'.format(unique_name) if unique_name else 'ansq')
log_format = "%(asctime)s - %(levelname)s - %(name)s: %(message)s"
logging.basicConfig(format=log_format)
logger.setLevel(logging.DEBUG if debug else logging.INFO)
Expand Down

0 comments on commit 6a902d9

Please sign in to comment.