对于 Stackdriver 错误报告:
您可以使用类似于建议用于 Google Compute Engine 的代码:https ://cloud.google.com/error-reporting/docs/setup/compute-engine#send_exception_data
以下是在 App Engine 柔性环境中使用 Express 和 Winston 对我有用的方法:
var winston = require('winston');
winston.add(winston.transports.File, { filename: '/var/log/app_engine/custom_logs/my.errors.json' });
var report = function (err, req) {
var payload = {
serviceContext: {
service: 'my service',
},
message: err.stack,
context: {
httpRequest: {
url: req.originalUrl,
method: req.method,
referrer: req.header('Referer'),
userAgent: req.header('User-Agent'),
remoteIp: req.ip,
responseStatusCode: 500,
}
}
};
winston.error (payload);
};
// Handle errors
app.use(function (err, req, res, next) {
report(err, req);
res.status(500).send(err.response || 'Something broke!');
});
对于 Stackdriver 日志记录:
实际上,request_log
App Engine flex 上仅包含 HTTP 请求日志条目。查看stdout
日志以查看应用程序的输出。