Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'scripts': 0.03; 'example:': 0.03; 'output': 0.05; 'ignored': 0.07; '"__main__":': 0.09; '===': 0.09; '__name__': 0.09; 'main()': 0.09; 'subject:modules': 0.09; 'whichever': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '24,': 0.16; 'a.py': 0.16; 'b.py': 0.16; 'c.py': 0.16; 'eof': 0.16; 'gpg': 0.16; 'https': 0.16; 'imports': 0.16; 'logger': 0.16; 'modules,': 0.16; 'normally.': 0.16; 'singleton': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'skip:f 30': 0.19; 'import': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'skip:% 10': 0.24; 'skip:l 30': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'logging': 0.26; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; "skip:' 10": 0.31; 'class': 0.32; 'skip:c 30': 0.32; 'url:python': 0.33; 'running': 0.33; 'moment': 0.34; 'skip:_ 10': 0.34; "can't": 0.35; 'connection': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'starting': 0.37; 'skip:[ 10': 0.38; 'pm,': 0.38; 'skip:p 20': 0.39; 'url:mail': 0.40; 'logs': 0.60; 'skip:c 50': 0.60; 'new': 0.61; 'entire': 0.61; 'from:charset:utf-8': 0.61; 'first': 0.61; 'such': 0.63; 'our': 0.64; 'pick': 0.64; 'subject:. ': 0.67; 'date,': 0.68; 'everywhere.': 0.84; 'skip:: 40': 0.84; '***': 0.95; 'url:tk': 0.95; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=Xte7tASD03fbY5cH+VgQytCxo+Keh+XFk/LcaMMPbpk=; b=TS8+AIoi71nMdbY2LVfnQp97TnSjazJBzP0BfFS+FdvtKOHohYTey9NMgS09R50TH7 MX4ayBUnjtTQnxqnaXAj+zmRJpExraohKsdQcSmPtJUiCTs4x3STyNYAyARbpmLIDA/1 1ZQyJr+OQz+zcqUITOmXGV1ExlPqQUtj22typMSzInE+EWebUZyw4nFICX31K4kQebkD OJfdyrkL6ho49XCWykWoRujiiqGvx/4xQdMNLMDmcrBdxPcYlm/Q8c2KOOaqfaKJbgvg tqzknkTMovFYft4Cfwk+McGAl208yoA648cRA4RmaHofx99SbhxEeCPPuqUq1z4ts1pX JzTw== X-Received: by 10.43.103.195 with SMTP id dj3mr19396778icc.3.1366825431223; Wed, 24 Apr 2013 10:43:51 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: =?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?= Date: Wed, 24 Apr 2013 19:43:31 +0200 Subject: Re: improvements sought re. logging across modules To: jkn+gg@nicorp.co.uk Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 120 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366825433 news.xs4all.nl 15948 [2001:888:2000:d::a6]:38618 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44277 On Wed, Apr 24, 2013 at 6:54 PM, The Night Tripper wr= ote: > Hi all > I have a small suite of python modules, say > > A.py > B.py > C.py > > which can be invoked in a variety of ways. eg. > > 1) A.py is invoked directly; this imports and uses B.py and C.py > > 2) B.py is invoked; this imports and uses A.py and C.py > > I use the logging module in all of these python modules, and I want to be > able to use a single logger across the entire suite of whichever set of > scripts is running. > > The way I do this at the moment is to have a separate module mylogger.py: > > =3D=3D mylogger.py =3D=3D > > import logging > > class MyLogger: #using python 2.4 ;-o > def __init__(self): > self.log =3D logging.getLogger(MY_APP_NAME) > def setupLogging(self): > self.log.setlevel(logging.DEBUG) > # ... > > # our singleton logging object > mylogger =3D Mylogger() > # EOF > > and then in my other modules A.py, B.py etc. I have something like: > > =3D=3D A.py =3D=3D > > import mylogger > gLog =3D mylogger.mylogger > > if __name__ =3D=3D "__main__": > gLog.setupLogging() > gLog.info("Module A running as main") > main() > #EOF > > =3D=3D B.py =3D=3D > > import mylogger > gLog =3D mylogger.mylogger > > if __name__ =3D=3D "__main__": > gLog.setupLogging() > gLog.info("Module B running as main") > main() > # EOF > > This works, but I can't help thinking I'm missing a trick here. Any > suggestions? > > Thanks > j^n > > -- > http://mail.python.org/mailman/listinfo/python-list No need to do such magic. Just set logging up as you would normally. The first logging instance should pick up logs from everywhere. For example: =3D=3D=3D aurqt/aqds.py =3D=3D=3D class AQDS: logging.basicConfig(format=3D'%(asctime)-15s [%(levelname)-7s] ' ':%(name)-10s: %(message)s', filename=3Dos.path.join(confdir, 'aurqt.log'), level=3Dlogging.DEBUG) log =3D logging.getLogger('aurqt') console =3D logging.StreamHandler() console.setLevel(logging.INFO) console.setFormatter(logging.Formatter('[%(levelname)-7s] ' ':%(name)-10s: %(message)s')) logging.getLogger('').addHandler(console) log.info('*** aurqt v' + __version__) =3D=3D=3D pkgbuilder/pbds.py =3D=3D=3D class PBDS: logging.basicConfig(format=3D'%(asctime)-15s [%(levelname)-7s] ' ':%(name)-10s: %(message)s', filename=3Dos.path.join(confdir, 'pkgbuilder.log'), level=3Dlogging.DEBUG) log =3D logging.getLogger('pkgbuilder') log.info('*** PKGBUILDer v' + __version__) =3D=3D=3D aurqt/__init__.py =3D=3D=3D from .aqds import AQDS DS =3D AQDS() import pkgbuilder # =E2=86=90 also imports pkgbuilder.DS =3D pkgbuilder.pbd= s.PBDS() =3D=3D=3D bin/aurqt output =3D=3D=3D [INFO ] :aurqt : *** aurqt v0.1.0 [INFO ] :requests.packages.urllib3.connectionpool: Starting new HTTPS connection (1): aur.archlinux.org [WARNING] :pkgbuilder: tty-clock version is a date, ignored for downgrade. =3D=3D=3D {confdir}/aurqt.log =3D=3D=3D 2013-04-24 19:34:21,079 [INFO ] :aurqt : *** aurqt v0.1.0 2013-04-24 19:35:19,096 [WARNING] :requests.packages.urllib3.connectionpool: Starting new HTTPS connection (1): aur.archlinux.org 2013-04-24 19:35:21,004 [WARNING] :pkgbuilder: tty-clock version is a date, ignored for downgrade. -- Kwpolska | GPG KEY: 5EAAEA16 stop html mail | always bottom-post http://asciiribbon.org | http://caliburn.nl/topposting.html