Skip to content

Commit

Permalink
Merge pull request #35 from probabl-ai/feat/logger
Browse files Browse the repository at this point in the history
Add the mandr logger to give the user full control over logging
  • Loading branch information
tuscland authored Jul 12, 2024
2 parents 295d536 + 818610b commit f9de141
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
30 changes: 29 additions & 1 deletion src/mandr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
"""Povide the InfoMander class for managing information in a data science project."""
"""Provide the InfoMander class for managing information in a data science project."""

import logging

import rich.logging

from .infomander import InfoMander

__all__ = ["InfoMander"]


class Handler(rich.logging.RichHandler):
"""A logging handler that renders output with Rich."""

def get_level_text(self, record: rich.logging.LogRecord) -> rich.logging.Text:
"""Get the logger name and levelname from the record."""
levelname = record.levelname
name = record.name
txt = f"<Logger {name} ({levelname})>"

return rich.logging.Text.styled(
txt.ljust(8),
f"logging.level.{levelname.lower()}",
)


formatter = logging.Formatter("%(message)s")
handler = Handler(markup=True)
handler.setFormatter(formatter)

logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
11 changes: 5 additions & 6 deletions src/mandr/app.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"""The webapp that can render the InfoMander objects."""

import json
import logging
from pathlib import Path

from flask import Flask, Response, request
from jinja2 import Template
from rich.console import Console

from .infomander import ARTIFACTS_KEY, LOGS_KEY, VIEWS_KEY, InfoMander
from .templates import TemplateRenderer

console = Console()

app = Flask(__name__)
logger = logging.getLogger(__name__)


def fetch_mander(*path):
Expand Down Expand Up @@ -83,8 +82,8 @@ def render_top_nav(*args):
for g in glob
if g.is_dir() and not g.parts[-1].startswith("_")
]
console.log(f"{links_out=}")
console.log(f"{path_pairs=}")
logger.debug(f"{links_out=}")
logger.debug(f"{path_pairs=}")
return nav_temp.render(path_pairs=path_pairs, links_out=links_out)


Expand Down Expand Up @@ -122,7 +121,7 @@ def home(path):
if len(path) == 0:
return render_mander(*[])
path_parts = path.split("/")
console.log(f"{path_parts=}")
logger.debug(f"{path_parts=}")
if path_parts[0] == "info":
return render_info(*path_parts[1:])
if path_parts[0] == "view":
Expand Down
2 changes: 0 additions & 2 deletions src/mandr/infomander.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

from diskcache import Cache
from joblib import dump
from rich.console import Console

from .templates import TemplateRenderer

console = Console()
LOGS_KEY = "logs"
VIEWS_KEY = "views"
TEMPLATES_KEY = "templates"
Expand Down

0 comments on commit f9de141

Please sign in to comment.