Skip to content

Commit

Permalink
2.8.1 - runtime log-level control
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Sep 15, 2023
1 parent 004ba50 commit b101fdb
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
- run: |
mk python-release owner=vkottler \
repo=runtimepy version=2.8.0
repo=runtimepy version=2.8.1
if: |
matrix.python-version == '3.11'
&& matrix.system == 'ubuntu-latest'
Expand Down
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[DESIGN]
max-args=8
max-attributes=12
max-parents=10
max-parents=11

[MESSAGES CONTROL]
disable=too-few-public-methods
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.3
hash=86fa83db6bac485c345afa670f2b159d
hash=5d61ecd838cd4afcef3266828cbc68d5
=====================================
-->

# runtimepy ([2.8.0](https://pypi.org/project/runtimepy/))
# runtimepy ([2.8.1](https://pypi.org/project/runtimepy/))

[![python](https://img.shields.io/pypi/pyversions/runtimepy.svg)](https://pypi.org/project/runtimepy/)
![Build Status](https://github.com/vkottler/runtimepy/workflows/Python%20Package/badge.svg)
Expand Down
2 changes: 1 addition & 1 deletion local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
major: 2
minor: 8
patch: 0
patch: 1
entry: runtimepy
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "runtimepy"
version = "2.8.0"
version = "2.8.1"
description = "A framework for implementing Python services."
readme = "README.md"
requires-python = ">=3.11"
Expand Down
4 changes: 2 additions & 2 deletions runtimepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.3
# hash=6ef03f741188d3cc145268db1edd0d8d
# hash=31ccb8de44b1ddacae998ffa068a10ae
# =====================================

"""
Expand All @@ -10,7 +10,7 @@

DESCRIPTION = "A framework for implementing Python services."
PKG_NAME = "runtimepy"
VERSION = "2.8.0"
VERSION = "2.8.1"

# runtimepy-specific content.
METRICS_NAME = "metrics"
53 changes: 53 additions & 0 deletions runtimepy/mixins/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
A module implementing a logger-mixin extension.
"""

# built-in
import logging

# third-party
from vcorelib.logging import LoggerMixin

# internal
from runtimepy.channel.environment import ChannelEnvironment
from runtimepy.enum.registry import RuntimeIntEnum


class LogLevel(RuntimeIntEnum):
"""A runtime enumeration for log level."""

DEBUG = logging.DEBUG
INFO = logging.INFO
WARNING = logging.WARNING
ERROR = logging.ERROR
CRITICAL = logging.CRITICAL


class LoggerMixinLevelControl(LoggerMixin):
"""A logger mixin that exposes a runtime-controllable level."""

def _log_level_changed(self, _: int, new_value: int) -> None:
"""Handle a change in log level."""

self.logger.setLevel(new_value)
self.logger.info("Log level set to '%s'.")

def setup_level_channel(
self,
env: ChannelEnvironment,
name: str = "log_level",
initial: str = "info",
) -> None:
"""Add a commandable log-level channel to the environment."""

chan = env.int_channel(
name, enum=LogLevel.register_enum(env.enums), commandable=True
)

# Set up change handler.
chan[0].raw.register_callback(self._log_level_changed)

# Set the initial log level.
env.set(name, initial)

del chan
7 changes: 4 additions & 3 deletions runtimepy/net/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@

# third-party
from vcorelib.asyncio import log_exceptions as _log_exceptions
from vcorelib.logging import LoggerMixin as _LoggerMixin
from vcorelib.logging import LoggerType as _LoggerType

# internal
from runtimepy.channel.environment import ChannelEnvironment
from runtimepy.channel.environment.command import ChannelCommandProcessor
from runtimepy.metrics import ConnectionMetrics
from runtimepy.mixins.environment import ChannelEnvironmentMixin
from runtimepy.mixins.logging import LoggerMixinLevelControl
from runtimepy.primitives import Bool
from runtimepy.primitives.byte_order import DEFAULT_BYTE_ORDER, ByteOrder

BinaryMessage = _Union[bytes, bytearray, memoryview]


class Connection(_LoggerMixin, ChannelEnvironmentMixin, _ABC):
class Connection(LoggerMixinLevelControl, ChannelEnvironmentMixin, _ABC):
"""A connection interface."""

uses_text_tx_queue = True
Expand All @@ -42,7 +42,7 @@ def __init__(
) -> None:
"""Initialize this connection."""

_LoggerMixin.__init__(self, logger=logger)
LoggerMixinLevelControl.__init__(self, logger=logger)
self._enabled = Bool(True)

# A queue for out-going text messages. Connections that don't use
Expand All @@ -64,6 +64,7 @@ def __init__(
self.metrics = ConnectionMetrics()

ChannelEnvironmentMixin.__init__(self, env=env)
self.setup_level_channel(self.env)
self.command = ChannelCommandProcessor(self.env, self.logger)
if add_metrics:
self.register_connection_metrics(self.metrics)
Expand Down
7 changes: 4 additions & 3 deletions runtimepy/task/basic/periodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from typing import Optional as _Optional

# third-party
from vcorelib.logging import LoggerMixin as _LoggerMixin
from vcorelib.math import DEFAULT_DEPTH as _DEFAULT_DEPTH
from vcorelib.math import MovingAverage as _MovingAverage
from vcorelib.math import RateTracker as _RateTracker
Expand All @@ -24,12 +23,13 @@
from runtimepy.channel.environment.command import ChannelCommandProcessor
from runtimepy.metrics import PeriodicTaskMetrics
from runtimepy.mixins.environment import ChannelEnvironmentMixin
from runtimepy.mixins.logging import LoggerMixinLevelControl
from runtimepy.primitives import Bool as _Bool
from runtimepy.primitives import Double as _Double
from runtimepy.primitives import Float as _Float


class PeriodicTask(_LoggerMixin, ChannelEnvironmentMixin, _ABC):
class PeriodicTask(LoggerMixinLevelControl, ChannelEnvironmentMixin, _ABC):
"""A class implementing a simple periodic-task interface."""

def __init__(
Expand All @@ -43,7 +43,7 @@ def __init__(
"""Initialize this task."""

self.name = name
_LoggerMixin.__init__(self, logger=_getLogger(self.name))
LoggerMixinLevelControl.__init__(self, logger=_getLogger(self.name))
self._task: _Optional[_asyncio.Task[None]] = None

self.period_s = _Float()
Expand All @@ -58,6 +58,7 @@ def __init__(
self.metrics = metrics

ChannelEnvironmentMixin.__init__(self, env=env)
self.setup_level_channel(self.env)
self.command = ChannelCommandProcessor(self.env, self.logger)
self.register_task_metrics(self.metrics)

Expand Down

0 comments on commit b101fdb

Please sign in to comment.