Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-05 Thread Peter J. Holzer
On 2023-01-04 12:32:40 -0500, Thomas Passin wrote:
> On 1/3/2023 10:35 AM, c.bu...@posteo.jp wrote:
> > The logging module write everything to stderr no matter which logging
> > level is used.
> 
> The OP wrote this, but it's not so by default.

By default it is - sort of. 

That is all log messages go to stderr, but not all log levels are
logged.

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-05 Thread Thomas Passin

On 1/5/2023 6:27 AM, Peter J. Holzer wrote:

On 2023-01-04 12:32:40 -0500, Thomas Passin wrote:

On 1/3/2023 10:35 AM, c.bu...@posteo.jp wrote:

The logging module write everything to stderr no matter which logging
level is used.


The OP wrote this, but it's not so by default.


By default it is - sort of.

That is all log messages go to stderr, but not all log levels are
logged.


Actually, I misread something else that made me think that non-warning 
messages would go to stdout, but I was mistaken.  As it happens, the 
page in the Python docs only mention stderr twice, both in the same 
paragraph:


"logging.lastResort
A “handler of last resort” is available through this attribute. This is 
a StreamHandler writing to sys.stderr with a level of WARNING, and is 
used to handle logging events in the absence of any logging 
configuration.  The end result is to just print the message to sys.stderr."


Nowhere else on this page is the printed output destination mentioned, 
except that it gets printed on the console, which could be either stderr 
or stdout.


The logging system is so configurable that a user could set a different 
destination for each level of logging.  So it seems that the O.P.'s 
original question about why the package's developers choose stderr for 
all levels can be answered: "They didn't".


--
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-05 Thread Peter J. Holzer
On 2023-01-05 08:31:40 -0500, Thomas Passin wrote:
> The logging system is so configurable that a user could set a different
> destination for each level of logging.  So it seems that the O.P.'s original
> question about why the package's developers choose stderr for all levels can
> be answered: "They didn't".

Which is almost exactly what I wrote in
<20230104162154.uzljittbs6xwt...@hjp.at> ;-)

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-05 Thread Thomas Passin

On 1/5/2023 2:18 PM, Peter J. Holzer wrote:

On 2023-01-05 08:31:40 -0500, Thomas Passin wrote:

The logging system is so configurable that a user could set a different
destination for each level of logging.  So it seems that the O.P.'s original
question about why the package's developers choose stderr for all levels can
be answered: "They didn't".


Which is almost exactly what I wrote in
<20230104162154.uzljittbs6xwt...@hjp.at> ;-)


Yup, two minds are better than one :)

--
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-05 Thread Grant Edwards
On 2023-01-05, Thomas Passin  wrote:

> The logging system is so configurable that...

I find it almost impossible to use unless I copy a working example I
find somewhere. ;)

I'm not at all surprised that the OP didn't understand how it
works. I've been writing Python programs for over 20 years, and it's
beyond me.

--
Grant




-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-05 Thread Thomas Passin

On 1/5/2023 3:29 PM, Grant Edwards wrote:

On 2023-01-05, Thomas Passin  wrote:


The logging system is so configurable that...


I find it almost impossible to use unless I copy a working example I
find somewhere. ;)

I'm not at all surprised that the OP didn't understand how it
works. I've been writing Python programs for over 20 years, and it's
beyond me.


I know what you mean!  I have never used the Python logging package, but 
I inherited a java project that uses Log4j.  Talk about obscure!  The 
python docs are a wonder of clarity beside Log4j.  So most of my Log4j 
configuration has been by copy-pasta, with occasional surprises and 
Log4j bug bites.  Like a handler that deleted its own previous log file 
when it rolled over at midnight - a "feature" that wasn't advertised.


I will say, though, that having used a bit of Log4j must have been 
helpful, since much of what is in the Python logging package docs seemed 
somewhat familiar and even seemed to make sense.


BTW, I was able to re-create that java project using Jython for about 
90% of it.  Way better!


--
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-05 Thread Weatherby,Gerard
logging.basicConfig()
logging.info(“Nice to know”)
logging.debug(“Details for when things are funky”)
logging.warn(“Trouble is brewing”)

From: Python-list  on 
behalf of Grant Edwards 
Date: Thursday, January 5, 2023 at 3:31 PM
To: python-list@python.org 
Subject: Re: What should go to stdout/stderr and why Python logging write 
everything to stderr?
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

On 2023-01-05, Thomas Passin  wrote:

> The logging system is so configurable that...

I find it almost impossible to use unless I copy a working example I
find somewhere. ;)

I'm not at all surprised that the OP didn't understand how it
works. I've been writing Python programs for over 20 years, and it's
beyond me.

--
Grant




--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!l6vSXQFKppEuLS0R5gYeLYaiHyVFfs2Rapqm1oPGEtvnZ5ivQyApZcnJyNTnnH9zEVY80ajNb-HfYkNwgw8fMtsnlSOT$
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-05 Thread Grant Edwards
On 2023-01-05, Weatherby,Gerard  wrote:
> logging.basicConfig()
> logging.info(“Nice to know”)
> logging.debug(“Details for when things are funky”)
> logging.warn(“Trouble is brewing”)

I always seem to need something slightly more complex. Usually something like:

 * Specify a log level on the command line.
 * Allow logging to a file as specified on the command line.
 * Optional timestamps for log messages.

I know, without doubt, that it can easily do all those things. I just
never seem to be able to figure out how unless I can find example code
to look at.

--
Grant

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-05 Thread Thomas Passin

On 1/5/2023 4:28 PM, Weatherby,Gerard wrote:

logging.basicConfig()
logging.info(“Nice to know”)
logging.debug(“Details for when things are funky”)
logging.warn(“Trouble is brewing”)


Not quite -

>>> import logging
>>> logging.basicConfig()
>>> logging.info("Nice to know")
>>> logging.debug("Details for when things are funky")
>>> logging.warn("Trouble is brewing")
:1: DeprecationWarning: The 'warn' function is deprecated, use 
'warning' instead

WARNING:root:Trouble is brewing

Only warning level messages are displayed, unless you do some 
configuration, as mentioned in the logging Howto page:


"The default level is WARNING, which means that only events of this 
level and above will be tracked, unless the logging package is 
configured to do otherwise."


So following the Howto:
>>> logging.basicConfig(level=logging.DEBUG)
>>> logging.info('So should this')
INFO:root:So should this

: Finally!

Not quite straightforward, though it is in the Howto.  Now about those 
handlers ...



From: Python-list  on behalf of 
Grant Edwards 
Date: Thursday, January 5, 2023 at 3:31 PM
To: python-list@python.org 
Subject: Re: What should go to stdout/stderr and why Python logging write 
everything to stderr?
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

On 2023-01-05, Thomas Passin  wrote:


The logging system is so configurable that...


I find it almost impossible to use unless I copy a working example I
find somewhere. ;)

I'm not at all surprised that the OP didn't understand how it
works. I've been writing Python programs for over 20 years, and it's
beyond me.

--
Grant




--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!l6vSXQFKppEuLS0R5gYeLYaiHyVFfs2Rapqm1oPGEtvnZ5ivQyApZcnJyNTnnH9zEVY80ajNb-HfYkNwgw8fMtsnlSOT$


--
https://mail.python.org/mailman/listinfo/python-list


Re: What should go to stdout/stderr and why Python logging write everything to stderr?

2023-01-05 Thread Thomas Passin

On 1/5/2023 4:24 PM, Stefan Ram wrote:

You often can replace threads in tkinter by coroutines using
   asyncio when you write a replacement for the mainloop of
   tkinter that uses asyncio. Now, try to read only the official
   documentation of asyncio and tkinter and figure out only from
   this how to get such a program to work!


Cool! Can we have a link, please?

--
https://mail.python.org/mailman/listinfo/python-list


Re: asyncio and tkinter (was: What should go to stdout/stderr and why Python logging write everything to stderr?)

2023-01-05 Thread Thomas Passin

