Skip to content

Commit bd2f54d

Browse files
authored
Merge pull request #1850 from Dominik-Vogel/enable_logging_by_default
Call `start_all_logging` on qcodes import
2 parents 3236b2c + 723a241 commit bd2f54d

File tree

6 files changed

+104
-5
lines changed

6 files changed

+104
-5
lines changed

docs/examples/15_minutes_to_QCoDeS.ipynb

+19-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"cell_type": "markdown",
9494
"metadata": {},
9595
"source": [
96-
"In every measurement session, it is highly recommended to have QCoDeS logging turned on. This will allow you to have all the logs in case troubleshooting is required. To enable logging, we add the following single line of code at the beginnig of our scripts after the imports:"
96+
"In every measurement session, it is highly recommended to have QCoDeS logging turned on. This will allow you to have all the logs in case troubleshooting is required. To enable logging, we can either add the following single line of code at the beginnig of our scripts after the imports:"
9797
]
9898
},
9999
{
@@ -121,6 +121,24 @@
121121
"start_all_logging()"
122122
]
123123
},
124+
{
125+
"cell_type": "markdown",
126+
"metadata": {},
127+
"source": [
128+
"or we can configure qcodes to automatically start logging on every import of qcodes, by running the following code once. (This will persist the current configuration in `~\\qcodesrc.json`)"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": 2,
134+
"metadata": {},
135+
"outputs": [],
136+
"source": [
137+
"from qcodes import config\n",
138+
"config.logger.start_logging_on_import = 'always'\n",
139+
"config.save_to_home()"
140+
]
141+
},
124142
{
125143
"cell_type": "markdown",
126144
"metadata": {},

docs/examples/logging/logging_example.ipynb

+20-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"source": [
1717
"## TL;DR\n",
1818
" * There is a QCoDeS logging module: `qcodes.utils.logger`\n",
19-
" * Call `logger.start_all_logging` at the start of every script/session to make sure all log messages get stored to the `.qcodes/logs` folder in your home directory.\n",
19+
" * Call `logger.start_all_logging` at the start of every script/session to make sure all log messages get stored to the `.qcodes/logs` folder in your home directory, or set `logger.start_logging_on_import` to `True` in your `qcodesrc.json` configuration.\n",
2020
" * For debugging purposes you can log messages of an individual instrument (and see the VISA dialog).\n",
2121
" * You can obtain all logging messages in a `pandas.DataFrame` for further analysis."
2222
]
@@ -106,6 +106,24 @@
106106
"2. Enable logging of the IPython commands into a separate log file."
107107
]
108108
},
109+
{
110+
"cell_type": "markdown",
111+
"metadata": {},
112+
"source": [
113+
"Alternatively we can configure qcodes to automatically start logging on every import of qcodes, by running the following code once. (This will persist the current configuration in `~\\qcodesrc.json`)"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": null,
119+
"metadata": {},
120+
"outputs": [],
121+
"source": [
122+
"from qcodes import config\n",
123+
"config.logger.start_logging_on_import = 'always'\n",
124+
"config.save_to_home()"
125+
]
126+
},
109127
{
110128
"cell_type": "markdown",
111129
"metadata": {},
@@ -1857,7 +1875,7 @@
18571875
"name": "python",
18581876
"nbconvert_exporter": "python",
18591877
"pygments_lexer": "ipython3",
1860-
"version": "3.7.4"
1878+
"version": "3.7.5"
18611879
},
18621880
"toc": {
18631881
"base_numbering": 1,

qcodes/__init__.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
# config
66

77
from qcodes.config import Config
8+
from qcodes.logger import start_all_logging
9+
from qcodes.logger.logger import conditionally_start_all_logging
810
from qcodes.utils.helpers import add_to_spyder_UMR_excludelist
911
from .version import __version__
1012

13+
config: Config = Config()
14+
15+
conditionally_start_all_logging()
16+
1117
# we dont want spyder to reload qcodes as this will overwrite the default station
1218
# instrument list and running monitor
1319
add_to_spyder_UMR_excludelist('qcodes')
14-
config: Config = Config()
20+
1521

1622
from qcodes.version import __version__
1723

qcodes/config/qcodesrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"loglevel": "WARNING",
88
"file_loglevel": "INFO"
99
},
10-
"logger":{
10+
"logger": {
11+
"start_logging_on_import": "if_telemetry_set_up",
1112
"console_level": "WARNING",
1213
"file_level": "INFO",
1314
"logger_levels":{

qcodes/config/qcodesrc_schema.json

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
"description": "controls all settings related to the logging module `qcodes.utils.logger`.",
6262
"type" : "object",
6363
"properties" : {
64+
"start_logging_on_import": {
65+
"type": "string",
66+
"enum": ["always", "never", "if_telemetry_set_up"],
67+
"description": "whether to call `start_all_logging` on qcodes import time. Valid values are 'always', 'never', 'if_telemetry_set_up'.",
68+
"default": "if_telemetry_set_up"
69+
},
6470
"console_level" :{
6571
"type" : "string",
6672
"description": "control logging level of console output",

qcodes/logger/logger.py

+50
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,56 @@ def start_all_logging() -> None:
293293
start_logger()
294294

295295

296+
def conditionally_start_all_logging() -> None:
297+
"""Start logging if qcodesrc.json setup for it and in tool environment.
298+
299+
This function will start logging if the session is not being executed by
300+
a tool such as pytest and under the following conditions depending on the
301+
qcodes configuration of ``config.logger.start_logging_on_import``:
302+
303+
For ``never``:
304+
305+
don't start logging automatically
306+
307+
For ``always``:
308+
309+
Always start logging when not in test environment
310+
311+
For ``if_telemetry_set_up``:
312+
313+
Start logging if the GUID components and the instrumentation key for
314+
telemetry are set up, and not in a test environment.
315+
"""
316+
def start_logging_on_import() -> bool:
317+
config = qc.config
318+
if config.logger.start_logging_on_import == 'always':
319+
return True
320+
elif config.logger.start_logging_on_import == 'never':
321+
return False
322+
elif config.logger.start_logging_on_import == 'if_telemetry_set_up':
323+
return (
324+
config.GUID_components.location != 0 and
325+
config.GUID_components.work_station != 0 and
326+
config.telemetry.instrumentation_key != \
327+
"00000000-0000-0000-0000-000000000000"
328+
)
329+
else:
330+
raise RuntimeError('Error in qcodesrc validation.')
331+
332+
def running_in_test_or_tool() -> bool:
333+
import sys
334+
tools = (
335+
'pytest.py',
336+
'pytest',
337+
'_jb_pytest_runner.py', # Jetbrains Pycharm
338+
'testlauncher.py' # VSCode
339+
)
340+
return any(sys.argv[0].endswith(tool) for tool in tools)
341+
342+
if start_logging_on_import() and not running_in_test_or_tool():
343+
start_all_logging()
344+
345+
296346
@contextmanager
297347
def handler_level(level: LevelType,
298348
handler: Union[logging.Handler,

0 commit comments

Comments
 (0)