如何在日志查看器中查看托管 VM 的 JSON 日志?
How to view JSON logs of a managed VM in the Log Viewer?
我正在尝试让 Compute Engine VM 实例上的 JSON 格式日志显示在 Google 开发者控制台的日志查看器中。根据 this documentation 应该可以这样做:
Applications using App Engine Managed VMs should write custom log
files to the VM's log directory at /var/log/app_engine/custom_logs.
These files are automatically collected and made available in the Logs
Viewer.
Custom log files must have the suffix .log or .log.json. If the suffix
is .log.json, the logs must be in JSON format with one JSON object per
line. If the suffix is .log, log entries are treated as plain text.
这似乎对我不起作用:以 .log
结尾的日志在日志查看器中可见,但显示为纯文本。以 .log.json
结尾的日志根本不可见。
它也与另一篇最近的文章相矛盾,该文章指出 file names must end in .log and its contents are treated as plain text。
据我所知,Google 在日志查看器中使用 fluentd to index the log files。在 GitHub 存储库中,我找不到任何证据表明 .log.json
文件正在被索引。
有谁知道如何让它工作?或者文档是否已过时并且是否由于某种原因删除了此功能?
我目前正在托管 VM 上开发 NodeJS 应用程序 运行,我还试图让我的日志打印在 Google 开发人员控制台上。我按照文档中的描述在“/var/log/app_engine”目录中创建了我的日志文件。不幸的是,这似乎对我不起作用,即使对于“.log”文件也是如此。
你能描述一下你的日志是在哪里创建的吗?此外,您的托管 VM 是否配置为 "Managed by Google" 或 "Managed by User" ?谢谢!
这是为托管 VM 日志查看器生成 JSON 日志的一种方法:
所需的JSON格式
目标是为包含以下内容的每个日志行创建单行 JSON 对象:
{
"message": "Error occurred!.",
"severity": "ERROR",
"timestamp": {
"seconds": 1437712034000,
"nanos": 905
}
}
(信息来源于Google:https://code.google.com/p/googleappengine/issues/detail?id=11678#c5)
使用python-json-记录器
参见:https://github.com/madzak/python-json-logger
def get_timestamp_dict(when=None):
"""Converts a datetime.datetime to integer milliseconds since the epoch.
Requires special handling to preserve microseconds.
Args:
when:
A datetime.datetime instance. If None, the timestamp for 'now'
will be used.
Returns:
Integer time since the epoch in milliseconds. If the supplied 'when' is
None, the return value will be None.
"""
if when is None:
when = datetime.datetime.utcnow()
ms_since_epoch = float(time.mktime(when.utctimetuple()) * 1000.0)
return {
'seconds': int(ms_since_epoch),
'nanos': int(when.microsecond / 1000.0),
}
def setup_json_logger(suffix=''):
try:
from pythonjsonlogger import jsonlogger
class GoogleJsonFormatter(jsonlogger.JsonFormatter):
FORMAT_STRING = "{message}"
def add_fields(self, log_record, record, message_dict):
super(GoogleJsonFormatter, self).add_fields(log_record,
record,
message_dict)
log_record['severity'] = record.levelname
log_record['timestamp'] = get_timestamp_dict()
log_record['message'] = self.FORMAT_STRING.format(
message=record.message,
filename=record.filename,
)
formatter = GoogleJsonFormatter()
log_path = '/var/log/app_engine/custom_logs/worker'+suffix+'.log.json'
make_sure_path_exists(log_path)
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(formatter)
logging.getLogger().addHandler(file_handler)
except OSError:
logging.warn("Custom log path not found for production logging")
except ImportError:
logging.warn("JSON Formatting not available")
要使用,只需调用 setup_json_logger
- 您可能还想为您的日志更改 worker
的名称。
我正在尝试让 Compute Engine VM 实例上的 JSON 格式日志显示在 Google 开发者控制台的日志查看器中。根据 this documentation 应该可以这样做:
Applications using App Engine Managed VMs should write custom log files to the VM's log directory at /var/log/app_engine/custom_logs. These files are automatically collected and made available in the Logs Viewer.
Custom log files must have the suffix .log or .log.json. If the suffix is .log.json, the logs must be in JSON format with one JSON object per line. If the suffix is .log, log entries are treated as plain text.
这似乎对我不起作用:以 .log
结尾的日志在日志查看器中可见,但显示为纯文本。以 .log.json
结尾的日志根本不可见。
它也与另一篇最近的文章相矛盾,该文章指出 file names must end in .log and its contents are treated as plain text。
据我所知,Google 在日志查看器中使用 fluentd to index the log files。在 GitHub 存储库中,我找不到任何证据表明 .log.json
文件正在被索引。
有谁知道如何让它工作?或者文档是否已过时并且是否由于某种原因删除了此功能?
我目前正在托管 VM 上开发 NodeJS 应用程序 运行,我还试图让我的日志打印在 Google 开发人员控制台上。我按照文档中的描述在“/var/log/app_engine”目录中创建了我的日志文件。不幸的是,这似乎对我不起作用,即使对于“.log”文件也是如此。
你能描述一下你的日志是在哪里创建的吗?此外,您的托管 VM 是否配置为 "Managed by Google" 或 "Managed by User" ?谢谢!
这是为托管 VM 日志查看器生成 JSON 日志的一种方法:
所需的JSON格式
目标是为包含以下内容的每个日志行创建单行 JSON 对象:
{
"message": "Error occurred!.",
"severity": "ERROR",
"timestamp": {
"seconds": 1437712034000,
"nanos": 905
}
}
(信息来源于Google:https://code.google.com/p/googleappengine/issues/detail?id=11678#c5)
使用python-json-记录器
参见:https://github.com/madzak/python-json-logger
def get_timestamp_dict(when=None):
"""Converts a datetime.datetime to integer milliseconds since the epoch.
Requires special handling to preserve microseconds.
Args:
when:
A datetime.datetime instance. If None, the timestamp for 'now'
will be used.
Returns:
Integer time since the epoch in milliseconds. If the supplied 'when' is
None, the return value will be None.
"""
if when is None:
when = datetime.datetime.utcnow()
ms_since_epoch = float(time.mktime(when.utctimetuple()) * 1000.0)
return {
'seconds': int(ms_since_epoch),
'nanos': int(when.microsecond / 1000.0),
}
def setup_json_logger(suffix=''):
try:
from pythonjsonlogger import jsonlogger
class GoogleJsonFormatter(jsonlogger.JsonFormatter):
FORMAT_STRING = "{message}"
def add_fields(self, log_record, record, message_dict):
super(GoogleJsonFormatter, self).add_fields(log_record,
record,
message_dict)
log_record['severity'] = record.levelname
log_record['timestamp'] = get_timestamp_dict()
log_record['message'] = self.FORMAT_STRING.format(
message=record.message,
filename=record.filename,
)
formatter = GoogleJsonFormatter()
log_path = '/var/log/app_engine/custom_logs/worker'+suffix+'.log.json'
make_sure_path_exists(log_path)
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(formatter)
logging.getLogger().addHandler(file_handler)
except OSError:
logging.warn("Custom log path not found for production logging")
except ImportError:
logging.warn("JSON Formatting not available")
要使用,只需调用 setup_json_logger
- 您可能还想为您的日志更改 worker
的名称。