Skip to content

Commit

Permalink
Always include JSON log payload when loading the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Sep 20, 2024
1 parent 2ec73b3 commit 39ae810
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### ✨ Improved

* Do not report a thermistor warning every interval.
* Always include JSON log payload when loading the DB.

### 🔧 Fixed

Expand Down
21 changes: 11 additions & 10 deletions src/lvmcryo/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from lvmcryo.config import Actions, Config, InteractiveMode, NotificationLevel
from lvmcryo.runner import post_fill_tasks
from lvmcryo.tools import add_json_handler


LOCKFILE = pathlib.Path("/data/lvmcryo.lock")
Expand Down Expand Up @@ -374,7 +375,7 @@ async def ln2(

from rich.prompt import Confirm

from sdsstools.logger import CustomJsonFormatter, get_logger
from sdsstools.logger import get_logger

from lvmcryo.handlers.ln2 import LN2Handler, get_now
from lvmcryo.notifier import Notifier
Expand Down Expand Up @@ -447,16 +448,16 @@ async def ln2(

if write_json:
json_path = config.log_path.with_suffix(".json")
json_handler = FileHandler(str(json_path), mode="w")
json_handler.setLevel(5)
json_handler.setFormatter(CustomJsonFormatter())
log.addHandler(json_handler)

elif config.notify:
# If notifying, start a file logger, but to a temporary location. This is
# just to be able to send the log body in a notification email.
add_json_handler(log, json_path)

else:
# We're still creating log files, but to a temporary location. This is
# just to be able to send the log body in a notification email and include
# it when loading the DB.
temp_file = NamedTemporaryFile()
log.start_file_logger(temp_file.name)
json_path = pathlib.Path(temp_file.name).with_suffix(".json")
json_handler = add_json_handler(log, json_path)

if verbose:
log.sh.setLevel(5)
Expand Down Expand Up @@ -578,7 +579,7 @@ async def ln2(
"error": str(error) if error else None,
"action": action.value,
"log_file": str(config.log_path) if config.log_path else None,
"json_file": str(json_path) if json_path else None,
"json_file": str(json_path) if json_path and write_json else None,
"log_data": log_data,
"configuration": configuration_json,
"valve_times": handler.get_valve_times(as_string=True),
Expand Down
13 changes: 13 additions & 0 deletions src/lvmcryo/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import asyncio
import contextlib
import logging
import os
import pathlib
import time
Expand All @@ -24,6 +25,7 @@
from rich.console import Console
from rich.progress import BarColumn, MofNCompleteColumn, Progress, TaskID, TextColumn

from sdsstools.logger import CustomJsonFormatter
from sdsstools.utils import run_in_executor


Expand Down Expand Up @@ -249,3 +251,14 @@ async def o2_alert(route: str = "http://lvm-hub.lco.cl:8080/api/alerts"):

except Exception as ee:
raise RuntimeError(f"Error reading alerts: {ee}")


def add_json_handler(log: logging.Logger, json_path: os.PathLike | pathlib.Path):
"""Adds a JSON handler to a logger."""

json_handler = logging.FileHandler(str(json_path), mode="w")
json_handler.setLevel(5)
json_handler.setFormatter(CustomJsonFormatter())
log.addHandler(json_handler)

return json_handler

0 comments on commit 39ae810

Please sign in to comment.