Skip to content

Commit

Permalink
Merge 4c7fe85 into 0bf7c13
Browse files Browse the repository at this point in the history
  • Loading branch information
alvassin committed Apr 11, 2023
2 parents 0bf7c13 + 4c7fe85 commit f97f067
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
8 changes: 8 additions & 0 deletions aiomisc/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Entrypoint:
DEFAULT_LOG_FORMAT: str = os.getenv(
"AIOMISC_LOG_FORMAT", LogFormat.default(),
)
DEFAULT_LOG_DATE_FORMAT: Optional[str] = os.getenv(
"AIOMISC_LOG_DATE_FORMAT"
)

DEFAULT_AIOMISC_DEBUG: bool = _get_env_bool("AIOMISC_DEBUG", "0")
DEFAULT_AIOMISC_LOG_CONFIG: bool = _get_env_bool(
Expand Down Expand Up @@ -80,6 +83,7 @@ async def _start(self) -> None:
basic_config(
level=self.log_level,
log_format=self.log_format,
date_format=self.log_date_format,
buffered=self.log_buffering,
loop=self.loop,
buffer_size=self.log_buffer_size,
Expand Down Expand Up @@ -108,6 +112,7 @@ def __init__(
log_format: Union[str, LogFormat] = DEFAULT_LOG_FORMAT,
log_buffering: bool = DEFAULT_AIOMISC_BUFFERING,
log_buffer_size: int = DEFAULT_AIOMISC_BUFFER_SIZE,
log_date_format: Optional[str] = DEFAULT_LOG_DATE_FORMAT,
log_flush_interval: float = DEFAULT_AIOMISC_LOG_FLUSH,
log_config: bool = DEFAULT_AIOMISC_LOG_CONFIG,
policy: asyncio.AbstractEventLoopPolicy = event_loop_policy,
Expand Down Expand Up @@ -138,6 +143,7 @@ def __init__(
self.log_buffer_size = log_buffer_size
self.log_buffering = log_buffering
self.log_config = log_config
self.log_date_format = log_date_format
self.log_flush_interval = log_flush_interval
self.log_format = log_format
self.log_level = log_level
Expand All @@ -155,6 +161,7 @@ def __init__(
aiomisc_log.basic_config(
level=self.log_level,
log_format=self.log_format,
date_format=log_date_format
)

if self._loop is not None:
Expand Down Expand Up @@ -206,6 +213,7 @@ def __exit__(
basic_config(
level=self.log_level,
log_format=self.log_format,
date_format=self.log_date_format,
buffered=False,
)

Expand Down
16 changes: 10 additions & 6 deletions docs/source/entrypoint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Module support configuration from environment variables:

* `AIOMISC_LOG_LEVEL` - default logging level
* `AIOMISC_LOG_FORMAT` - default log format
* `AIOMISC_LOG_DATE_FORMAT` - default logging date format
* `AIOMISC_LOG_CONFIG` - should logging be configured
* `AIOMISC_LOG_FLUSH` - interval between logs flushing from buffer
* `AIOMISC_LOG_BUFFERING` - should logging be buffered
Expand Down Expand Up @@ -154,8 +155,8 @@ but handle ``Service``'s and other ``entrypoint``'s kwargs.
Logging configuration
=====================

``entrypoint`` accepts a specific set of formats in which logs will be
written to stderr.
``entrypoint`` accepts ``log_format`` argument with a specific set of formats,
in which logs will be written to stderr:

* ``stream`` - Python's default logging handler
* ``color`` - logging with `colorlog` module
Expand All @@ -164,13 +165,16 @@ written to stderr.
* ``plain`` - just log messages, without date or level info
* ``journald`` - available only when `logging-journald` module
has been installed.
* ``rich``/``rich_tb` - available only when `rich` module has been installed.
* ``rich``/``rich_tb`` - available only when `rich` module has been installed.
``rich_tb`` it's the same as ``rich`` but with fully expanded tracebacks.

Additionally, you can specify log level using ``log_level`` argument and date
format using ``log_date_format`` parameters.

An ``entrypoint`` will call ``aiomisc.log.basic_config`` function implicitly
using passed ``log_level=`` and ``log_format=`` parameters.
Alternatively you can call ``aiomisc.log.basic_config`` function manually
passing it already created eventloop.
using passed ``log_*`` parameters. Alternatively you can call
``aiomisc.log.basic_config`` function manually passing it already created
eventloop.

However, you can configure logging earlier using ``aiomisc_log.basic_config``,
but you will lose log buffering and flushing in a separate thread.
Expand Down
40 changes: 39 additions & 1 deletion tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
from contextlib import ExitStack, suppress
from tempfile import mktemp
from typing import Any, Optional, Set, Tuple
from unittest import mock

import aiohttp.web
import fastapi
import pytest

import aiomisc
from aiomisc.entrypoint import Entrypoint
from aiomisc.entrypoint import Entrypoint, entrypoint
from aiomisc.service import TCPServer, TLSServer, UDPServer
from aiomisc.service.aiohttp import AIOHTTPService
from aiomisc.service.asgi import ASGIApplicationType, ASGIHTTPService
from aiomisc.service.tcp import RobustTCPClient, TCPClient
from aiomisc.service.tls import RobustTLSClient, TLSClient
from aiomisc_log import LogFormat
from aiomisc_log.enum import DateFormat, LogLevel
from tests import unix_only


Expand Down Expand Up @@ -884,3 +887,38 @@ async def test_add_remove_service(entrypoint: aiomisc.Entrypoint):
await entrypoint.start_services(service)

assert len(StorageService.INSTANCES) == 10


@pytest.mark.parametrize('entrypoint_logging_kwargs,basic_config_kwargs', [
(
{},
{
'level': LogLevel.info.name,
'log_format': LogFormat.plain.name,
'date_format': None
}
),
(
{
'log_level': LogLevel.debug,
'log_format': LogFormat.stream,
'log_date_format': DateFormat.stream
},
{
'level': LogLevel.debug,
'log_format': LogFormat.stream,
'date_format': DateFormat.stream
}
)
])
def test_entrypoint_log_params(entrypoint_logging_kwargs, basic_config_kwargs):
with mock.patch("aiomisc_log.basic_config") as basic_config_mock:
with entrypoint(**entrypoint_logging_kwargs):
pass

# unbuffered logging is configured on init, buffered on start,
# unbuffered on stop.
assert basic_config_mock.call_count == 3
for call_args, call_kwargs in basic_config_mock.call_args_list:
for key, value in basic_config_kwargs.items():
assert call_kwargs[key] == value

0 comments on commit f97f067

Please sign in to comment.