Python Forum

Full Version: logging messages ahead of print messages
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I was trying to play with logging and understand.
One thing i noticed got my attention and would like to understand the reason for that. Can you help me on that.

Why are the log messages displayed ahead of print messages, though they are not coded in that specific sequence?
In this example, i was hoping the Set2 logs come after the print messages.

Please note that i am aware that "logging.basicConfig(level=logging.INFO)" will not take effect and that is what i was playing with.

import logging

print("")
logging.debug("Set1 : This is a debug message")
logging.info("Set1 : This is a info message")
logging.warning("Set1 : This is a warning message")
logging.error("Set1 : This is a error message")
logging.critical("Set1 : This is a critical message")

print("The default serverity level is WARNING. So we have WARNING and above messages displayed")
print("")
print("Attempting to change the severity level to INFO")
print("However, due to limitation on configuring the logger this will not take effect")
logging.basicConfig(level=logging.INFO)

print("")
logging.debug("Set2 : This is a debug message")
logging.info("Set2 : This is a info message")
logging.warning("Set2 : This is a warning message")
logging.error("Set2 : This is a error message")
logging.critical("Set2 : This is a critical message")
WARNING:root:Set1 : This is a warning message
ERROR:root:Set1 : This is a error message
CRITICAL:root:Set1 : This is a critical message
WARNING:root:Set2 : This is a warning message
ERROR:root:Set2 : This is a error message
CRITICAL:root:Set2 : This is a critical message

The default serverity level is WARNING. So we have WARNING and above messages displayed

Attempting to change the severity level to INFO
However, due to limitation on configuring the logger this will not take effect
I ran the code and it was order as expected
Output:
WARNING:root:Set1 : This is a warning message ERROR:root:Set1 : This is a error message CRITICAL:root:Set1 : This is a critical message The default serverity level is WARNING. So we have WARNING and above messages displayed Attempting to change the severity level to INFO However, due to limitation on configuring the logger this will not take effect WARNING:root:Set2 : This is a warning message ERROR:root:Set2 : This is a error message CRITICAL:root:Set2 : This is a critical message
I got the same output as Yoriz using idle, linux, 3.53
Thanks.
@Yoriz
@joe_momma

Interesting !! You are correct. I tried in windows command prompt and it worked as expected.

Earlier i was using Spyder from anaconda package and it's IPython has a different outcome.

I guess i should try command prompt as well, if i find something interesting like this.
I think it depends on the terminal you use. I use xonsh and have sometimes problems with my terminal.
Also ncurses seems not to work with my configuration. In this case I have use /bin/bash.
You have also to recognize, that you have 3 streams: Standard input/output/error

Exceptions and logging is always on stderr. Output of data is stdout.
The print function outputs the text by default on stdout, but you can change it.

If you repeat the test, where the order was wrong, add following to your print function:
print('message', file=sys.stderr)
Tell us, what happens.
(Jun-14-2019, 02:40 PM)DeaD_EyE Wrote: [ -> ]I think it depends on the terminal you use. I use xonsh and have sometimes problems with my terminal.
Also ncurses seems not to work with my configuration. In this case I have use /bin/bash.
You have also to recognize, that you have 3 streams: Standard input/output/error

Exceptions and logging is always on stderr. Output of data is stdout.
The print function outputs the text by default on stdout, but you can change it.

If you repeat the test, where the order was wrong, add following to your print function:
print('message', file=sys.stderr)
Tell us, what happens.


The outcome was as expected.
(Jun-14-2019, 02:40 PM)DeaD_EyE Wrote: [ -> ]I think it depends on the terminal you use. I use xonsh and have sometimes problems with my terminal.
Also ncurses seems not to work with my configuration. In this case I have use /bin/bash.
You have also to recognize, that you have 3 streams: Standard input/output/error

Exceptions and logging is always on stderr. Output of data is stdout.
The print function outputs the text by default on stdout, but you can change it.

If you repeat the test, where the order was wrong, add following to your print function:
print('message', file=sys.stderr)
Tell us, what happens.

The results are as expected.
Can you please elaborate?

Does error messages(stderr) get more priority over the normal output messages (stdout) ?