[英]Python - not able to push file line no and file path to Google Cloud StackDriver
I have the following to send logging messages to stackdriver
: 我有以下发送日志消息到
stackdriver
:
logger.py 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 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. 如果我想记录一些非常不便的内容,我还必须在每个函数中调用
use_logging_handler()
。 If I put use_logging_handler()
at the start of a file, it would log print()
statements as well which is very annoying. 如果我将
use_logging_handler()
放在文件的开头,它也会记录print()
语句,这很烦人。 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
)? 我怎么能日志的文件名和行号为云而无需调用
use_logging_handler()
在每一个功能(类似的东西Winston
在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,并传递与条目相关联的“日志名称”:
logger = client.logger(LOG_NAME) logger = client.logger(LOG_NAME)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.