Open Bug 944177 Opened 11 years ago Updated 2 years ago

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

Categories

(Testing :: Mozbase, defect)

x86_64
Linux
defect

Tracking

(Not tracked)

REOPENED

People

(Reporter: wlach, Unassigned)

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
Closed: 6 years ago
Resolution: --- → WONTFIX
I don't think we should close this one, afaik this gotcha is still there.
Status: RESOLVED → REOPENED
Resolution: WONTFIX → ---
Summary: mozlog's getLogger throws exception in what seems like a normal use case → [mozlog] mozlog's getLogger throws exception in what seems like a normal use case
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.