Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper logging #232

Closed
lucasgautheron opened this issue Jun 5, 2021 · 2 comments · Fixed by #441
Closed

Proper logging #232

lucasgautheron opened this issue Jun 5, 2021 · 2 comments · Fixed by #441
Assignees
Labels
enhancement New feature or request

Comments

@lucasgautheron
Copy link
Collaborator

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

@lucasgautheron lucasgautheron added the enhancement New feature or request label Jun 5, 2021
@lucasgautheron lucasgautheron self-assigned this Jun 5, 2021
@LoannPeurey LoannPeurey self-assigned this Aug 3, 2022
@MichaBiriuchinskii
Copy link
Contributor

MichaBiriuchinskii commented Jun 15, 2023

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 :

  • Change print() redirecting them into a logg file.

Responsibilities :

Scripts :

  • Micha - annotations.py
  • Micha - cmdline.py
  • Micha - metrics.py (no prints at all)
  • Élodie - "converters.py"
  • Élodie - "projects.py"
  • Élodie - "utils.py"

Pipelines :

  • Élodie - "init"
  • Élodie - "anonymize.py"
  • Élodie - "aef....py"
  • Élodie - "fake....py"
  • Élodie - "metrics.py"
  • Micha - "metricsFunctions.py" (no prints at all)
  • Micha - "pipeline.py" (no prints at all)
  • Micha - "processors.py"
  • Micha - "samplers.py"
  • Micha - "zooniverse.py"

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

Tuto

Video1

Video2.1

Video2.2

@MichaBiriuchinskii
Copy link
Contributor

MichaBiriuchinskii commented Jul 12, 2023

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}').

@MichaBiriuchinskii MichaBiriuchinskii linked a pull request Jul 25, 2023 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants