@pokutuna/requestlog-cloudfunctions
v0.0.3
Published
a middleware to write request logs for Google Cloud Functions
Downloads
1
Readme
@pokutuna/requestlog-cloudfunctions
A middleware to write an httpRequest
log with trace
field on Cloud Functions.
This makes it possible to group your application logs in an HTTP request.
Usage
import express = require("express");
import { logHttpRequest } from "@pokutuna/requestlog-cloudfunctions";
const projectId = "<YOUR_PROJECT_ID>";
const logId = "request_log";
const app = express();
app.use(logHttpRequest({ projectId, logId }));
...
Options
projectId
- default:
process.env.GCLOUD_PROJECT
- You must give
projectId
on nodejs10 runtime which doesn't provideGCLOUD_PROJECT
environment variable.
- default:
logId
- default:
"request_log"
- Logs are written to logName
projects/{projectId}/logs/{logId}
on Stackdriver Logging
- default:
Writing logs
- (Recommend) use
@google-cloud/logging-winston
or@google-cloud/logging-bunyan
- see Setting Up Stackdriver Logging for Node.js | Stackdriver Logging | Google Cloud
- These libraries provide
req.log(...)
method considering the trace field.
- Or write the
trace
field by yourself.- The
X-Cloud-Trace-Context
request header contains${traceId}/${spanId}
- Write
trace
field withprojects/${projectId}/traces/${traceId}
in metadata of a entry using the client library@google-cloud/logging
.
const { Logging } = require("@google-cloud/logging"); const projectId = "<YOUR_PROJECT_ID>"; const logging = new Logging({ projectId }); const log = logging.log("application_log"); exports.app = (req, res) => { const [traceId] = req.header("X-Cloud-Trace-Context").split("/"); const trace = `projects/${projectId}/traces/${traceId}`; // log.entry(metadata, data) const entry = log.entry( { trace }, { message: "this is a message", obj: { key: "value" } } ); await log.write(entry); };
- The