GlacioHack / xdem

Analysis of digital elevation models (DEMs)
https://xdem.readthedocs.io/
MIT License
134 stars 39 forks source link

[POC] use logging module #565

Open adebardo opened 3 weeks ago

adebardo commented 3 weeks ago

Context

When using xdem errors and other types of information are displayed via print. The use of the logging module is more relevant, especially with the arrival of the CLI. This ticket, therefore proposes to replace all prints of xdem in logging.

First, it is good to read the [documentation] (https://docs.python.org/en/3/howto/logging.html) in order to master the different levels and constraints related to the use

Code

The files affected by the changes are as follows: -xdem/examples.py

For CLI

Doc

Some help for an API use

import logging  

# Configuration 
logging.basicConfig(level=logging.DEBUG,  
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',  
                    datefmt='%Y-%m-%d %H:%M:%S',  
                    handlers=[  
                        logging.FileHandler('app.log'),  # saving in a file  
  logging.StreamHandler()  # Print in console  
  ])  

logger = logging.getLogger('mylogger')  

def division(a, b):  
    try:  
        result = a / b  
  except ZeroDivisionError:  
        logger.error("Error : division by zero")  
    else:  
        logger.info("The %s / %s = %s", a, b, result)  
        return result  

division(10, 2)  
division(10, 0)

Tests

Estimation 3d

duboise-cnes commented 2 weeks ago

Thanks @adebardo for the issue. It's ok for me globally. Some remarks and complements from my experience of other projects:

adebardo commented 2 weeks ago

Thank you for this feedback.
1 - I acknowledge the possibility of adding this to the config file indeed.
2 - I’ve noted the point about the logging format. 3 - I’ll also add more global logging management.

adehecq commented 1 week ago

It would be great to have the logging option indeed and be able to set it up through the CLI and API. We can also possibly add it to a global config file like what is done in geoutils.

  • for many reasons (performance and compatibily), f-string is not so well in logging.

Just out of curiosity, what is wrong with the f-string? Why would it have lower performance? I got used to the f-strings now because they are much easier to read. So I would try to use f-string whenever possible, i.e. outside of logging.