List Info

Thread: piping CP 'print' to file is delayed - how to fix ?




piping CP 'print' to file is delayed - how to fix ?
country flaguser name
United States
2007-03-19 19:29:07


On Mar 19, 1:18 am, Jorge Godoy <jgo...gmail.com> wrote:
> "northof40" <shearich...gmail.com> writes:
> > Hi - I start my CP app looking like this :
>
> > nohup python  cposerver.py  > logs/logfilename
>
> > ... so that 'print' statements in the code end up
in logfilename.
>
> > My problem isn't (I think)  a CP one. I find that
print statements are
> > delayed in appearing in logfilename - they do
appear eventually but if
> > you're sitting with a user and the screen crashes
you can't rely on
> > doing 'tail logfilename' and see output which is
relevant to what's
> > just happened.
>
> > I presume that unix only writes the piped output
to the file when it's
> > a got a blocks worth ?
>
> > So my question is does anyone know how I can
'flush' print statements
> > to the piped output.
>
> > Would appreciate any ideas.
>
> Not answering your question, but why are you using
print statements when you
> can use the logging module?  With logging you can have
different levels --
> debug, info, warning, fatal, ... -- and filter then to
different files, as
> well.  Besides, you can have debug logs turned on while
developing and turned
> off while in production...
>

Yes that's a good question. Basically because we'd never
written a CP
app before this one and we were kind of feeling our way for
a long
time - using 'print'  was just easy to start with when there
were a
lot of other things which were new to us and then three
months later
you're still doing it. I suspect replacing the prints with
logs is
what we should do now.

regards

Richard.


> --
> Jorge Godoy      <jgo...gmail.com>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


piping CP 'print' to file is delayed - how to fix ?
user name
2007-03-19 19:47:25
"northof40" <shearichardgmail.com> writes:

> Yes that's a good question. Basically because we'd
never written a CP
> app before this one and we were kind of feeling our way
for a long
> time - using 'print'  was just easy to start with when
there were a
> lot of other things which were new to us and then three
months later
> you're still doing it. I suspect replacing the prints
with logs is
> what we should do now.



It shouldn't be hard.  And it is the best option.  Specially
because you won't
have to change your code when deploying it.

You can also use different namespaces in different parts of
your code so that
you know where you are without needing to resort to things
like

     print "I'm here at the update method of xyz
module"




Learning the logging module will also be useful to your
applications outside
of CP, since it is a standard module in Python (since Python
2.3).  Here's
some help on where to find docs:

   http:/
/docs.python.org/lib/module-logging.html


And here's one TurboGears project example of it:


=================================== (log.cfg)
==================================
# LOGGING
# Logging is often deployment specific, but some handlers
and
# formatters can be defined here.

[logging]
[[formatters]]
[[[message_only]]]
format='*(message)s'

[[[full_content]]]
format='*(asctime)s *(name)s *(levelname)s *(message)s'

[[handlers]]
[[[debug_out]]]
class='StreamHandler'
level='DEBUG'
args='(sys.stdout,)'
formatter='full_content'

[[[access_out]]]
class='StreamHandler'
level='INFO'
args='(sys.stdout,)'
formatter='message_only'

[[[error_out]]]
class='StreamHandler'
level='ERROR'
args='(sys.stdout,)'
============================================================
====================

And inside the application, I simply:

============================================================
====================
import logging
log = logging.getLogger('my_application.controller.module')

...

class MyClass(...):

    ...

    log.debug('Some message: %s' % some_value)
============================================================
====================


So, as you see, it is not all that hard.  And one can get
very sophisticated
with the options available at the docs for it.



Enjoy logging! 



-- 
Jorge Godoy      <jgodoygmail.com>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )