Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60227

Re: Help me to print to screen as well as log

From Peter Otten <__peter__@web.de>
Subject Re: Help me to print to screen as well as log
Date 2013-11-22 15:26 +0100
Organization None
References <08d39aeb-cc25-4a80-b42e-fd8eac9a9d1a@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3040.1385130369.18130.python-list@python.org> (permalink)

Show all headers | View raw


Himanshu Garg wrote:

> I want that print "hello" should appear on screen as well as get saved in
> a log file.
> 
> How can I accomplish this?

In Python 3 print() is a function -- you can replace it with a custom 
function that invokes the original print() twice.

In both Python 3 and Python 2 you can redirect sys.stdout/stderr to a custom 
object with a write() method.

If you are actually logging I suggest that you look into the logging package 
which allows multiple handlers, see

http://docs.python.org/3.3/howto/logging.html

You will need to replace your print() calls with

some_logger.info(message)

or similar.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Help me to print to screen as well as log Himanshu Garg <hgarg.india@gmail.com> - 2013-11-22 05:51 -0800
  Re: Help me to print to screen as well as log Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-22 14:15 +0000
    Re: Help me to print to screen as well as log Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-22 15:38 +0000
  Re: Help me to print to screen as well as log Peter Otten <__peter__@web.de> - 2013-11-22 15:26 +0100
  Re: Help me to print to screen as well as log Himanshu Garg <hgarg.india@gmail.com> - 2013-11-23 05:11 -0800
    Re: Help me to print to screen as well as log Dave Angel <davea@davea.name> - 2013-11-23 18:02 -0500
  Re: Help me to print to screen as well as log Miki Tebeka <miki.tebeka@gmail.com> - 2013-11-23 16:23 -0800
  Re: Help me to print to screen as well as log Himanshu Garg <hgarg.india@gmail.com> - 2013-11-23 23:00 -0800

csiph-web