Which is the preferred way to collect and send logs to google cloud logging when logs are produced by several processes ?
Here is my CloudLoggingHandler based proposal, would you care to criticize it ?
import google
from multiprocessing import Process
from logging import getLogger
class Worker(Process):
def __init__(self):
super(Worker, self).__init__()
def __setup_logger(self):
handler = CloudLoggingHandler(google.cloud.logging.Client(), name='log-name')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
google.cloud.logging.handlers.setup_logging(handler)
def run(self):
self.__setup_logger()
for i in range(10):
logging.warning("i=%d", i)
if __name__ == "__main__":
for _ in range(2):
w = Worker()
w.start()
I read about queue based log handlers here, but CloudLoggingHandler uses a batch commit in an isolated thread, so queue based handlers would be overkill. Am I correct ?
Sources indicates that CloudLoggingHandler is thread safe, so it might be sufficient to have one instance of CloudLoggingHandler shared by all the processes. Would it work? if so isn't it to harsh ?
Edits below to answer @thomas-schultz.
I stuck to my proposal, mostly because I was prototyping, it worked "out of the box", and I did not check for performances issues. I'm reconsidering this choice.
Indeed, from what I understand the CloudLoggingHandler with BackgroundThreadTransport blocks the main thread until the log is sent to the logging endpoint. This would occur almost for each log line. Indeed, the batch is sent as soon as there is one log record (cf source).
In my dev environnement, when several processes log concurrently, it occurs that one processe waits up to 1s for the log to be sent. I guess that is mostly network cost, and it would shrink to "not so much" from within the google data centers.
I'm considering to define a StreamHandler which would push all the log record to a Queue. This queue would be read by a Process which would be in charge to sent the logs to the logging endpoint. This process might rely on CloudLoggingHandler to do that, if it is relevant.
Does this make sense ?