I have the following to send logging messages to stackdriver
:
logger.py
import google.cloud.logging
import os
import logging
import sys
logging.basicConfig(level=logging.INFO, format="[VREPORT_LOG] [%(asctime)s] [%(pathname)s:%(lineno)d] %(message)s",datefmt="%H:%M:%S", stream=sys.stdout)
if (os.environ.get("PYTHON_ENV") != "local"):
client = google.cloud.logging.Client()
client.setup_logging()
def use_logging_handler():
logging.info("Initialising logger...")
if __name__ == '__main__':
use_logging_handler()
server.py
from utilities.logger import use_logging_handler
use_logging_handler()
...
logging.info("test")
logging.info("test1")
This is what happens on the server terminal
[09:35:33] [server.py:2322] test
test
[09:35:33] [server.py:2323] test1
test1
This is what was logged to the cloud
I 2019-08-20T09:35:33.831234Z test1
I 2019-08-20T09:35:33.830988Z test
I also have to call use_logging_handler()
in every function if I want to log something which is very inconvenient. If I put use_logging_handler()
at the start of a file, it would log print()
statements as well which is very annoying. How can I log the file name and line number to the cloud and without having to call use_logging_handler()
in every function (something similar to Winston
in NodeJS
)?
You need to configure the logging handler
To write log entries , first create a Logger, passing the “log name” with which to associate the entries:
logger = client.logger(LOG_NAME)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.