Skip to content

Commit

Permalink
feat(server-utils): add logging configuration to server_utils to be u…
Browse files Browse the repository at this point in the history
…sed by servers.
  • Loading branch information
vegano1 committed Oct 13, 2023
1 parent 981b3cb commit 8ee97dd
Show file tree
Hide file tree
Showing 23 changed files with 521 additions and 250 deletions.
1 change: 1 addition & 0 deletions api/src/opentrons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ async def initialize() -> ThreadManagedHardware:
"""
Initialize the Opentrons hardware returning a hardware instance.
"""
# TODO (ba, 2023-10-12): Remove log init once the opentrons_hardware process is up and running
robot_conf = robot_configs.load()
logging_config.log_init(robot_conf.log_level)

Expand Down
5 changes: 3 additions & 2 deletions robot-server/robot_server/app_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

from opentrons import __version__

from server_utils import PackageName, log_init

from .errors import exception_handlers
from .hardware import start_initializing_hardware, clean_up_hardware
from .persistence import start_initializing_persistence, clean_up_persistence
from .router import router
from .service import initialize_logging
from .service.task_runner import (
initialize_task_runner,
clean_up_task_runner,
Expand Down Expand Up @@ -55,7 +56,7 @@ async def on_startup() -> None:
"""Handle app startup."""
settings = get_settings()

initialize_logging()
log_init(PackageName.ROBOT_SERVER, settings.log_level)
initialize_task_runner(app_state=app.state)
start_initializing_hardware(
app_state=app.state,
Expand Down
142 changes: 0 additions & 142 deletions robot-server/robot_server/service/logging.py

This file was deleted.

5 changes: 5 additions & 0 deletions robot-server/robot_server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,10 @@ class RobotServerSettings(BaseSettings):
),
)

log_level: str = Field(
default="INFO",
description="The log level for the robot server logs.",
)

class Config:
env_prefix = "OT_ROBOT_SERVER_"
3 changes: 2 additions & 1 deletion robot-server/robot_server/system/time_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from typing import Dict, Tuple, Union, cast
from datetime import datetime, timezone
from opentrons.util.helpers import utc_now
from opentrons.config import IS_ROBOT

from server_utils.config import IS_ROBOT

from robot_server.system import errors
from robot_server.service.errors import CommonErrorDef
Expand Down
4 changes: 3 additions & 1 deletion server-utils/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@ push: wheel
.PHONY: push-ot3
push-ot3: sdist
$(call push-python-sdist,$(host),$(ssh_key),$(ssh_opts),$(sdist_file),"/opt/opentrons-system-server","server_utils",,,$(version_file))
$(call push-python-sdist,$(host),$(ssh_key),$(ssh_opts),$(sdist_file),"/opt/opentrons-robot-server","server_utils")
$(call push-python-sdist,$(host),$(ssh_key),$(ssh_opts),$(sdist_file),"/opt/opentrons-robot-server","server_utils",,,$(version_file))
$(call push-python-sdist,$(host),$(ssh_key),$(ssh_opts),$(sdist_file),"/opt/opentrons-update-server","server_utils",,,$(version_file))
$(call push-python-sdist,$(host),$(ssh_key),$(ssh_opts),$(sdist_file),"/opt/ot3usb","server_utils",,,$(version_file))
10 changes: 10 additions & 0 deletions server-utils/server_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@
This package provides common utilities to be shared across any Opentrons servers
intended to run on Opentrons robots.
"""

from .constants import *
from .logging import log_init

print("Im importing server_utils")

__all__ = [
"PackageName",
"log_init",
]
65 changes: 65 additions & 0 deletions server-utils/server_utils/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Common environment data about the robot."""

import os
import sys

from enum import Enum, auto
from typing import Optional
from pathlib import Path

IS_WIN = sys.platform.startswith("win")
IS_OSX = sys.platform == "darwin"
IS_LINUX = sys.platform.startswith("linux")
IS_ROBOT = bool(
IS_LINUX
and (os.environ.get("RUNNING_ON_PI") or os.environ.get("RUNNING_ON_VERDIN"))
)
#: This is the correct thing to check to see if we’re running on a robot
IS_VIRTUAL = bool(os.environ.get("ENABLE_VIRTUAL_SMOOTHIE"))

class SystemArchitecture(Enum):
HOST = auto()
BUILDROOT = auto()
YOCTO = auto()


class SystemOS(Enum):
WIN = auto()
OSX = auto()
LINUX = auto()


OS: SystemOS = SystemOS.LINUX
if IS_WIN:
OS = SystemOS.WIN
elif IS_OSX:
OS = SystemOS.OSX

ARCHITECTURE: SystemArchitecture = SystemArchitecture.HOST
#: The system architecture running

OT_SYSTEM_VERSION = "0.0.0"
#: The semver string of the system

if IS_ROBOT:
if "OT_SYSTEM_VERSION" in os.environ:
OT_SYSTEM_VERSION = os.environ["OT_SYSTEM_VERSION"]
ARCHITECTURE = SystemArchitecture.YOCTO
else:
try:
with open("/etc/VERSION.json") as vj:
contents = json.load(vj)
OT_SYSTEM_VERSION = contents["buildroot_version"]
ARCHITECTURE = SystemArchitecture.BUILDROOT
except Exception:
log.exception("Could not find version file in /etc/VERSION.json")


__all__ = [
"OS",
"SystemOS",
"IS_ROBOT",
"ARCHITECTURE",
"SystemArchitecture",
"OT_SYSTEM_VERSION",
]
12 changes: 12 additions & 0 deletions server-utils/server_utils/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from enum import Enum

VERBOSE = 10

class PackageName(Enum):
"""The types of packages to configure logging"""
ROBOT_SERVER = "robot_server"
SYSTEM_SERVER = "system_server"
UPDATE_SERVER = "otupdate"
HARDWARE_SERVER = "opentrons_hardware"
OT3USBBridge = "ot3usb"

26 changes: 26 additions & 0 deletions server-utils/server_utils/logging/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Logging module used accross servers."""

import logging

from ..constants import VERBOSE
from .config import log_init


logging.addLevelName(VERBOSE, "VERBOSE")


class Logging(logging.Logger):
def __init__(self, *args, **kwargs):
"""initializer"""
super().__init__(*args, **lwargs)

def verbose(self, msg: str, *args, **kwargs) -> None:
"""Custom log level for very chatty messages like can logs."""
if self.isEnabledFor(VERBOSE):
self._log(VERBOSE, msg, args, **kwargs)


__all__ = [
"Logging",
"log_init",
]
Loading

0 comments on commit 8ee97dd

Please sign in to comment.