简体   繁体   English

Python-无法将文件行号和文件路径推送到Google Cloud StackDriver

[英]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()在每一个功能(类似的东西WinstonNodeJS )?

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.

相关问题 Google Cloud Stackdriver 忽略 Python 日志中的格式 - Google Cloud Stackdriver ignores formatting in Python logs 无法将Celery任务日志推送到Google Cloud(堆栈驱动程序) - Unable to push Celery task logs to Google Cloud (stackdriver) 在谷歌云上部署 python 文件 - Deploying python file on google cloud 使用 Python 从 Google Cloud Stackdriver API 检索日志名称列表 - Retrieve list of log names from Google Cloud Stackdriver API with Python 将 python 中使用的库的日志发送到谷歌云堆栈驱动程序日志记录 - Send logs of libraries used in python to google cloud stackdriver logging python 找不到证书文件路径 - python not able to find the certificate file path Google Cloud:自定义stackdriver指标 - Google Cloud: custom stackdriver metrics 是否可以通过JSON密钥文件本身而非文件路径进行Google Cloud Python身份验证? - Is there a way for Google Cloud Python Authentication via the JSON keyfile itself and not the file path? 无法从App Engine中的Google云端存储中读取文件内容 - Not able to read file content from google cloud storage in app engine 如何在Google Cloud Shell上编辑python文件 - How to edit python file on Google Cloud Shell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM