3.4 KiB
Logging in pydase
The pydase library organizes its loggers per module, mirroring the Python package hierarchy. This structured approach allows for granular control over logging levels and behaviour across different parts of the library. Logs can also include details about client identification based on headers sent by the client or proxy, providing additional context for debugging or auditing.
Changing the Log Level
You have two primary ways to adjust the log levels in pydase:
-
Directly targeting
pydaseloggersYou can set the log level for any
pydaselogger directly in your code. This method is useful for fine-tuning logging levels for specific modules withinpydase. For instance, if you want to change the log level of the mainpydaselogger or target a submodule likepydase.data_service, you can do so as follows:# <your_script.py> import logging # Set the log level for the main pydase logger logging.getLogger("pydase").setLevel(logging.INFO) # Optionally, target a specific submodule logger # logging.getLogger("pydase.data_service").setLevel(logging.DEBUG) # Your logger for the current script logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.info("My info message.")This approach allows for specific control over different parts of the
pydaselibrary, depending on your logging needs. -
Using the
ENVIRONMENTenvironment variableFor a more global setting that affects the entire
pydaselibrary, you can utilize theENVIRONMENTenvironment variable. Setting this variable to"production"will configure allpydaseloggers to only log messages of level"INFO"and above, filtering out more verbose logging. This is particularly useful for production environments where excessive logging can be overwhelming or unnecessary.ENVIRONMENT="production" python -m <module_using_pydase>In the absence of this setting, the default behavior is to log everything of level
"DEBUG"and above, suitable for development environments where more detailed logs are beneficial.
Client Identification in Logs
The logging system in pydase includes information about clients based on headers sent by the client or a proxy. The priority for identifying the client is fixed and as follows:
Remote-UserHeader: This header is typically set by authentication servers like Authelia. While it can be set manually by users, its primary purpose is to provide client information authenticated through such servers.X-Client-IDHeader: This header is intended for use by Python clients to pass custom client identification information. It acts as a fallback when theRemote-Userheader is not available.- Default Socket.IO Session ID: If neither of the above headers is present, the system falls back to the default Socket.IO session ID to identify the client.
For example, a log entries might include the following details based on the available headers:
2025-01-20 06:47:50.940 | INFO | pydase.server.web_server.api.v1.application:_get_value:36 - Client [id=This is me!] is getting the value of 'property_attr'
2025-01-20 06:48:13.710 | INFO | pydase.server.web_server.api.v1.application:_get_value:36 - Client [user=Max Muster] is getting the value of 'property_attr'