Skip to content

Commit 75cd142

Browse files
committed
WIP: Pseudocode example of adding a logging.config.dictConfig cli argument
This lets you easily load a custom logging configuration including handlers
1 parent dd1ef2a commit 75cd142

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

preditor/cli.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22

33
import logging
4+
import logging.config
45
import sys
56
from distutils.spawn import find_executable
67

@@ -35,13 +36,25 @@ def get_app_id(name, is_default):
3536
default_if_no_args=True,
3637
context_settings=CONTEXT_SETTINGS,
3738
)
38-
def cli():
39+
@click.option(
40+
"--logging-cfg",
41+
# Note: Using eager makes it so logging is configured as early as possible
42+
# based on this argument.
43+
is_eager=True,
44+
type=click.Path(file_okay=True, resolve_path=True),
45+
envvar='PREDITOR_LOGGING_CFG',
46+
help="Path to json file defining a logging configuration based on "
47+
"logging.config.dictConfig. If not specified the preditor prefs are used."
48+
"If the env var PREDITOR_LOGGING_CFG is set it will use that value.",
49+
)
50+
def cli(logging_config):
3951
"""PrEditor is a Qt based python console and code editor. It runs in many
4052
DCC's like Maya, Houdini, Nuke, and 3ds Max.
4153
4254
To see help for launching the PrEditor gui, use `preditor launch -h`.
4355
"""
44-
pass
56+
if logging_config is not None:
57+
preditor.config.logging_cfg = logging_config
4558

4659

4760
# launch

preditor/config.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import
22

3+
import json
34
import logging
45
from functools import wraps
56

@@ -47,6 +48,7 @@ def __init__(self):
4748
self._locks = {}
4849
self._name = None
4950
self._logging = False
51+
self._logging_cfg = None
5052
self._headless_callback = None
5153
self._on_create_callback = None
5254
self._parent_callback = None
@@ -62,6 +64,7 @@ def dump(self, indent=0):
6264
f"{' ' * indent}excepthooks: {self.excepthooks!r}",
6365
f"{' ' * indent}error_dialog_class: {self.error_dialog_class!r}",
6466
f"{' ' * indent}logging: {self.logging}",
67+
f"{' ' * indent}logging_cfg: {self.logging_cfg}",
6568
f"{' ' * indent}headless_callback: {self.headless_callback!r}",
6669
f"{' ' * indent}parent_callback: {self.parent_callback!r}",
6770
f"{' ' * indent}on_create_callback: {self.on_create_callback!r}",
@@ -216,6 +219,27 @@ def logging(self, state):
216219
self._logging = state
217220
self.update_logging()
218221

222+
@property
223+
def logging_cfg(self):
224+
"""If set use this json file to configure logging instead of preditor prefs.
225+
226+
This can be used to load handlers and other customization's. Using this
227+
disables saving of the logging configuration to user prefs.
228+
"""
229+
return self._logging_cfg
230+
231+
@logging_cfg.setter
232+
def logging_cfg(self, logging_cfg):
233+
# TODO: if this is not None then it should disable saving of the logging prefs
234+
# TODO: Should this use @set_if_unlocked() to disable updating?
235+
# TODO: Should we add some shortcuts to files stored in the logging_cfgs dir?
236+
# For example: "preditor\resource\logging_cfgs\default_cfg.json"
237+
self._logging_cfg = logging_cfg
238+
with open(logging_cfg) as fle:
239+
cfg = json.load(fle)
240+
logging.config.dictConfig(cfg)
241+
self.update_logging()
242+
219243
@property
220244
def name(self):
221245
"""The name to use for the global instance of PrEditor.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"version": 1,
3+
"disable_existing_loggers": false,
4+
"formatters": {
5+
"standard": {
6+
"format": "%(name)s - %(levelname)s - %(message)s"
7+
}
8+
},
9+
"handlers": {
10+
"console": {
11+
"class": "logging.StreamHandler",
12+
"formatter": "standard",
13+
"stream": "ext://sys.stdout"
14+
}
15+
},
16+
"loggers": {
17+
"": {
18+
"handlers": ["console"],
19+
"propagate": true
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)