mozlog's getLogger throws exception in what seems like a normal use case

REOPENED
Unassigned

Status

Testing
Mozbase
REOPENED
5 years ago
6 months ago

People

(Reporter: wlach, Unassigned)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

The following code throws an exception:

import mozlog

class Foo(object):
    def __init__(self):
        handler = mozlog.StreamHandler()
        handler.setFormatter(mozlog.MozFormatter(include_timestamp=True))
        self.logger = mozlog.getLogger("Foo", handler)

    def blah(self):
        self.logger.log(mozlog.INFO, "blah")

for i in range(1,10):
    print "Iteration %s" % i
    f = Foo()
    f.blah() # throws an exception the second time

This is because getLogger is designed to throw an exception if you call getLogger() with a custom handler, when that logger already exists. This was a deliberate decision, to avoid user error: https://bugzilla.mozilla.org/show_bug.cgi?id=891488#c7

:jgraham suggested a neat workaround -- if we create the logger outside of __init__, only one copy of the logger will get created, so we sidestep the problem:

class Foo(object):
    handler = mozlog.StreamHandler()
    handler.setFormatter(mozlog.MozFormatter(include_timestamp=True))
    logger = mozlog.getLogger("Foo", handler)

Of course, knowing this incantation is somewhat magical. Should we modify getLogger itself? Create a variant of LoggingMixin that provides timestamps (the original user story behind this?). Other ideas?
Summary: mozlog's getLogger throws exception in normal cases → mozlog's getLogger throws exception in what seems like a normal use case
Mass closing bugs with no activity in 2+ years. If this bug is important to you, please re-open.
Status: NEW → RESOLVED
Last Resolved: 6 months ago
Resolution: --- → WONTFIX
I don't think we should close this one, afaik this gotcha is still there.
Status: RESOLVED → REOPENED
Resolution: WONTFIX → ---
You need to log in before you can comment on or make changes to this bug.