LAAC-LSCP / ChildProject

Python package for the management of day-long recordings of children.
https://childproject.readthedocs.io
MIT License
13 stars 5 forks source link

Proper logging #232

Closed lucasgautheron closed 5 months ago

lucasgautheron commented 3 years ago

Is your feature request related to a problem? Please describe.

The use of printf everywhere getting messy and unclear

Describe the solution you'd like

https://stackoverflow.com/questions/42388844/where-to-configure-logging

MichaBiriuchinskii commented 1 year ago

Hi, I'm not sure whether it's exactly the same issue, but we're going to work on a similar problem. Here we'll write down all our actions and responsibilities so that it's easier to track our progress.

The task :

Responsibilities :

Scripts :

Pipelines :

15/06/2023 - start of work on this issue

Tuto

Video1

Video2.1

Video2.2

MichaBiriuchinskii commented 1 year ago

I've seen a lot of similar cases, so I want to add some information how to handle them here.

Let's say, we have the following code :

proc = subprocess.Popen(args, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) #on lance la commande définie dans args, on dit qu'on n'a pas besoin de stdout, et s'il y a quelque erreur, on print cela (stdout, stderr) = proc.communicate() success = proc.returncode == 0 if not success: print(stderr, file=sys.stderr)

To turn the print into a logger, we simply need to refer to the variable, without any extra parameters : logger_annotations.error(stderr)

Another thing which is worth explaining is the loggin insert-variables.

When it comes to choosing between %s and %d in logging messages, it depends on the type of the variable you are inserting and the desired behavior:

In general, if we are confident that the variable we are inserting is always going to be an integer, we can use %d for clarity (as I already started doing). However, if the variable can be of different types, or if we prefer a more flexible approach, we can use %s to allow for automatic string conversion.

It's worth noting that in newer versions of Python (3.6 and above), there is an alternative method of string formatting using f-strings, which provide a more concise and readable syntax. For example, instead of %s, we can use {} as a placeholder, and directly insert the variable using f-strings, like logging.warning('{}'.format(warning_name)) or logging.warning(f'{warning_name}').