stackdriver-logging-winston-koa
Version:
Koa middleware for Winston transport to Cloud Logging
73 lines • 3.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.REQUEST_LOG_SUFFIX = void 0;
exports.makeMiddleware = makeMiddleware;
/*
* Adapted from:
* https://github.com/googleapis/nodejs-logging-winston/blob/main/src/middleware/express.ts
*/
const google_auth_library_1 = require("google-auth-library");
const logging_1 = require("@google-cloud/logging");
const logging_winston_1 = require("@google-cloud/logging-winston");
const make_middleware_1 = require("./common/make-middleware");
exports.REQUEST_LOG_SUFFIX = '_reqlog';
async function makeMiddleware(logger, optionsOrTransport, skipParentEntryForCloudRun) {
let transport;
// If no custom transports are provided, use default or instantiate one.
const cloudTransport = logger.transports.find(t => t instanceof logging_winston_1.LoggingWinston);
// If user provides a custom transport, always add it to the logger.
if (optionsOrTransport instanceof logging_winston_1.LoggingWinston) {
transport = optionsOrTransport;
logger.add(transport);
}
else if (cloudTransport && !optionsOrTransport) {
// Check if logger already contains a Cloud transport
transport = cloudTransport;
}
else {
const options = { logName: 'winston_log', ...optionsOrTransport };
transport = new logging_winston_1.LoggingWinston(options);
logger.add(transport);
}
const auth = (transport.common.redirectToStdout
? transport.common.cloudLog
: transport.common.cloudLog).logging.auth;
const [env, projectId] = await Promise.all([
auth.getEnv(),
auth.getProjectId(),
]);
// Unless we are running on Google App Engine or Cloud Functions, generate a
// parent request log entry that all the request specific logs ("app logs")
// will nest under. GAE and GCF generate the parent request logs
// automatically.
// Cloud Run also generates the parent request log automatically, but skipping
// the parent request entry has to be explicity enabled until the next major
// release in which we can change the default behavior.
let emitRequestLogEntry;
if (env !== google_auth_library_1.GCPEnv.APP_ENGINE &&
env !== google_auth_library_1.GCPEnv.CLOUD_FUNCTIONS &&
(env !== google_auth_library_1.GCPEnv.CLOUD_RUN || !skipParentEntryForCloudRun)) {
const requestLogName = logging_1.Log.formatName_(projectId, `${transport.common.logName}${exports.REQUEST_LOG_SUFFIX}`);
emitRequestLogEntry = (httpRequest, trace, span, sampled) => {
logger.info({
// The request logs must have a log name distinct from the app logs
// for log correlation to work.
logName: requestLogName,
[logging_winston_1.LOGGING_TRACE_KEY]: trace,
[logging_winston_1.LOGGING_SPAN_KEY]: span,
[logging_winston_1.LOGGING_SAMPLED_KEY]: sampled,
httpRequest,
message: httpRequest.requestUrl || 'http request',
});
};
}
return (0, make_middleware_1.makeMiddleware)(projectId, (trace, span, sampled) => makeChildLogger(logger, trace, span, sampled), emitRequestLogEntry);
}
function makeChildLogger(logger, trace, span, sampled) {
return logger.child({
[logging_winston_1.LOGGING_TRACE_KEY]: trace,
[logging_winston_1.LOGGING_SPAN_KEY]: span,
[logging_winston_1.LOGGING_SAMPLED_KEY]: sampled,
});
}
//# sourceMappingURL=koa.js.map