generated from brenozd/python-template
feat: adding proper logger
This commit is contained in:
@@ -20,13 +20,21 @@ class CodeNodeType(Enum):
|
||||
return self.name.title()
|
||||
|
||||
|
||||
@dataclass
|
||||
class TextPoint:
|
||||
row: int
|
||||
column: int
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"({self.row}, {self.column})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class CodeNode:
|
||||
language: CodeNodeLang
|
||||
type: CodeNodeType
|
||||
text: str
|
||||
# Where to put the documentation
|
||||
documentation_after_line: int | None = None
|
||||
text_point: TextPoint | None = None # Where the text is in the file
|
||||
documentation: str | None = None
|
||||
|
||||
def from_lang(self, lang: CodeNodeLang) -> "CodeNode":
|
||||
@@ -38,9 +46,9 @@ class CodeNode:
|
||||
"language": self.language,
|
||||
"type": self.type,
|
||||
"text": self.text,
|
||||
"documentation_after_line": self.documentation_after_line,
|
||||
"text_point": self.text_point,
|
||||
"documentation": self.documentation,
|
||||
}
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"CodeNode(language='{str(self.language)}', type='{str(self.type)}', text={repr(self.text)}, documentation_after_line={self.documentation_after_line}, documentation={repr(self.documentation)})"
|
||||
return f"CodeNode(language='{str(self.language)}', type='{str(self.type)}', text={repr(self.text)}, text_point={self.text_point}, documentation={repr(self.documentation)})"
|
||||
|
||||
40
src/docai/logger.py
Normal file
40
src/docai/logger.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import logging
|
||||
from logging import LogRecord
|
||||
from rich.logging import RichHandler
|
||||
from datetime import datetime, timezone
|
||||
import langchain
|
||||
|
||||
|
||||
class IsoFormatter(logging.Formatter):
|
||||
"""
|
||||
Custom log formatter producing ISO-8601 timestamps with timezone info.
|
||||
Example:
|
||||
2025-01-01T12:34:56.789+00:00 [INFO] logger_name: message
|
||||
"""
|
||||
|
||||
def formatTime(self, record: LogRecord, datefmt: str | None = None) -> str:
|
||||
dt = datetime.fromtimestamp(record.created, tz=timezone.utc)
|
||||
return dt.isoformat(timespec="milliseconds")
|
||||
|
||||
def format(self, record: LogRecord) -> str:
|
||||
record.timestamp = self.formatTime(record)
|
||||
record.level = record.levelname
|
||||
record.logger = record.name
|
||||
return super().format(record)
|
||||
|
||||
|
||||
FORMAT = "{timestamp} [{level}] {logger}: {message}"
|
||||
|
||||
handler = RichHandler(markup=True, rich_tracebacks=True, show_path=True, tracebacks_suppress=[langchain])
|
||||
handler.setFormatter(IsoFormatter(fmt=FORMAT, style="{"))
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.NOTSET,
|
||||
handlers=[handler],
|
||||
)
|
||||
|
||||
# Silence OpenAI + HTTPX + HTTPCore
|
||||
logging.getLogger("openai").setLevel(logging.WARNING)
|
||||
logging.getLogger("httpx").setLevel(logging.WARNING)
|
||||
logging.getLogger("httpcore").setLevel(logging.WARNING)
|
||||
logging.getLogger("httpclient").setLevel(logging.WARNING)
|
||||
Reference in New Issue
Block a user