On 1/5/2023 7:52 PM, Stefan Ram wrote:

Thomas Passin  writes:

On 1/5/2023 4:24 PM, Stefan Ram wrote:

You often can replace threads in tkinter by coroutines using
asyncio when you write a replacement for the mainloop of
tkinter that uses asyncio. Now, try to read only the official
documentation of asyncio and tkinter and figure out only from
this how to get such a program to work!

Cool! Can we have a link, please?


   I do not post links, but tried to create a minimal example app.


Thanks!  That's not exactly obvious, is it?  Congratulations on getting 
it working.



import asyncio
import logging
import tkinter

# This program was tested on Windows, it is experimental.
# It should open a tkinter root window.
# Ctrl-E should create a task. You should be able to create
# a new task even while one task is already running.
# Each task will end after about 10 seconds.
# Ctrl-X should exit the process.
# Or, use the menu "Action".

# I (Stefan Ram) designed and wrote the program, but especially
# the code in "app_async_mainloop" and "terminate_tasks" was
# written following educational material from the World-Wide Web.

class app_class( tkinter.Tk ):
 def __init__( self, *args, **kwargs ):
 self.is_running = 1_000_000
 super().__init__( *args, **kwargs )
 root = self
 root.protocol( "WM_DELETE_WINDOW", self.app_exit_macro )
 async def app_example_task( self, example_label ):
 try:
 for i in range( 10 ):
 example_label[ 'text' ]=str( i )
 await asyncio.sleep( 1 )
 except asyncio.CancelledError:
 pass
 example_label.destroy()
 def app_example_macro( self ):
 root = self
 example_label=tkinter.Label( root )
 example_label.pack()
 asyncio.get_running_loop().create_task\
 ( self.app_example_task( example_label ))
 root = self
 def terminate_tasks( self ):
 loop = asyncio.get_running_loop()
 pending = asyncio.all_tasks( loop=loop )
 label_tasks = []
 for task in pending:
 if 'app_example_task' in str( task ):
 label_tasks.append( task )
 task.cancel()
 group = asyncio.gather( *label_tasks, return_exceptions=True )
 try:
 loop.run_until_complete( group )
 except AssertionError:
 # ignoring an assertion error on Windows I do not understand
 pass
 def app_exit_macro( self ):
 self.terminate_tasks()
 self.is_running = 99
 root = self
 root.destroy()
 def app_add_basemenu( self, example=False ):
 root = self
 menubar = tkinter.Menu( root )
 menu = tkinter.Menu( menubar, tearoff=0 )
 name = "Actions"; menu = tkinter.Menu( menubar, tearoff=0 )
 if example:
 text = "Example";
 callback = self.app_example_macro; menu.add_command\
 ( label=text, underline=0, command=callback,
   accelerator="Control-e" );
 root.bind\
 ( "", lambda event, callback=callback:callback() )
 text = "Exit";
 callback = self.app_exit_macro
 menu.add_command\
 ( label=text, underline=1,
   command=callback, accelerator="Control-x" )
 root.bind\
 ( "", lambda event,callback=callback: callback() )
 menubar.add_cascade( label=name, underline=0, menu=menu )
 root.config( menu=menubar )
 async def app_async_mainloop( self ):
 root = self
 root.willdispatch()
 root.tk.dooneevent( tkinter._tkinter.DONT_WAIT )
 while self.is_running > 0:
 if self.is_running < 1_000_000: self.is_running -= 1
 try:
 await asyncio.sleep\
 ( tkinter._tkinter.getbusywaitinterval() / 10_000 )
 root.tk.dooneevent( tkinter._tkinter.DONT_WAIT )
 root.update()
 except tkinter.TclError:
 break
 async def app_async_main( self ):
 try:
 await self.app_async_mainloop()
 except asyncio.CancelledError:
 logging.debug( f'asyncio.CancelledError' )
 def app_async_run( self ):
 asyncio.run( self.app_async_main() )

app = app_class()
app.app_add_basemenu(example=True)
app.app_async_run()





--
https://mail.python.org/mailman/listinfo/python-list