Skip to content

Commit 84fa5c7

Browse files
committed
Add new logging functionality configurer to main script
1 parent 096949e commit 84fa5c7

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

streamdeck/__main__.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import importlib.util
44
import json
55
import logging
6-
import os
76
from argparse import ArgumentParser
87
from pathlib import Path
98
from typing import TYPE_CHECKING, cast
@@ -21,6 +20,7 @@
2120
StreamDeckConfigDict,
2221
)
2322
from streamdeck.manager import PluginManager
23+
from streamdeck.utils.logging import configure_streamdeck_logger
2424

2525

2626
if TYPE_CHECKING:
@@ -30,16 +30,7 @@
3030
from typing_extensions import Self # noqa: UP035
3131

3232

33-
logger = logging.getLogger("streamdeck-plugin-sdk")
34-
35-
logger.addHandler(logging.StreamHandler())
36-
37-
# TODO: Add better functionality for setting up logging to save to files in the proper streamdeck log directory.
38-
file_handler = logging.FileHandler(os.path.expanduser("~/plugin.log"))
39-
file_handler.setLevel(logging.DEBUG)
40-
logger.addHandler(file_handler)
41-
42-
logger.info("Starting run...")
33+
logger = logging.getLogger("streamdeck")
4334

4435

4536
def setup_cli() -> ArgumentParser:
@@ -65,14 +56,21 @@ def setup_cli() -> ArgumentParser:
6556

6657
# Options that will always be passed in by the StreamDeck software when running this plugin.
6758
parser.add_argument("-port", dest="port", type=int, help="Port", required=True)
68-
parser.add_argument("-pluginUUID", dest="pluginUUID", type=str, help="pluginUUID", required=True)
69-
parser.add_argument("-registerEvent", dest="registerEvent", type=str, help="registerEvent", required=True)
59+
parser.add_argument(
60+
"-pluginUUID", dest="pluginUUID", type=str, help="pluginUUID", required=True
61+
)
62+
parser.add_argument(
63+
"-registerEvent", dest="registerEvent", type=str, help="registerEvent", required=True
64+
)
7065
parser.add_argument("-info", dest="info", type=str, help="info", required=True)
7166

7267
return parser
7368

7469

75-
def determine_action_scripts(plugin_dir: Path | None, action_scripts: list[str] | None) -> list[str]:
70+
def determine_action_scripts(
71+
plugin_dir: Path | None,
72+
action_scripts: list[str] | None,
73+
) -> list[str]:
7674
"""Determine the action scripts to be loaded based on provided arguments.
7775
7876
plugin_dir and action_scripts cannot both have values -> either only one of them isn't None, or they are both None.
@@ -102,7 +100,6 @@ def determine_action_scripts(plugin_dir: Path | None, action_scripts: list[str]
102100
raise KeyError(msg) from e
103101

104102

105-
106103
def read_streamdeck_config_from_pyproject(plugin_dir: Path) -> StreamDeckConfigDict:
107104
"""Get the streamdeck section from a plugin directory by reading pyproject.toml.
108105
@@ -178,12 +175,12 @@ def _load_module_from_file(filepath: Path) -> ModuleType:
178175
# Create a module specification for a module located at the given filepath.
179176
# A "specification" is an object that contains information about how to load the module, such as its location and loader.
180177
# "module.name" is an arbitrary name used to identify the module internally.
181-
spec: ModuleSpec = importlib.util.spec_from_file_location("module.name", str(filepath)) # type: ignore
178+
spec: ModuleSpec = importlib.util.spec_from_file_location("module.name", str(filepath)) # type: ignore
182179
# Create a new module object from the given specification.
183180
# At this point, the module is created but not yet loaded (i.e. its code hasn't been executed).
184181
module: ModuleType = importlib.util.module_from_spec(spec)
185182
# Load the module by executing its code, making available its functions, classes, and variables.
186-
spec.loader.exec_module(module) # type: ignore
183+
spec.loader.exec_module(module) # type: ignore
187184

188185
return module
189186

@@ -205,6 +202,10 @@ def main():
205202
info = json.loads(args.info)
206203
plugin_uuid = info["plugin"]["uuid"]
207204

205+
# After configuring once here, we can grab the logger in any other module with `logging.getLogger("streamdeck")`, or
206+
# a child logger with `logging.getLogger("streamdeck.mycomponent")`, all with the same handler/formatter configuration.
207+
configure_streamdeck_logger(name="streamdeck", plugin_uuid=plugin_uuid)
208+
208209
action_scripts = determine_action_scripts(
209210
plugin_dir=args.plugin_dir,
210211
action_scripts=args.action_scripts,
@@ -223,6 +224,10 @@ def main():
223224
)
224225

225226
for action in actions:
227+
# Configure a logger for each action, giving it the last part of its uuid as logger name.
228+
action_component_name = action.uuid.split(".")[-1]
229+
configure_streamdeck_logger(name=action_component_name, plugin_uuid=plugin_uuid)
230+
226231
manager.register_action(action)
227232

228233
manager.run()

0 commit comments

Comments
 (0)