You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"The logger can be used to send logs at different levels: ``DEBUG``, ``INFO``,\n``WARNING``, ``ERROR`` and ``CRITICAL``. Each of this level is equal to an integer\nvalue. If the logger level is at least equal to the emitted report level, the log is\ndisplayed. Else, it is omitted. By default, the logger is set to the ``WARNING``\nlevel.\n\n"
33
+
]
34
+
},
35
+
{
36
+
"cell_type": "code",
37
+
"execution_count": null,
38
+
"metadata": {
39
+
"collapsed": false
40
+
},
41
+
"outputs": [],
42
+
"source": [
43
+
"print(f\"The level 'INFO' corresponds to the value {logging.INFO}.\")\nprint(f\"The level 'ERROR' corresponds to the value {logging.ERROR}.\")\nlogger.debug(\"Log that will not be displayed.\")\nlogger.warning(\"Log that will be displayed.\")"
44
+
]
45
+
},
46
+
{
47
+
"cell_type": "markdown",
48
+
"metadata": {},
49
+
"source": [
50
+
"The function `~template.set_log_level` can be used to edit the level of the logger.\n\n"
51
+
]
52
+
},
53
+
{
54
+
"cell_type": "code",
55
+
"execution_count": null,
56
+
"metadata": {
57
+
"collapsed": false
58
+
},
59
+
"outputs": [],
60
+
"source": [
61
+
"set_log_level(\"DEBUG\")\nlogger.debug(\"Log that will now be displayed.\")"
62
+
]
63
+
},
64
+
{
65
+
"cell_type": "markdown",
66
+
"metadata": {},
67
+
"source": [
68
+
"By default, the logger has one `~logging.StreamHandler` which outputs to\n``sys.stdout``. The level of both the logger and of this first handler can be changed\nwith `~template.set_log_level`. Additional file handlers can be added with\n`~template.add_file_handler`. Each handler can be set to a different level than the\nlogger.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>For the purpose of this example, a temporary file is used. Logs can be saved to\n any text file, e.g. a ``.txt`` or ``.log`` file.</p></div>\n\n"
69
+
]
70
+
},
71
+
{
72
+
"cell_type": "code",
73
+
"execution_count": null,
74
+
"metadata": {
75
+
"collapsed": false
76
+
},
77
+
"outputs": [],
78
+
"source": [
79
+
"directory = TemporaryDirectory()\nfile = Path(directory.name) / \"mylogs.log\"\nadd_file_handler(file, verbose=\"INFO\") # different level than the logger\nlogger.debug(\"Log displayed but not saved to file.\")\nlogger.info(\"Log displayed and saved to file.\")"
80
+
]
81
+
},
82
+
{
83
+
"cell_type": "markdown",
84
+
"metadata": {},
85
+
"source": [
86
+
"Since the file handler we added is set to the ``INFO`` level, it should capture only\nthe second log.\n\n"
87
+
]
88
+
},
89
+
{
90
+
"cell_type": "code",
91
+
"execution_count": null,
92
+
"metadata": {
93
+
"collapsed": false
94
+
},
95
+
"outputs": [],
96
+
"source": [
97
+
"with open(file) as f:\n lines = f.readlines()\nfor line in lines:\n print(line)"
98
+
]
99
+
},
100
+
{
101
+
"cell_type": "markdown",
102
+
"metadata": {},
103
+
"source": [
104
+
"A message level must be equal or above both the logger and the handler level to be\nemitted on a specific handler. More information on the\n[Python logging documentation](pyLogging_) and on the flowchart below:\n\n.. figure:: ../../_static/logging/flowchart-light.png\n :class: only-light\n\n.. figure:: ../../_static/logging/flowchart-dark.png\n :class: only-dark\n\n\n"
105
+
]
106
+
},
107
+
{
108
+
"cell_type": "markdown",
109
+
"metadata": {},
110
+
"source": [
111
+
"Finally, the handlers are listed in ``logger.handlers``. When an handler is not used\nanymore, it can be closed. This step is optional on Unix systems while it might be\nmantadory depending on the situation on Windows.\n\n"
0 commit comments