Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'handler': 0.04; 'output': 0.04; 'root': 0.04; 'subject:Python': 0.05; 'debug': 0.05; 'method.': 0.05; '__name__': 0.07; 'permissions': 0.07; 'skip:/ 10': 0.07; 'python': 0.09; '###': 0.09; 'from:addr:matt': 0.09; 'logger': 0.09; 'script,': 0.09; 'stdout': 0.09; 'sys.stderr': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'skip:f 30': 0.15; "'__main__':": 0.16; 'loggin': 0.16; 'stderr:': 0.16; '\xa0maybe': 0.16; 'wrote:': 0.17; '8bit%:3': 0.17; 'module': 0.19; 'define': 0.20; 'email addr:gmail.com>': 0.20; 'import': 0.21; 'file:': 0.22; 'skip:% 10': 0.22; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'script': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'setting': 0.26; 'am,': 0.27; 'logging': 0.27; 'message-id:@mail.gmail.com': 0.27; 'lines': 0.28; 'run': 0.28; 'cat': 0.29; 'url:mailman': 0.29; '8bit%:5': 0.29; 'maybe': 0.29; 'fri,': 0.30; 'writes': 0.30; 'error': 0.30; 'stuff': 0.30; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; 'info': 0.32; 'url:listinfo': 0.32; 'received:74.125.82': 0.33; 'skip:l 40': 0.33; 'hi,': 0.33; 'skip:d 20': 0.34; 'program,': 0.34; 'received:google.com': 0.34; 'whatever': 0.35; 'skip:f 40': 0.35; 'skip:l 30': 0.35; 'add': 0.36; 'but': 0.36; 'received:74.125': 0.36; 'url:org': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'end': 0.40; 'url:mail': 0.40; 'enable': 0.60; 'your': 0.60; 'matthew': 0.65; 'url:info': 0.75; '2013': 0.84; 'use\xa0': 0.84; 'works!': 0.84; 'wilson': 0.91 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type:x-gm-message-state; bh=HNcMPscnaOE8k8n14x278FnHug5EDwL8yy6Tt4BQBrs=; b=cLdwwa7m92sPhk3AW23fCUbOD0j5ZpCJeTQnfK7bqbuvBQdwQjO0n2sHp0DUBCoE/X eSbdMrJUmKi6zhrTzlVMdEsKdk2hBubwvcVzkBJZQvItfR7y2U3zDFBDx+H7WBA5WGNz +IV3jVUcbc1/TDfe/pk5supaXJamij7LQ+WKTwO7s1pBn1EC2Ei9bEm7g2TNF1B7haCh jrMXFgqPgrjagnq0SeyWg0mwmz7XQeADuocxLbWdqC36c6DbzDXqWlJa3PcWFUwW2Kaj eQdHnCLiIJ/HHIQfsKaJe/WzAAPOsBuKiIu7e+vG8NTxDYq2kEYVqhzXkZm3AMRGizk6 wIqQ== MIME-Version: 1.0 X-Received: by 10.180.97.132 with SMTP id ea4mr18943472wib.23.1363088021060; Tue, 12 Mar 2013 04:33:41 -0700 (PDT) In-Reply-To: References: Date: Tue, 12 Mar 2013 07:33:40 -0400 Subject: Re: Missing logging output in Python From: "W. Matthew Wilson" To: gabor.a.halasz@gmail.com Content-Type: multipart/alternative; boundary=f46d04426c4e4b161f04d7b8a9e2 X-Gm-Message-State: ALoCoQlIHcCc5OGGA47vUxpT8mRpu2uFIKtkGcEw+wEuZ3VxJsE3jmRBsbK3WDskFXJ8mCK2Zr2P Cc: python-list@python.org 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: 196 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363088028 news.xs4all.nl 6908 [2001:888:2000:d::a6]:34372 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41115 --f46d04426c4e4b161f04d7b8a9e2 Content-Type: text/plain; charset=ISO-8859-1 I made that code into a program like this: ### BEGIN import logging def configure_logging(): logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)8s %(message)s', datefmt='%Y-%m-%d\t%H:%M:%s', filename='/tmp/logfun.log', filemode='a') # define a Handler that writes INFO messages to sys.stderr console = logging.StreamHandler() console.setLevel(logging.INFO) # set format that is cleaber for console use formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') # tell the handler to use this format console.setFormatter(formatter) # add the handler to the root logger logging.getLogger('').addHandler(console) if __name__ == '__main__': configure_logging() logging.debug('a') logging.info('b') logging.warn('c') logging.error('d') logging.critical('e') ### END and when I run the program, I get INFO and greater messages to stderr: $ python logfun.py root : INFO b root : WARNING c root : ERROR d root : CRITICAL e and I get this stuff in the log file: $ cat /tmp/logfun.log 2013-03-12 07:31:1363087862 root DEBUG a 2013-03-12 07:31:1363087862 root INFO b 2013-03-12 07:31:1363087862 root WARNING c 2013-03-12 07:31:1363087862 root ERROR d 2013-03-12 07:31:1363087862 root CRITICAL e In other words, your code works! Maybe you should check permissions on the file you are writing to. Matt On Fri, Mar 8, 2013 at 9:07 AM, wrote: > Hi, > > I would like to enable loggin in my script using the logging module that > comes with Python 2.7.3. > > I have the following few lines setting up logging in my script, but for > whatever reason I don't seem to get any output to stdout or to a file > provided to the basicConfig method. > > Any ideas? > > # cinfiguring logging > logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s > %(levelname)8s %(message)s', > datefmt='%Y-%m-%d\t%H:%M:%s', > filename=config["currentLoop"], filemode='a') > # define a Handler that writes INFO messages to sys.stderr > console = logging.StreamHandler() > console.setLevel(logging.INFO) > # set format that is cleaber for console use > formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') > # tell the handler to use this format > console.setFormatter(formatter) > # add the handler to the root logger > logging.getLogger('').addHandler(console) > -- > http://mail.python.org/mailman/listinfo/python-list > -- W. Matthew Wilson matt@tplus1.com http://tplus1.com --f46d04426c4e4b161f04d7b8a9e2 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
I made that code into a program like this:

