-
Notifications
You must be signed in to change notification settings - Fork 27
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 #46 from a5chin/feature/logger
Set logger
- Loading branch information
Showing
12 changed files
with
756 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Tools.""" | ||
|
||
from tools.logger import Logger | ||
|
||
__all__ = ["Logger"] |
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,11 @@ | ||
"""Tools.""" | ||
|
||
from tools.logger.googlecloud import GoogleCloudFormatter | ||
from tools.logger.local import LocalFormatter | ||
from tools.logger.logger import Logger | ||
|
||
__all__ = [ | ||
"GoogleCloudFormatter", | ||
"LocalFormatter", | ||
"Logger", | ||
] |
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,16 @@ | ||
from enum import StrEnum | ||
|
||
|
||
class LogColor(StrEnum): | ||
"""Color code for logger.""" | ||
|
||
NORMAL = "\033[0m" | ||
BLACK = "\033[30m" | ||
RED = "\033[31m" | ||
GREEN = "\033[32m" | ||
YELLOW = "\033[33m" | ||
BLUE = "\033[34m" | ||
PURPLE = "\033[35m" | ||
CYAN = "\033[36m" | ||
GREY = "\033[37m" | ||
BLOOD = "\033[41m" |
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,32 @@ | ||
import logging | ||
|
||
|
||
class GoogleCloudFormatter(logging.Formatter): | ||
"""Formatter for Google Cloud logger.""" | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
"""Style for Google Cloud logger. | ||
Args: | ||
record (logging.LogRecord): Raw log | ||
Returns: | ||
str: Log format for Google Cloud | ||
""" | ||
from pydantic import BaseModel, PositiveInt | ||
|
||
class Record(BaseModel): | ||
"""Record for Google Cloud.""" | ||
|
||
name: str | ||
line: PositiveInt | ||
func: str | ||
message: str | ||
|
||
return Record( | ||
name=record.name, | ||
line=record.lineno, | ||
func=record.funcName, | ||
message=record.getMessage(), | ||
).model_dump_json() |
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,47 @@ | ||
import logging | ||
|
||
|
||
class LocalFormatter(logging.Formatter): | ||
"""Formatter for local logger.""" | ||
|
||
def __init__(self) -> None: | ||
"""Initialize local logger formatter.""" | ||
from tools.logger.color import LogColor | ||
from tools.logger.style import LogStyle | ||
|
||
super().__init__() | ||
base = ( | ||
f"{LogColor.GREEN}%(asctime)s{LogStyle.RESET}" | ||
" | " | ||
f"{{color}}%(levelname)-8s{LogStyle.RESET}" | ||
" | " | ||
f"{LogColor.CYAN}%(name)s{LogStyle.RESET}" | ||
":" | ||
f"{LogColor.CYAN}%(funcName)s{LogStyle.RESET}" | ||
":" | ||
f"{LogColor.CYAN}%(lineno)s{LogStyle.RESET}" | ||
" - " | ||
f"{{color}}%(message)s{LogStyle.RESET}" | ||
) | ||
self.formats = { | ||
logging.DEBUG: base.format(color=LogColor.BLUE + LogStyle.BOLD), | ||
logging.INFO: base.format(color=LogColor.NORMAL + LogStyle.BOLD), | ||
logging.WARNING: base.format(color=LogColor.YELLOW + LogStyle.BOLD), | ||
logging.ERROR: base.format(color=LogColor.RED + LogStyle.BOLD), | ||
logging.CRITICAL: base.format(color=LogColor.BLOOD + LogStyle.BOLD), | ||
} | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
"""Style for local logger. | ||
Args: | ||
record (logging.LogRecord): Raw log | ||
Returns: | ||
str: Log format for local | ||
""" | ||
fmt = self.formats.get(record.levelno) | ||
formatter = logging.Formatter(fmt) | ||
|
||
return formatter.format(record) |
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,42 @@ | ||
import logging | ||
import sys | ||
|
||
from tools.logger.type import LogType | ||
|
||
|
||
class Logger(logging.Logger): | ||
"""Logger.""" | ||
|
||
def __init__(self, name: str, log_type: LogType = LogType.LOCAL) -> None: | ||
"""Initialize local logger formatter. | ||
Args: | ||
name (str): Logger name | ||
log_type (LogType, optional): Local or something. | ||
Defaults to LogType.LOCAL. | ||
""" | ||
super().__init__(name=name) | ||
|
||
if log_type == LogType.LOCAL: | ||
from tools.logger import LocalFormatter | ||
|
||
formatter = LocalFormatter() | ||
handler = logging.StreamHandler(stream=sys.stdout) | ||
|
||
handler.setFormatter(formatter) | ||
self.addHandler(handler) | ||
elif log_type == LogType.GOOGLE_CLOUD: | ||
import google.cloud.logging | ||
from google.cloud.logging_v2.handlers import StructuredLogHandler | ||
|
||
from tools.logger import GoogleCloudFormatter | ||
|
||
client = google.cloud.logging.Client() | ||
client.setup_logging() | ||
|
||
formatter = GoogleCloudFormatter() | ||
handler = StructuredLogHandler(stream=sys.stdout) | ||
|
||
handler.setFormatter(formatter) | ||
self.addHandler(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from enum import StrEnum | ||
|
||
|
||
class LogStyle(StrEnum): | ||
"""Style code logger.""" | ||
|
||
BOLD = "\033[1m" | ||
ULINE = "\033[4m" | ||
BLINK = "\033[5m" | ||
INVERT = "\033[7m" | ||
RESET = "\x1b[0m" |
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,9 @@ | ||
from enum import StrEnum, auto | ||
|
||
|
||
class LogType(StrEnum): | ||
"""Logger type.""" | ||
|
||
LOCAL = auto() | ||
GOOGLE_CLOUD = auto() | ||
AWS = auto() |
Oops, something went wrong.