Python module “logging” double output

Python module “logging” double output



I want to use the logging module, but I'm having some trouble because it is outputting twice. I've read a lot of posts with people having the same issue and log.propagate = False or `log.handlers.pop()´ fixed it for them. This doesn't work for me.


logging


log.propagate = False



I have a file called logger.py that looks like this:


import logging

def __init__():
log = logging.getLogger("output")
filelog = logging.FileHandler("output.log")
formatlog = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
filelog.setFormatter(formatlog)
log.addHandler(filelog)
log.setLevel(logging.INFO)

return log



So that I from multiple files can write:


import logger
log = logger.__init__()



But this is giving me issues. So I've seen several solutions, but I don't know how to incorporate it in multiple scripts without defining the logger in all of them.





"it is outputting twice": to where and where? To output.log and stderr? In any case yes it looks like you should be setting log.propagate = False in your function. Or (less likely) does each message appear twice in output.log?
– BrianO
Sep 2 at 21:56



output.log


log.propagate = False


output.log





It outputs the same message twice to output.log, but I've written an answer to it: stackoverflow.com/a/52141311/9875740
– HelloThereToad
Sep 2 at 21:58





PS log.propagate = False would guard against every message also being written by the root logger, if the root has a handler. If this is the only configuration you do, that'll never happen so you actually don't have to touch propagate. But if you call logging.log(...), logging.debug(), ... or logging.critical(), they will "helpfully" create a root handler if there aren't any.
– BrianO
Sep 2 at 22:07


log.propagate = False


propagate




2 Answers
2



multiple scripts lead to multiple processes. so you unluckily create multiple objects returned from the logger.__init__() function.


logger.__init__()



usually, you have one script which creates the logger and the different processes,
but as i understand, you like to have multiple scripts logging to the same destination.



if you like to have multiple processes to log to the same destination, i recommend using inter process communication as "named pipes" or otherwise using a UDP/TCP port for logging.



There are also queue modules available in python to send a (atomic) logging entry to be logged in one part (compared to append to a file by multiple processes - which may lead from 111111n and 22222n to 11212121221nn in the file)



otherwise think about named pipes...



Code snippet for logging server
Note: i just assumed to log everything as error...


import socket
import logging

class mylogger():
def __init__(self, port=5005):
log = logging.getLogger("output")
filelog = logging.FileHandler("output.log")
formatlog = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
filelog.setFormatter(formatlog)
log.addHandler(filelog)
log.setLevel(logging.INFO)
self.log = log
UDP_IP = "127.0.0.1" # localhost
self.port = port
self.UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
self.UDPClientSocket.bind((UDP_IP, self.port))
def pollReceive(self):
data, addr = self.UDPClientSocket.recvfrom(1024) # buffer size is 1024 bytes
print("received message:", data)
self.log.error( data )
pass

log = mylogger()

while True:
log.pollReceive()





I could be wrong, but I don't think OP means multiple scripts running concurrently, just that he'd like to use this as a library routine in various scripts.
– BrianO
Sep 2 at 21:45





In certain cases yes.
– HelloThereToad
Sep 2 at 21:53





Ah, then each time it's called it adds another handler to the logger :) In general you want to configure logging once, only. Shameless self-promo: github.com/Twangist/prelogging, a sensible API for setting up logging.
– BrianO
Sep 2 at 21:59




I found a solution which was really simple. All I needed to do was to add an if statement checking if the handlers already existed. So my logger.py file now looks like this:


if


logger.py


import logging

def __init__():
log = logging.getLogger("output")

if not log.handlers:
filelog = logging.FileHandler("output.log")
formatlog = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
filelog.setFormatter(formatlog)
log.addHandler(filelog)
log.setLevel(logging.INFO)

return log



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?