UNPKG

1.99 kBTypeScriptView Raw
1/*!
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import * as http from 'http';
17import { ServerRequest, CloudLoggingHttpRequest } from '../../utils/http-request';
18/**
19 * Generates an express middleware that installs a request-specific logger on
20 * the `request` object. It optionally can do HttpRequest timing that can be
21 * used for generating request logs. This can be used to integrate with logging
22 * libraries such as winston and bunyan.
23 *
24 * @param projectId Generated traceIds will be associated with this project.
25 * @param makeChildLogger A function that generates logger instances that will
26 * be installed onto `req` as `req.log`. The logger should include the trace in
27 * each log entry's metadata (associated with the LOGGING_TRACE_KEY property.
28 * @param emitRequestLog Optional. A function that will emit a parent request
29 * log. While some environments like GAE and GCF emit parent request logs
30 * automatically, other environments do not. When provided this function will be
31 * called with a populated `CloudLoggingHttpRequest` which can be emitted as
32 * request log.
33 */
34export declare function makeMiddleware<LoggerType>(projectId: string, makeChildLogger: (trace: string, span?: string, traceSampled?: boolean) => LoggerType, emitRequestLog?: (httpRequest: CloudLoggingHttpRequest, trace: string, span?: string, traceSampled?: boolean) => void): (req: ServerRequest, res: http.ServerResponse, next: Function) => void;