UNPKG

7.71 kBJavaScriptView Raw
1"use strict";
2// Copyright 2017 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.
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.LOGGING_SAMPLED_KEY = exports.LOGGING_SPAN_KEY = exports.LOGGING_TRACE_KEY = exports.LoggingWinston = exports.getCurrentTraceFromAgent = exports.getDefaultMetadataForTracing = exports.express = void 0;
17const TransportStream = require("winston-transport");
18const common_1 = require("./common");
19Object.defineProperty(exports, "getCurrentTraceFromAgent", { enumerable: true, get: function () { return common_1.getCurrentTraceFromAgent; } });
20const express = require("./middleware/express");
21exports.express = express;
22const default_metadata_1 = require("./default-metadata");
23Object.defineProperty(exports, "getDefaultMetadataForTracing", { enumerable: true, get: function () { return default_metadata_1.getDefaultMetadataForTracing; } });
24const LEVEL = Symbol.for('level');
25/**
26 * This module provides support for streaming your winston logs to
27 * [Cloud Logging](https://cloud.google.com/logging).
28 *
29 * @class
30 *
31 * @param {object} [options]
32 * @param {object} [options.level] The default log level. Winston will filter
33 * messages with a severity lower than this.
34 * @param {object} [options.levels] Custom logging levels as supported by
35 * winston. This list is used to translate your log level to the Cloud
36 * Logging level. Each property should have an integer value between 0 (most
37 * severe) and 7 (least severe). If you are passing a list of levels to your
38 * winston logger, you should provide the same list here.
39 * @param {boolean} [options.inspectMetadata=false] Serialize winston-provided log
40 * metadata using `util.inspect`.
41 * @param {string} [options.logName=winston_log] The name of the log that will receive
42 * messages written to this transport.
43 * @param {object} [options.resource] The monitored resource that the transport
44 * corresponds to. On Google Cloud Platform, this is detected automatically,
45 * but you may optionally specify a specific monitored resource. For more
46 * information see the
47 * [official documentation]{@link
48 * https://cloud.google.com/logging/docs/api/reference/rest/v2/MonitoredResource}.
49 * @param {object} [options.serviceContext] For logged errors, we provide this
50 * as the service context. For more information see
51 * [this guide]{@link
52 * https://cloud.google.com/error-reporting/docs/formatting-error-messages} and
53 * the [official documentation]{@link
54 * https://cloud.google.com/error-reporting/reference/rest/v1beta1/ServiceContext}.
55 * @param {string} [options.serviceContext.service] An identifier of the
56 * service, such as the name of the executable, job, or Google App Engine
57 * service name.
58 * @param {string} [options.serviceContext.version] Represents the version of
59 * the service.
60 * @param {string} [options.projectId] The project ID from the Google Cloud
61 * Console, e.g. 'grape-spaceship-123'. We will also check the environment
62 * variable `GCLOUD_PROJECT` for your project ID. If your app is running in
63 * an environment which supports {@link
64 * https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application
65 * Application Default Credentials}, your project ID will be detected
66 * automatically.
67 * @param {string} [options.keyFilename] Full path to the a .json, .pem, or .p12
68 * key downloaded from the Google Cloud Console. If you provide a path
69 * to a JSON file, the `projectId` option above is not necessary. NOTE: .pem
70 * and .p12 require you to specify the `email` option as well.
71 * @param {string} [options.email] Account email address. Required when using a
72 * .pem or .p12 keyFilename.
73 * @param {object} [options.credentials] Credentials object.
74 * @param {string} [options.credentials.client_email]
75 * @param {string} [options.credentials.private_key]
76 * @param {boolean} [options.autoRetry=true] Automatically retry requests if the
77 * response is related to rate limits or certain intermittent server errors.
78 * We will exponentially backoff subsequent requests by default.
79 * @param {number} [options.maxRetries=3] Maximum number of automatic retries
80 * attempted before returning the error.
81 * @param {constructor} [options.promise] Custom promise module to use instead
82 * of native Promises.
83 *
84 * @example <caption>Import the client library</caption>
85 * const {LoggingWinston} = require('@google-cloud/logging-winston');
86 *
87 * @example <caption>Create a client that uses <a
88 * href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application
89 * Default Credentials (ADC)</a>:</caption> const loggingWinston = new
90 * LoggingWinston();
91 *
92 * @example <caption>Create a client with <a
93 * href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit
94 * credentials</a>:</caption> const loggingWinston = new LoggingWinston({
95 * projectId: 'your-project-id',
96 * keyFilename: '/path/to/keyfile.json'
97 * });
98 *
99 * @example <caption>include:samples/quickstart.js</caption>
100 * region_tag:logging_winston_quickstart
101 * Full quickstart example:
102 */
103class LoggingWinston extends TransportStream {
104 constructor(options) {
105 options = options || {};
106 super({
107 level: options.level,
108 });
109 this.common = new common_1.LoggingCommon(options);
110 }
111 // eslint-disable-next-line
112 log(info, callback) {
113 // eslint-disable-next-line @typescript-eslint/no-unused-vars
114 const { message, level, splat, stack, ...metadata } = info;
115 // If the whole message is an error we have to manually copy the stack into
116 // metadata. Errors dont have enumerable properties so they don't
117 // destructure.
118 if (stack)
119 metadata.stack = stack;
120 this.common.log(info[LEVEL] || level, message, metadata || {}, callback);
121 }
122}
123exports.LoggingWinston = LoggingWinston;
124LoggingWinston.LOGGING_TRACE_KEY = common_1.LOGGING_TRACE_KEY;
125LoggingWinston.LOGGING_SPAN_KEY = common_1.LOGGING_SPAN_KEY;
126LoggingWinston.LOGGING_SAMPLED_KEY = common_1.LOGGING_SAMPLED_KEY;
127// LOGGING_TRACE_KEY is Cloud Logging specific and has the format:
128// logging.googleapis.com/trace
129// For more information, see: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.trace
130exports.LOGGING_TRACE_KEY = common_1.LOGGING_TRACE_KEY;
131// LOGGING_SPAN_KEY is Cloud Logging specific and has the format:
132// logging.googleapis.com/spanId
133// For more information, see: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.span_id
134exports.LOGGING_SPAN_KEY = common_1.LOGGING_SPAN_KEY;
135// LOGGING_SAMPLED_KEY is Cloud Logging specific and has the format:
136// logging.googleapis.com/trace_sampled
137// The value of this field must be either true or false. For more information,
138// see traceSampled on the LogEntry page: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.trace_sampled
139exports.LOGGING_SAMPLED_KEY = common_1.LOGGING_SAMPLED_KEY;
140//# sourceMappingURL=index.js.map
\No newline at end of file