Skip to content

Commit

Permalink
[resotolib][feat] Add log counter (#1885)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias authored Jan 19, 2024
1 parent 01dc405 commit c83481c
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions resotolib/resotolib/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Logger,
)


from prometheus_client import Counter
from resotolib.args import ArgumentParser
from resotolib.types import Json

Expand All @@ -29,6 +29,8 @@
DEBUG5 = DEBUG - 4
TRACE = DEBUG - 5

LogRecordCounter = Counter("log_record_counter", "Number of logs by severity", ["component", "level"])

getLogger().setLevel(ERROR)
getLogger("resoto").setLevel(INFO)

Expand Down Expand Up @@ -115,6 +117,16 @@ def format(self, record: LogRecord) -> str:
return json.dumps(message_dict, default=str)


class PrometheusLoggingCounter(StreamHandler): # type: ignore
def __init__(self, component: str) -> None:
super().__init__()
self.component = component

def emit(self, record: LogRecord) -> None:
LogRecordCounter.labels(component=self.component, level=record.levelname).inc()
super().emit(record)


def setup_logger(
proc: str,
*,
Expand All @@ -123,11 +135,12 @@ def setup_logger(
quiet: bool = False,
level: Optional[str] = None,
json_format: bool = True,
count_logs: bool = True,
) -> None:
handler = PrometheusLoggingCounter(proc) if count_logs else StreamHandler()
# override log output via env var
plain_text = os.environ.get("RESOTO_LOG_TEXT", "false").lower() == "true"
if json_format and not plain_text:
handler = StreamHandler()
formatter = JsonFormatter(
{
"timestamp": "asctime",
Expand All @@ -144,7 +157,7 @@ def setup_logger(
log_format = f"%(asctime)s|{proc}|%(levelname)5s|%(process)d|%(threadName)10s %(message)s"
# allow to define the log format via env var
log_format = os.environ.get("RESOTO_LOG_FORMAT", log_format)
basicConfig(format=log_format, datefmt="%y-%m-%d %H:%M:%S", force=force)
basicConfig(handlers=[handler], format=log_format, datefmt="%y-%m-%d %H:%M:%S", force=force)
argv = sys.argv[1:]
if level:
getLogger("resoto").setLevel(level)
Expand Down

0 comments on commit c83481c

Please sign in to comment.