2

Python's logging facility comes with several levels of severity:

  • CRITICAL
  • ERROR
  • WARNING*
  • INFO
  • DEBUG
  • NOTSET

*The root logger defaults to WARNING.


How does one deal with regular messages in this setup?

With "regular messages" I mean those that are not warnings but should always be shown to the user.

An example could be:

Loop over a list of files and apply an accumulating operation to them. A "regular message" could be "currently working on {filename}". The INFO level might hold the status of individual sub-steps of the file treatment that the user may not be interested in knowing most of the time. On the WARNING level could be potential problems like two files containing differing entries for the same key.

I think that I want a level between WARNING and INFO, which might be called NOTICE, NOTE, REGULAR or similar. This level would be my default instead of WARNING. Why does logging not provide such a level?

I understand that I can add such a level fairly easily but the logging manual strongly argues against custom levels. Thus, I assume there must be a canonical way of logging messages like the above ...


EDIT: Just to clarify regarding the vote to close for being "opinion-based": I would like to know how logging is supposed to be used sticking to the given set of levels and conventions. Thus, I would think that the correct answer is not necessarily an opinion but should be the best practice of using this module.

The manual states "Defining your own levels is possible, but should not be necessary, as the existing levels have been chosen on the basis of practical experience". In my example above, I seem to be lacking one level: either "regular messages" or -- if I switch to INFO meaning "regular messages" -- something like "verbose info" for the user.

2
  • A "regular message" that is displayed to the user has the INFO level, while the level that "might hold the status of individual sub-steps of the file treatment that the user may not be interested in knowing most of the time" is intuitively the DEBUG level. Why does logging not provide such a level? 1. Because there's no strict convention on what the logging levels are and it would be impossible to satisfy everyone's needs. 2. In your case, the defined levels are more than enough IMO. Commented Aug 7, 2019 at 10:58
  • Hmm, I am not convinced yet. If there's no strict convention (which is obviously true), why is logging so strict about it's own convention? I know the answer is: in order to have compatibility between modules written by different people. Anyway, if I follow your suggestion of using INFO for user-visible messages and DEBUG for the developer, I am now lacking something between those two levels: "verbose info" for the user. Finally, if that was the intended way, why isn't INFO the default level? Commented Aug 7, 2019 at 12:56

1 Answer 1

2

Log messages are not really indented for the user. What you are describing sounds more like regular output. However there are at least two possible ways to solve this. Either make the regular message INFO level and move the not-as-interesting messages to the DEBUG level, or use a log filter. To quote the documentation:

Filters can be used by Handlers and Loggers for more sophisticated filtering than is provided by levels.

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe my misunderstanding is exactly answered by your second sentence!? When you say "sounds more like regular output", do you mean I should actually just use regular print() for "regular messages" and logging stays separate?
If it's output that is intended to be seen by the regular user, and it's a command line script, than yes print is absolutely appropriate for that.
So, to be clear: you'd say print plus logging? I am after levels of verbosity in order to achieve a functionality like e.g. ssh -v, ssh -vv, ssh -vvv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.