Skip to content

Commit 2ae7d95

Browse files
authored
Fix logger (#274)
* remove automatic setup_logger() call * update test logger * use root logger in public api if setup_config() has not been called * add publicAPI Dataset logger test * add setup_logging() call in publicAPI doc notebooks --------- Co-authored-by: Gauthier BERTHOMIEU <gauthier.berthomieu@ensta.fr> (rien à voir avec Gautzilla 🥷)
1 parent 84c2d73 commit 2ae7d95

6 files changed

Lines changed: 135 additions & 45 deletions

File tree

docs/source/example_ltas_public.ipynb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@
1111
},
1212
"outputs": [],
1313
"source": [
14-
"# Executing this cell will disable all TQDM outputs in stdout.\n",
14+
"# Executing this cell will:\n",
15+
"\n",
16+
"# Disable all TQDM outputs in stdout.\n",
1517
"import os\n",
1618
"\n",
17-
"os.environ[\"DISABLE_TQDM\"] = \"True\""
19+
"os.environ[\"DISABLE_TQDM\"] = \"True\"\n",
20+
"\n",
21+
"# Setup the python logger for the Public API\n",
22+
"from osekit import setup_logging\n",
23+
"\n",
24+
"setup_logging() # Overwrites the default logger to"
1825
]
1926
},
2027
{

docs/source/example_multiple_spectrograms_public.ipynb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@
1111
},
1212
"outputs": [],
1313
"source": [
14-
"# Executing this cell will disable all TQDM outputs in stdout.\n",
14+
"# Executing this cell will:\n",
15+
"\n",
16+
"# Disable all TQDM outputs in stdout.\n",
1517
"import os\n",
1618
"\n",
17-
"os.environ[\"DISABLE_TQDM\"] = \"True\""
19+
"os.environ[\"DISABLE_TQDM\"] = \"True\"\n",
20+
"\n",
21+
"# Setup the python logger for the Public API\n",
22+
"from osekit import setup_logging\n",
23+
"\n",
24+
"setup_logging() # Overwrites the default logger to"
1825
]
1926
},
2027
{

docs/source/example_reshaping_multiple_files_public.ipynb

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,27 @@
22
"cells": [
33
{
44
"cell_type": "code",
5+
"execution_count": null,
56
"id": "dc7ebca70b3b5da",
67
"metadata": {
78
"tags": [
89
"remove-cell"
910
]
1011
},
12+
"outputs": [],
1113
"source": [
12-
"# Executing this cell will disable all TQDM outputs in stdout.\n",
14+
"# Executing this cell will:\n",
15+
"\n",
16+
"# Disable all TQDM outputs in stdout.\n",
1317
"import os\n",
1418
"\n",
15-
"os.environ[\"DISABLE_TQDM\"] = \"True\""
16-
],
17-
"outputs": [],
18-
"execution_count": null
19+
"os.environ[\"DISABLE_TQDM\"] = \"True\"\n",
20+
"\n",
21+
"# Setup the python logger for the Public API\n",
22+
"from osekit import setup_logging\n",
23+
"\n",
24+
"setup_logging() # Overwrites the default logger to"
25+
]
1926
},
2027
{
2128
"cell_type": "markdown",
@@ -35,10 +42,12 @@
3542
},
3643
{
3744
"cell_type": "code",
45+
"execution_count": null,
3846
"id": "bb002105fc9632e8",
3947
"metadata": {
4048
"tags": []
4149
},
50+
"outputs": [],
4251
"source": [
4352
"from pathlib import Path\n",
4453
"\n",
@@ -52,9 +61,7 @@
5261
")\n",
5362
"\n",
5463
"dataset.build()"
55-
],
56-
"outputs": [],
57-
"execution_count": null
64+
]
5865
},
5966
{
6067
"cell_type": "markdown",
@@ -64,8 +71,10 @@
6471
},
6572
{
6673
"cell_type": "code",
74+
"execution_count": null,
6775
"id": "a29c761d4bbd5303",
6876
"metadata": {},
77+
"outputs": [],
6978
"source": [
7079
"print(f\"{' DATASET ':#^60}\")\n",
7180
"print(f\"{'Begin:':<30}{str(dataset.origin_dataset.begin):>30}\")\n",
@@ -86,9 +95,7 @@
8695
" for f in dataset.origin_files\n",
8796
" ],\n",
8897
").set_index(\"Name\")"
89-
],
90-
"outputs": [],
91-
"execution_count": null
98+
]
9299
},
93100
{
94101
"cell_type": "markdown",
@@ -98,10 +105,12 @@
98105
},
99106
{
100107
"cell_type": "code",
108+
"execution_count": null,
101109
"id": "b4c2c3857ffcb60f",
102110
"metadata": {
103111
"tags": []
104112
},
113+
"outputs": [],
105114
"source": [
106115
"from osekit.public_api.analysis import Analysis, AnalysisType\n",
107116
"from pandas import Timestamp, Timedelta\n",
@@ -113,9 +122,7 @@
113122
" data_duration=Timedelta(seconds=5),\n",
114123
" name=\"reshape_example\",\n",
115124
")"
116-
],
117-
"outputs": [],
118-
"execution_count": null
125+
]
119126
},
120127
{
121128
"cell_type": "markdown",
@@ -128,19 +135,19 @@
128135
},
129136
{
130137
"cell_type": "code",
138+
"execution_count": null,
131139
"id": "2f799a26d626e418",
132140
"metadata": {
133141
"tags": []
134142
},
143+
"outputs": [],
135144
"source": [
136145
"# Returns a Core API AudioDataset that matches the analysis\n",
137146
"audio_dataset = dataset.get_analysis_audiodataset(analysis=analysis)\n",
138147
"\n",
139148
"# Filter the returned AudioDataset\n",
140149
"audio_dataset.data = [ad for ad in audio_dataset.data if not ad.is_empty]"
141-
],
142-
"outputs": [],
143-
"execution_count": null
150+
]
144151
},
145152
{
146153
"cell_type": "markdown",
@@ -150,15 +157,15 @@
150157
},
151158
{
152159
"cell_type": "code",
160+
"execution_count": null,
153161
"id": "9b65cfdc720d50e6",
154162
"metadata": {
155163
"tags": []
156164
},
165+
"outputs": [],
157166
"source": [
158167
"dataset.run_analysis(analysis=analysis, audio_dataset=audio_dataset)"
159-
],
160-
"outputs": [],
161-
"execution_count": null
168+
]
162169
},
163170
{
164171
"cell_type": "markdown",
@@ -168,8 +175,10 @@
168175
},
169176
{
170177
"cell_type": "code",
178+
"execution_count": null,
171179
"id": "3cb0adbb96d2251a",
172180
"metadata": {},
181+
"outputs": [],
173182
"source": [
174183
"pd.DataFrame(\n",
175184
" [\n",
@@ -182,25 +191,23 @@
182191
" for ad in dataset.get_dataset(analysis.name).data\n",
183192
" ],\n",
184193
").set_index(\"Exported file\")"
185-
],
186-
"outputs": [],
187-
"execution_count": null
194+
]
188195
},
189196
{
190197
"cell_type": "code",
198+
"execution_count": null,
191199
"id": "58b7aec2d8863a02",
192200
"metadata": {
193201
"tags": [
194202
"remove-cell"
195203
]
196204
},
205+
"outputs": [],
197206
"source": [
198207
"# Reset the dataset to get all files back to place.\n",
199208
"\n",
200209
"dataset.reset()"
201-
],
202-
"outputs": [],
203-
"execution_count": null
210+
]
204211
}
205212
],
206213
"metadata": {

src/osekit/__init__.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import logging.config
24
import os.path
35
from pathlib import Path
@@ -9,14 +11,25 @@
911

1012
__all__ = [
1113
"Job_builder",
14+
"setup_logging",
1215
"utils",
1316
]
1417

15-
16-
def _setup_logging(
17-
config_file="logging_config.yaml",
18+
def setup_logging(
19+
config_file: str | Path = "logging_config.yaml",
1820
default_level: int = logging.INFO,
1921
) -> None:
22+
"""Configure logger using a configuration yaml file.
23+
24+
Parameters
25+
----------
26+
config_file: str | Path
27+
Path to a logging configuration file
28+
default_level: int
29+
Logging level to use
30+
Default value, `logging.INFO`
31+
32+
"""
2033
user_config_file_path = Path(os.getenv("OSMOSE_USER_CONFIG", ".")) / config_file
2134
default_config_file_path = Path(__file__).parent / config_file
2235

@@ -35,6 +48,5 @@ def _setup_logging(
3548
logging.config.dictConfig(logging_config)
3649
else:
3750
logging.basicConfig(level=default_level)
38-
39-
40-
_setup_logging()
51+
msg = "Configuration file not found, using default configuration."
52+
logging.getLogger(__name__).warning(msg)

src/osekit/public_api/dataset.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ def build(self) -> None:
136136
source=self.folder,
137137
destination=self.folder / "other",
138138
excluded_paths={file.path for file in ads.files}
139-
| set((self.folder / "log").iterdir())
139+
| set(
140+
(self.folder / "log").iterdir()
141+
if (self.folder / "log").exists()
142+
else ()
143+
)
140144
| {self.folder / "log"},
141145
)
142146
self._sort_dataset(ads)
@@ -146,6 +150,16 @@ def build(self) -> None:
146150
self.logger.info("Build done!")
147151

148152
def _create_logger(self) -> None:
153+
if not logging.getLogger("dataset").handlers:
154+
message = (
155+
"Logging has not been configured. "
156+
"The dataset will use the root logger. "
157+
"Use osekit.setup_logging() if wanted."
158+
)
159+
logging.warning(message)
160+
self.logger = logging.getLogger()
161+
return
162+
149163
logs_directory = self.folder / "log"
150164
if not logs_directory.exists():
151165
logs_directory.mkdir(mode=DPDEFAULT)

0 commit comments

Comments
 (0)