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


Groups > comp.lang.python > #44277

Re: improvements sought re. logging across modules

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 <kwpolska@gmail.com>
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 <D9OdncANCNCqk-XMnZ2dnUVZ8sOdnZ2d@brightview.co.uk>
References <D9OdncANCNCqk-XMnZ2dnUVZ8sOdnZ2d@brightview.co.uk>
From Chris “Kwpolska” Warrick <kwpolska@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1024.1366825433.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Wed, Apr 24, 2013 at 6:54 PM, The Night Tripper <jkn+gg@nicorp.co.uk> wrote:
> 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:
>
> == mylogger.py ==
>
> import logging
>
> class MyLogger:   #using python 2.4 ;-o
>     def __init__(self):
>         self.log = logging.getLogger(MY_APP_NAME)
>     def setupLogging(self):
>         self.log.setlevel(logging.DEBUG)
>         # ...
>
> # our singleton logging object
> mylogger = Mylogger()
> # EOF
>
> and then in my other modules A.py, B.py etc. I have something like:
>
> == A.py ==
>
> import mylogger
> gLog = mylogger.mylogger
>
> if __name__ == "__main__":
>     gLog.setupLogging()
>     gLog.info("Module A running as main")
>     main()
> #EOF
>
> == B.py ==
>
> import mylogger
> gLog = mylogger.mylogger
>
> if __name__ == "__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:

=== aurqt/aqds.py ===
class AQDS:
    logging.basicConfig(format='%(asctime)-15s [%(levelname)-7s] '
                        ':%(name)-10s: %(message)s',
                        filename=os.path.join(confdir, 'aurqt.log'),
                        level=logging.DEBUG)
    log = logging.getLogger('aurqt')
    console = 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__)

=== pkgbuilder/pbds.py ===
class PBDS:
    logging.basicConfig(format='%(asctime)-15s [%(levelname)-7s] '
                        ':%(name)-10s: %(message)s',
                        filename=os.path.join(confdir, 'pkgbuilder.log'),
                        level=logging.DEBUG)
    log = logging.getLogger('pkgbuilder')
    log.info('*** PKGBUILDer v' + __version__)

=== aurqt/__init__.py ===
from .aqds import AQDS
DS = AQDS()
import pkgbuilder # ← also imports pkgbuilder.DS = pkgbuilder.pbds.PBDS()

=== bin/aurqt output ===
[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.

=== {confdir}/aurqt.log ===
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 <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html

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


Thread

improvements sought re. logging across modules The Night Tripper <jkn+gg@nicorp.co.uk> - 2013-04-24 17:54 +0100
  Re: improvements sought re. logging across modules Fábio Santos <fabiosantosart@gmail.com> - 2013-04-24 18:29 +0100
  Re: improvements sought re. logging across modules Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-04-24 19:43 +0200
  Re: improvements sought re. logging across modules Dave Angel <davea@davea.name> - 2013-04-24 13:55 -0400

csiph-web