-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from aiokitchen/featuire/rework-logging
Rework logging
- Loading branch information
Showing
8 changed files
with
228 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
import logging | ||
import sys | ||
from typing import IO, Any, Optional | ||
|
||
from aiomisc_log.enum import DateFormat | ||
|
||
|
||
try: | ||
from rich.console import Console | ||
from rich import reconfigure | ||
from rich.logging import RichHandler | ||
|
||
def rich_formatter( | ||
date_format: Optional[str] = None, stream: Optional[IO[str]] = None, | ||
rich_tracebacks: bool = False, **_: Any, | ||
**kwargs: Any, | ||
) -> logging.Handler: | ||
handler = RichHandler( | ||
console=Console(file=stream or sys.stderr), | ||
log_time_format=date_format or DateFormat.rich.value, | ||
rich_tracebacks=rich_tracebacks, | ||
kwargs.setdefault("rich_tracebacks", False) | ||
kwargs.setdefault( | ||
"log_time_format", date_format or DateFormat.rich.value, | ||
) | ||
|
||
if "console" not in kwargs: | ||
if stream is None: | ||
reconfigure(stderr=True) | ||
else: | ||
reconfigure(file=stream) | ||
|
||
handler = RichHandler(**kwargs) | ||
formatter = logging.Formatter("%(message)s") | ||
handler.setFormatter(formatter) | ||
return handler | ||
except ImportError: | ||
def rich_formatter( | ||
date_format: Optional[str] = None, stream: Optional[IO[str]] = None, | ||
rich_tracebacks: bool = False, **_: Any, | ||
**kwargs: Any, | ||
) -> logging.Handler: | ||
raise ImportError("You must install \"rich\" library for use it") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.