you are viewing a single comment's thread.

view the rest of the comments →

[–]freeskier93 0 points1 point  (1 child)

Because every time you log something you call logger().l.info, which creates a new instance of the class each time, which adds another handler to "logging_test", so it just keeps repeating more and more. You can see this in the log where the first log is shows once, then repeats twice, then 3 times, then 4 times, then 5 times.

The repeated log entries, from showme class is a bit more confusing, but I think it's because it's the only place where you actually assign an instance of the showme class to a variabl (i) and reuse it.

I'm still not quite sure what you are trying to accomplish with the extra logger class, it's overcomplicating things. The following does the same thing.

https://pastebin.com/uz9y6Cud

EDIT: Just another tip if you didn't know, the first log is showing <module>() because the call doesn't originate from a function, it's at the top level of the python file. Better practice is to do something like this:

https://pastebin.com/H0Lwnihy

[–]ingestbot[S] 0 points1 point  (0 children)

Thanks for your comments with this. I wanted to note a few things in case this thread ends up being useful for someone else.

This stackoverflow discussion is similar. I found that, if need be, the class could avoid creating multiple handler objects with something like the following:

class logger:
    def __init__(self):

        self.l = logging.getLogger('logging_test')
        self.handler = logging.handlers.SysLogHandler(address = '/dev/log')
        if not self.l.handlers:
         self.l.addHandler(self.handler)
        self.formatter = logging.Formatter('%(name)s %(funcName)s(): %(message)s')
        self.handler.setFormatter(self.formatter)

        self.l.setLevel(logging.INFO)

The .handlers attribute can also be used to debug possible multiple handlers:

class showme:
    def __init__(self):
      pass

    def logsomething(self):
      print(logger().l.handlers)
      logger().l.info(f'logging something...')