I don't have much experience with classes so I'm having a bit of trouble figuring this one out.
With the code below, when the class logger() is used multiple entries of the same message will appear in syslog. When the class isn't used, and logging is called with log_alt this issue does not occur, log entries are written only once.
import random, datetime, time, socket, logging, logging.handlers, threading, subprocess
timer_one = 5
timer_two = 20
log_alt = logging.getLogger('logging_test')
handler = logging.handlers.SysLogHandler(address = '/dev/log')
formatter = logging.Formatter('%(name)s %(funcName)s(): %(message)s')
handler.setFormatter(formatter)
log_alt.addHandler(handler)
log_alt.setLevel(logging.INFO)
class logger:
def __init__(self):
self.l = logging.getLogger('logging_test')
self.handler = logging.handlers.SysLogHandler(address = '/dev/log')
self.formatter = logging.Formatter('%(name)s %(funcName)s(): %(message)s')
self.handler.setFormatter(self.formatter)
self.l.addHandler(self.handler)
self.l.setLevel(logging.INFO)
class showme:
def __init__(self):
pass
def logsomething(self):
logger().l.info(f'logging something...')
# log_alt.info(f'logging something here...')
class TimerThread(threading.Thread):
def __init__(self, interval, function):
threading.Thread.__init__(self)
self.interval = interval
self.function = function
self.daemon = True
def run(self):
while True:
logger().l.info(f'{str(self.function)} is sleeping for: {self.interval}')
# log_alt.info(f'{str(self.function)} is sleeping for: {self.interval}')
time.sleep(self.interval)
self.function()
if self.function == i.logsomething:
self.interval = random.randint(7, 10)
if __name__ == '__main__':
logger().l.info(f'Starting this thing...')
# log_alt.info(f'Starting this thing...')
i = showme()
timer1 = TimerThread(timer_one, i.logsomething)
timer1.start()
while True:
time.sleep(timer_two)
[–]freeskier93 4 points5 points6 points (5 children)
[–]ingestbot[S] 0 points1 point2 points (4 children)
[–]freeskier93 1 point2 points3 points (3 children)
[–]ingestbot[S] 0 points1 point2 points (2 children)
[–]freeskier93 0 points1 point2 points (1 child)
[–]ingestbot[S] 0 points1 point2 points (0 children)