farmOS / farmOS.py

A Python library for interacting with farmOS over API.
GNU General Public License v3.0
27 stars 12 forks source link

Add support for logging #21

Closed paul121 closed 4 years ago

paul121 commented 5 years ago

Create a standard practice of logging within the library. Options to log at various levels.

Probably using the built in Python library - https://docs.python.org/3/library/logging.html

paul121 commented 4 years ago

Implemented a lot from this guide: https://docs.python-guide.org/writing/logging/

I was going to follow the logging practice in fastAPI (https://github.com/tiangolo/fastapi/pull/781/commits/57ed8b5fe5fcedb4b959e1231bbad42e79c73c9c) but found that creating loggers under the namespace of the module provides greater control. i.e. logger = logging.getLogger(__name__) instead of logger = logging.getLogger("farmOS")

You can configure farmOS logs to be displayed with the following:

import logging

# Required to init a config on the ROOT logger, that all other inherit from
logging.basicConfig()

 # Configure all loggers under farmOS (farmOS.client, famrOS.session) to desired level
logging.getLogger("farmOS").setLevel(logging.DEBUG)

 # Hide debug logging from the farmOS.session module
logging.getLogger("farmOS.session").setLevel(logging.WARNING)