Gebruik van logging
Om fouten beter af te vangen en te verwerken in de toolbox kan logging worden ingesteld via de data adapter
import logging
from pathlib import Path
from toolbox_continu_inzicht import Config, DataAdapter
Bij het aanmaken van een data adapter voegen we een basis logger object toe.
Hier zitten verder geen instelling aan gekoppeld, maar wordt de log ‘terug gegeven’ aan de gebruiker.
GlobalVariables:
rootdir: 'data_sets'
DataAdapter:
mycsv_in:
type: csv
file: 'test_csv_in.csv'
= Path.cwd() / "data_sets"
data_path = Config(config_path=data_path / "test_config.yaml")
config
config.lees_config()= DataAdapter(config=config) data_adapter
Het standaard niveau is WARNING
, test en info zien we dan niet
"test") data_adapter.logger.info(
"test") data_adapter.logger.warning(
2025-08-13 15:30:53 WARNING - 666804439: test
Zo kunnen we bijvoorbeeld het niveau aanpassen:
GlobalVariables:
rootdir: 'data_sets'
logging:
level: DEBUG
DataAdapter:
mycsv_in:
type: csv
file: 'test_csv_in.csv'
= Path.cwd() / "data_sets"
data_path = Config(config_path=data_path / "test_config_debug_logging.yaml")
config
config.lees_config()= DataAdapter(config=config) data_adapter
2025-08-13 15:30:53 DEBUG - data_adapter: Logging is ingesteld.
Dan zien we ineens meer logging informatie
input("mycsv_in") data_adapter.
2025-08-13 15:30:53 DEBUG - data_adapter: DataAdapter input: function_input_config={'type': 'csv', 'file': 'test_csv_in.csv'}
objectid | objecttype | parameterid | date_time | value | |
---|---|---|---|---|---|
0 | 1 | measuringstation | 1 | 1726227377000 | 4.8 |
1 | 2 | measuringstation | 1 | 1726227377000 | 5.0 |
2 | 3 | measuringstation | 1 | 1726227377000 | 5.2 |
3 | 4 | measuringstation | 1 | 1726227377000 | 5.4 |
4 | 5 | measuringstation | 1 | 1726227377000 | 5.6 |
Het logging object heeft standaard de naam “toolbox_continu_inzicht”, met de standaard python functionaliteit kunnen we er bij
= logging.getLogger("toolbox_continu_inzicht")
get_log get_log
<Logger toolbox_continu_inzicht (DEBUG)>
of via de data adapter
data_adapter.logger
<Logger toolbox_continu_inzicht (DEBUG)>
Maken we een nieuwe data adapter aan, dan gaat de logging naar het zelfde object, mits we de naam niet aanpassen.
from toolbox_continu_inzicht.proof_of_concept import ValuesDivideTwo
= Path.cwd() / "data_sets"
data_path = Config(config_path=data_path / "test_config_debug_logging.yaml")
config
config.lees_config()= DataAdapter(config=config)
data_adapter = ValuesDivideTwo(data_adapter=data_adapter)
values_divide_two input="mycsv_in", output="mycsv_out") values_divide_two.run(
2025-08-13 15:30:53 DEBUG - data_adapter: Logging is ingesteld.
2025-08-13 15:30:53 DEBUG - data_adapter: DataAdapter input: function_input_config={'type': 'csv', 'file': 'test_csv_in.csv'}
2025-08-13 15:30:53 INFO - example_module: Division started for 5 rows
2025-08-13 15:30:53 DEBUG - example_module: Division with dataframe containing 'Index(['objectid', 'objecttype', 'parameterid', 'date_time', 'value'], dtype='object')' as columns
Tot nu toe ging de logging alleen naar de gebruiker (stderr), maar we kunnen het ook opslaan
GlobalVariables:
rootdir: 'data_sets'
logging:
name: toolbox_continu_inzicht # default
level: DEBUG
mode: w # default
file: hidden_logfile_advanced.log
DataAdapter:
mycsv_in:
type: csv
file: 'test_csv_in.csv'
= Path.cwd() / "data_sets"
data_path = Config(config_path=data_path / "test_config_advanced_logging.yaml")
config
config.lees_config()= DataAdapter(config=config) data_adapter
2025-08-13 15:30:53 DEBUG - data_adapter: Logging is ingesteld.
Zo zien we nu naast de StreamHandler
ook een FileHandler
get_log.handlers
[<StreamHandler stderr (NOTSET)>,
<FileHandler D:\a\toolbox-continu-inzicht\toolbox-continu-inzicht\continu_inzicht\docs\examples\notebooks\data_sets\hidden_logfile_advanced.log (NOTSET)>]
"add own error") data_adapter.logger.debug(
2025-08-13 15:30:53 DEBUG - 393206663: add own error
Ook deze blijft hetzelfde ookal maken wij een nieuw object aan
= Path.cwd() / "data_sets"
data_path = Config(config_path=data_path / "test_config_advanced_logging.yaml")
config
config.lees_config()= DataAdapter(config=config)
data_adapter get_log.handlers
2025-08-13 15:30:53 DEBUG - data_adapter: Logging is ingesteld.
[<StreamHandler stderr (NOTSET)>,
<FileHandler D:\a\toolbox-continu-inzicht\toolbox-continu-inzicht\continu_inzicht\docs\examples\notebooks\data_sets\hidden_logfile_advanced.log (NOTSET)>]
Je hoeft ook maar één keer een bestand op te geven, de andere nemen de instellingen over omdat het object telkens wordt ‘opgehaald’
= Path.cwd() / "data_sets"
data_path = Config(config_path=data_path / "test_config_debug_logging.yaml")
config
config.lees_config()= DataAdapter(config=config)
data_adapter get_log.handlers
2025-08-13 15:30:53 DEBUG - data_adapter: Logging is ingesteld.
[<StreamHandler stderr (NOTSET)>,
<FileHandler D:\a\toolbox-continu-inzicht\toolbox-continu-inzicht\continu_inzicht\docs\examples\notebooks\data_sets\hidden_logfile_advanced.log (NOTSET)>]
De naam en het niveau wordt wel aangepast, dus als we toch geen debug meer willen kan dat op deze manier:
= Path.cwd() / "data_sets"
data_path = Config(config_path=data_path / "test_config_debug_logging.yaml")
config
config.lees_config()"logging"]["level"] = "WARNING"
config.global_variables[= DataAdapter(config=config)
data_adapter get_log.handlers
[<StreamHandler stderr (NOTSET)>,
<FileHandler D:\a\toolbox-continu-inzicht\toolbox-continu-inzicht\continu_inzicht\docs\examples\notebooks\data_sets\hidden_logfile_advanced.log (NOTSET)>]
Advies is dan ook om op een plek je ‘advanced’ log te definiëren, en bij alle andere data adapters de standaard waarde te behouden.