Python Forum
logging messages ahead of print messages
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
logging messages ahead of print messages
#1
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
Reply
#2
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
Reply
#3
I got the same output as Yoriz using idle, linux, 3.53
Reply
#4
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.
Reply
#5
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(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.
Reply
#7
(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) ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Script that alternates between 2 text messages DiscoMatic 1 517 Dec-12-2023, 03:02 PM
Last Post: buran
  Can I use logging in a class (without multiple messages) mevan 2 591 Oct-16-2023, 11:08 PM
Last Post: mevan
  How to continuously receive messages until specified to stop using Bleak jacobbreen25 3 2,121 Dec-28-2022, 04:25 PM
Last Post: jacobbreen25
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,874 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  Reordering output messages paulo79 4 1,577 May-25-2022, 04:46 PM
Last Post: menator01
  pySerial - missed messages q_nerk 2 1,740 Jan-27-2022, 09:59 PM
Last Post: Jeff_t
  logging vs print syafiq14 2 1,909 Aug-28-2021, 10:23 AM
Last Post: snippsat
  Logstash - sending Logstash messages to another host in case of Failover in python Suriya 0 1,665 Jul-27-2021, 02:02 PM
Last Post: Suriya
Question Yielding ahead of time Daring_T 5 2,433 Jun-01-2021, 02:43 AM
Last Post: Daring_T
  User serial/pyserial to send messages to an arudino via terminal manually bkapadia 2 2,721 Mar-10-2021, 11:26 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020