Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'root': 0.05; 'skip:l 60': 0.07; 'sys': 0.07; 'skip:/ 10': 0.09; 'python': 0.11; 'suggest': 0.14; '"error': 0.16; 'abort': 0.16; 'logger': 0.16; 'stdout': 0.16; 'subject:PYTHON': 0.16; 'import': 0.22; 'team,': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'error': 0.23; 'skip:% 10': 0.24; 'skip:l 30': 0.24; '>': 0.26; 'logging': 0.26; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'file': 0.32; 'screen': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'info': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'displays': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'ensure': 0.60; 'logs': 0.60; 'levels': 0.65; 'url:info': 0.73; 'subject:skip:S 10': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=x8wm1/RYjL3FzfsX4MjXxCElAihmroso0eAKitfcQYA=; b=pf58ijdrW+UsGSxPWQJUQdhErZdQURw/lQw3g5+IQN/s0ryQLG4Ek13q7nxkxzTo8N i7xznUtIVfaXaTiBr3k3HA6ICOEI6sqljiOAtdN/hpYSQ7a8SSVfYXsPjSXeUg0jkd4J Qi1h6di2z/Pk8CsF2soPiEL/lnnpC/0v2mmEf/D3r8kyyaY2AUtNoCOVmRVWUoVomeRu /NxWPjSRDIp6T66j1EruLyZd6SqqnPZ5CDEqGJQELJjpMH/paLvzHMoGqn6QoXxOdRzp MnwX3A7P16Vk2Fgsxjh58HEexkAJJpOEAatmAjYuzdfP1qpOsptz38g1wztPrR4HKBDs z4Kg== MIME-Version: 1.0 X-Received: by 10.152.120.133 with SMTP id lc5mr20095113lab.62.1416829092681; Mon, 24 Nov 2014 03:38:12 -0800 (PST) Date: Mon, 24 Nov 2014 17:08:12 +0530 Subject: PYTHON LOGGING with StreamHandler and FileHandler From: Ganesh Pal To: "python-list@python.org" Content-Type: multipart/alternative; boundary=089e01176919c6dd3f0508993a42 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 92 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416829096 news.xs4all.nl 2919 [2001:888:2000:d::a6]:49775 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81451 --089e01176919c6dd3f0508993a42 Content-Type: text/plain; charset=UTF-8 Hi Team, The below program logs all the messages to the log file and displays the same on the console . Please suggest how do I ensure that error and warning log levels go ONLY to stdout and/or stder Code : import logging import sys # StreamHandler root = logging.getLogger() root.setLevel(logging.DEBUG) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) root.addHandler(ch) # FileHandler logger = logging.getLogger('myapp') ch = logging.FileHandler('/var/tmp/myapp1.log') logger.setLevel(logging.DEBUG) logger.error('We have a problem') logger.info('While this is just chatty') print "error abort" ## should be displayed on screen with using loggin. output : # python modified_logger.py 2014-11-24 11:18:12,355 - myapp - ERROR - We have a problem 2014-11-24 11:18:12,356 - myapp - INFO - While this is just chatty error abort # echo > /var/tmp/myapp1.log # python modified_logger.py 2014-11-24 11:18:45,872 - myapp - ERROR - We have a problem 2014-11-24 11:18:45,872 - myapp - INFO - While this is just chatty error abort Regards, Ganesh --089e01176919c6dd3f0508993a42 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Hi Team,


=
The below program logs all the messages to the log file and disp= lays the same on the console .

Please suggest how = do I ensure that error and warning log levels g= o ONLY to stdout and/or stder


Code :

impor= t logging
import sys

# StreamHandler
root =3D logging.getLogger()
root.setLevel(logging.DEBUG)<= /div>

ch =3D logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter =3D logging.Formatter('= %(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.set= Formatter(formatter)
root.addHandler(ch)


# FileHandler

logger =3D logging.g= etLogger('myapp')
ch =C2=A0=3D logging.FileHandler('/= var/tmp/myapp1.log')
logger.setLevel(logging.DEBUG)

logger.error('We have a problem')
logger.info('While this is just chatty'= )
print "error abort" ## should be displayed on screen = with using loggin.<something>

output :=C2=A0=

# python =C2=A0modified_logger.py
= 2014-11-24 11:18:12,355 - myapp - ERROR - We have a problem
2014-= 11-24 11:18:12,356 - myapp - INFO - While this is just chatty
err= or abort

# echo > /var/tmp/myapp1.log
# python =C2=A0modified_logger.py
2014-11-24 11:18:45,872 - myap= p - ERROR - We have a problem
2014-11-24 11:18:45,872 - myapp - I= NFO - While this is just chatty
error abort

<= /div>
Regards,
Ganesh
--089e01176919c6dd3f0508993a42--