Skip to content

Commit 48b55e4

Browse files
authored
RSDK-9921 - named module logger (#858)
1 parent c52a0c4 commit 48b55e4

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/viam/module/module.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import io
3+
import os
34
import logging as pylogging
45
import sys
56
from inspect import iscoroutinefunction
@@ -35,8 +36,6 @@
3536
from .service import ModuleRPCService
3637
from .types import Reconfigurable, Stoppable
3738

38-
LOGGER = logging.getLogger(__name__)
39-
4039

4140
def _parse_module_args() -> argparse.Namespace:
4241
"""
@@ -56,6 +55,7 @@ class Module:
5655
_lock: Lock
5756
parent: Optional[RobotClient] = None
5857
server: Server
58+
logger: pylogging.Logger
5959

6060
@classmethod
6161
def from_args(cls) -> Self:
@@ -116,6 +116,12 @@ def __init__(self, address: str, *, log_level: int = logging.INFO) -> None:
116116
self._address = address
117117
self.server = Server(resources=[], module_service=ModuleRPCService(self))
118118
self._log_level = log_level
119+
120+
module_name = os.environ.get('VIAM_MODULE_NAME')
121+
# this can happen if the user is running an old version of viam-server that doesn't set `VIAM_MODULE_NAME`
122+
if module_name is None:
123+
module_name = __name__
124+
self.logger = logging.getLogger(module_name)
119125
self._ready = True
120126
self._lock = Lock()
121127

@@ -130,7 +136,7 @@ async def _connect_to_parent(self):
130136
log_level=self._log_level,
131137
),
132138
)
133-
LOGGER.debug("Starting module logging")
139+
self.logger.debug("Starting module logging")
134140
logging.setParent(self.parent)
135141

136142
async def _get_resource(self, name: ResourceName) -> ResourceBase:
@@ -159,13 +165,13 @@ async def start(self):
159165

160166
async def stop(self):
161167
"""Stop the module service and gRPC server"""
162-
LOGGER.debug("Shutting down module")
168+
self.logger.debug("Shutting down module")
163169
try:
164170
logging.shutdown()
165171
if self.parent is not None:
166172
await self.parent.close()
167173
except Exception as e:
168-
LOGGER.error("Encountered error while shutting down module", exc_info=e)
174+
self.logger.error("Encountered error while shutting down module", exc_info=e)
169175

170176
def set_ready(self, ready: bool):
171177
"""Set the module's ready state. The module automatically sets to READY on load. Setting to False can be useful

0 commit comments

Comments
 (0)