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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'method.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Help': 0.11; 'python': 0.11; 'suggest': 0.14; 'garg': 0.16; 'invokes': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:log': 0.16; 'subject:screen': 0.16; 'wrote:': 0.18; 'print': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'file.': 0.24; 'logging': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'appear': 0.29; 'allows': 0.31; 'url:python': 0.33; 'screen': 0.34; 'url:org': 0.36; 'should': 0.36; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'url:3': 0.61; 'write()': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Help me to print to screen as well as log
Date Fri, 22 Nov 2013 15:26:31 +0100
Organization None
References <08d39aeb-cc25-4a80-b42e-fd8eac9a9d1a@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084aa86.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3040.1385130369.18130.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385130369 news.xs4all.nl 15926 [2001:888:2000:d::a6]:44178
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60227

Show key headers only | 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