-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import contextlib | ||
import logging | ||
import logging.handlers | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Generator | ||
|
||
__all__ = ("setup_logging",) | ||
|
||
|
||
@contextlib.contextmanager | ||
def setup_logging() -> "Generator[None, None, None]": | ||
log = logging.getLogger() | ||
|
||
try: | ||
max_bytes = 32 * 1024 * 1024 # 32MB | ||
log.setLevel(logging.INFO) | ||
|
||
file_handler = logging.handlers.RotatingFileHandler( | ||
filename="stock-api.log", | ||
encoding="utf-8", | ||
mode="w", | ||
maxBytes=max_bytes, | ||
backupCount=5, | ||
) | ||
stream_handler = logging.StreamHandler() | ||
|
||
dt_fmt = "%Y-%m-%d %H:%M:%S" | ||
fmt = logging.Formatter( | ||
"[{asctime}] [{levelname:<7}] {name}: {message}", dt_fmt, style="{" | ||
) | ||
|
||
file_handler.setFormatter(fmt) | ||
stream_handler.setFormatter(fmt) | ||
log.addHandler(file_handler) | ||
log.addHandler(stream_handler) | ||
|
||
yield | ||
finally: | ||
handlers = log.handlers[:] | ||
for handler in handlers: | ||
handler.close() | ||
log.removeHandler(handler) |
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