<= div style>### BEGIN

import logging
=
def configure_logging():

=A0 =A0 lo= gging.basicConfig(level=3Dlogging.DEBUG, format=3D'%(asctime)s %(name)-= 12s %(levelname)8s %(message)s',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 datefmt=3D'%Y-%m-%= d\t%H:%M:%s',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= filename=3D'/tmp/logfun.log', filemode=3D'a')
=A0 =A0 # define a Handler that writes INFO messages to sys.st= derr
=A0 =A0 console =3D logging.StreamHandler()
=A0 =A0 console.= setLevel(logging.INFO)

=A0 =A0 # set format that i= s cleaber for console use
=A0 =A0 formatter =3D logging.Formatter= ('%(name)-12s: %(levelname)-8s %(message)s')

=A0 =A0 # tell the handler to use this format
=A0 =A0 console.setFormatter(formatter)

=A0 =A0 #= add the handler to the root logger
=A0 =A0 logging.getLogger(= 9;').addHandler(console)

if __name__ =3D=3D '__main__':

=A0 =A0 configure_logging()

=A0 =A0 loggi= ng.debug('a')
=A0 =A0 log= ging.info('b')
=A0 =A0 logging.warn('c')
=A0 =A0 logging.error('= ;d')
=A0 =A0 logging.critical('e')
### END

and when I = run the program, I get INFO and greater messages to stderr:

$ python logfun.py=A0
root = =A0 =A0 =A0 =A0: INFO =A0 =A0 b
root =A0 =A0 =A0 =A0: WARNING =A0= c
root =A0 =A0 =A0 =A0: ERROR =A0 =A0d
root =A0 =A0 =A0= =A0: CRITICAL e

and I get this stuff in the log file:

=
$ cat /tmp/logfun.log=A0
2013-03-12 07:31:1363087862 root =A0 =A0 =A0 =A0 =A0 =A0DEBU= G a
2013-03-12 07:31:13630878= 62 root =A0 =A0 =A0 =A0 =A0 =A0 INFO b
2013-03-12 07:31:1363087862 root =A0 =A0 =A0 =A0 = =A0WARNING c
2013-03-12 07:31:1363087862 root =A0 =A0 =A0 =A0 =A0 =A0ERROR d
2013-03-12 07:31:136= 3087862 root =A0 =A0 =A0 =A0 CRITICAL e

In o= ther words, your code works! =A0Maybe you should check permissions on the f= ile you are writing to.

Matt


--f46d04426c4e4b161f04d7b8a9e2--