Skip to content

Commit

Permalink
Merge pull request #46 from a5chin/feature/logger
Browse files Browse the repository at this point in the history
Set logger
  • Loading branch information
a5chin authored Dec 19, 2024
2 parents 515f116 + 798ec97 commit 5b9f413
Show file tree
Hide file tree
Showing 12 changed files with 756 additions and 49 deletions.
9 changes: 1 addition & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ default_stages: [pre-commit]

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.0
rev: v0.8.3
hooks:
- id: ruff
name: Ruff check
Expand All @@ -13,13 +13,6 @@ repos:
name: Ruff format
description: "Run 'ruff format' for extremely fast Python formatting"

- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.389
hooks:
- id: pyright
name: Pyright type check
description: "Type Check by Pyright"

- repo: https://github.com/hadolint/hadolint
rev: v2.12.0
hooks:
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ readme = "README.md"
license = { file = "LICENSE" }

dependencies = [
"google-cloud-logging>=3.11.3",
"pydantic>=2.10.3",
]

[tool.uv]
dev-dependencies = [
"pre-commit>=4.0.1",
"pyright>=1.1.389",
"pytest>=8.3.3",
"ruff>=0.8.0",
"pyright>=1.1.390",
"pytest>=8.3.4",
"ruff>=0.8.3",
]
5 changes: 4 additions & 1 deletion pyrightconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"pythonVersion": "3.12",
"pythonPlatform": "All",
"venv": ".venv",
"include": [],
"typeCheckingMode": "standard",
"include": [
"tools"
],
"exclude": [
"**/__pycache__",
".pytest_cache",
Expand Down
5 changes: 5 additions & 0 deletions tools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Tools."""

from tools.logger import Logger

__all__ = ["Logger"]
11 changes: 11 additions & 0 deletions tools/logger/__init__.py
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",
]
16 changes: 16 additions & 0 deletions tools/logger/color.py
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"
32 changes: 32 additions & 0 deletions tools/logger/googlecloud.py
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()
47 changes: 47 additions & 0 deletions tools/logger/local.py
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)
42 changes: 42 additions & 0 deletions tools/logger/logger.py
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)
11 changes: 11 additions & 0 deletions tools/logger/style.py
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"
9 changes: 9 additions & 0 deletions tools/logger/type.py
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()
Loading

0 comments on commit 5b9f413

Please sign in to comment.