Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44271 > unrolled thread
| Started by | The Night Tripper <jkn+gg@nicorp.co.uk> |
|---|---|
| First post | 2013-04-24 17:54 +0100 |
| Last post | 2013-04-24 13:55 -0400 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | The Night Tripper <jkn+gg@nicorp.co.uk> |
|---|---|
| Date | 2013-04-24 17:54 +0100 |
| Subject | improvements sought re. logging across modules |
| Message-ID | <D9OdncANCNCqk-XMnZ2dnUVZ8sOdnZ2d@brightview.co.uk> |
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
[toc] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-04-24 18:29 +0100 |
| Message-ID | <mailman.1023.1366824584.3114.python-list@python.org> |
| In reply to | #44271 |
[Multipart message — attachments visible in raw view] — view raw
Maybe import mylogger.mylogger as gLog? I don't know what you mean by
"missing a trick". Your example seems pretty pythonic to me, except for the
fact that you use a singleton where you could have a couple of functions
and use the module as the namespace.
On 24 Apr 2013 17:58, "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
>
[toc] | [prev] | [next] | [standalone]
| From | Chris “Kwpolska” Warrick <kwpolska@gmail.com> |
|---|---|
| Date | 2013-04-24 19:43 +0200 |
| Message-ID | <mailman.1024.1366825433.3114.python-list@python.org> |
| In reply to | #44271 |
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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-04-24 13:55 -0400 |
| Message-ID | <mailman.1027.1366826130.3114.python-list@python.org> |
| In reply to | #44271 |
On 04/24/2013 12:54 PM, The Night Tripper 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 > Right there you have a potential problem. Unless you make those imports conditional, you have an import loop, which can be a minor problem, or a big one. Whenever you find loops in the import call tree, please break them. The best way is to move the interdependencies into yet another module, and let both A and B import that one. -- DaveA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web