1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | exports.getNodejsLibraryVersion = exports.LoggingCommon = exports.getCurrentTraceFromAgent = exports.NODEJS_WINSTON_DEFAULT_LIBRARY_VERSION = exports.LOGGING_SAMPLED_KEY = exports.LOGGING_SPAN_KEY = exports.LOGGING_TRACE_KEY = void 0;
|
17 | const util = require("util");
|
18 | const logging_1 = require("@google-cloud/logging");
|
19 | const instrumentation_1 = require("@google-cloud/logging/build/src/utils/instrumentation");
|
20 | const mapValues = require("lodash.mapvalues");
|
21 |
|
22 |
|
23 | const NPM_LEVEL_NAME_TO_CODE = {
|
24 | error: 3,
|
25 | warn: 4,
|
26 | info: 6,
|
27 | http: 6,
|
28 | verbose: 7,
|
29 | debug: 7,
|
30 | silly: 7,
|
31 | };
|
32 |
|
33 | const CLOUD_LOGGING_LEVEL_CODE_TO_NAME = {
|
34 | 0: 'emergency',
|
35 | 1: 'alert',
|
36 | 2: 'critical',
|
37 | 3: 'error',
|
38 | 4: 'warning',
|
39 | 5: 'notice',
|
40 | 6: 'info',
|
41 | 7: 'debug',
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 | exports.LOGGING_TRACE_KEY = 'logging.googleapis.com/trace';
|
47 |
|
48 |
|
49 |
|
50 | exports.LOGGING_SPAN_KEY = 'logging.googleapis.com/spanId';
|
51 |
|
52 |
|
53 |
|
54 | exports.LOGGING_SAMPLED_KEY = 'logging.googleapis.com/trace_sampled';
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | exports.NODEJS_WINSTON_DEFAULT_LIBRARY_VERSION = '6.0.0';
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function getCurrentTraceFromAgent() {
|
67 |
|
68 | const agent = global._google_trace_agent;
|
69 | if (!agent || !agent.getCurrentContextId || !agent.getWriterProjectId) {
|
70 | return null;
|
71 | }
|
72 | const traceId = agent.getCurrentContextId();
|
73 | if (!traceId) {
|
74 | return null;
|
75 | }
|
76 | const traceProjectId = agent.getWriterProjectId();
|
77 | if (!traceProjectId) {
|
78 | return null;
|
79 | }
|
80 | return `projects/${traceProjectId}/traces/${traceId}`;
|
81 | }
|
82 | exports.getCurrentTraceFromAgent = getCurrentTraceFromAgent;
|
83 | class LoggingCommon {
|
84 | constructor(options) {
|
85 | var _a, _b;
|
86 | options = Object.assign({
|
87 | scopes: ['https://www.googleapis.com/auth/logging.write'],
|
88 | }, options);
|
89 | this.logName = options.logName || 'winston_log';
|
90 | this.inspectMetadata = options.inspectMetadata === true;
|
91 | this.levels = options.levels || NPM_LEVEL_NAME_TO_CODE;
|
92 | this.redirectToStdout = (_a = options.redirectToStdout) !== null && _a !== void 0 ? _a : false;
|
93 | if (!this.redirectToStdout) {
|
94 | this.cloudLog = new logging_1.Logging(options).log(this.logName, {
|
95 | removeCircular: true,
|
96 |
|
97 |
|
98 |
|
99 | maxEntrySize: options.maxEntrySize || 250000,
|
100 | });
|
101 | }
|
102 | else {
|
103 | const logSyncOptions = {
|
104 | useMessageField: (_b = options.useMessageField) !== null && _b !== void 0 ? _b : true,
|
105 | };
|
106 | this.cloudLog = new logging_1.Logging(options).logSync(this.logName, undefined, logSyncOptions);
|
107 | }
|
108 | this.resource = options.resource;
|
109 | this.serviceContext = options.serviceContext;
|
110 | this.prefix = options.prefix;
|
111 | this.labels = options.labels;
|
112 | this.defaultCallback = options.defaultCallback;
|
113 | }
|
114 | log(level, message, metadata, callback) {
|
115 | metadata = metadata || {};
|
116 |
|
117 | let instrumentationEntry;
|
118 | if (!(0, instrumentation_1.setInstrumentationStatus)(true)) {
|
119 | instrumentationEntry = (0, instrumentation_1.createDiagnosticEntry)('nodejs-winston', getNodejsLibraryVersion());
|
120 |
|
121 | instrumentationEntry.metadata.resource = this.resource;
|
122 | instrumentationEntry.metadata.logName = metadata.logName;
|
123 | instrumentationEntry.metadata.timestamp = metadata.timestamp;
|
124 | }
|
125 | message = message || '';
|
126 | const hasMetadata = Object.keys(metadata).length;
|
127 | if (this.levels[level] === undefined) {
|
128 | throw new Error('Unknown log level: ' + level);
|
129 | }
|
130 | const levelCode = this.levels[level];
|
131 | const cloudLevel = CLOUD_LOGGING_LEVEL_CODE_TO_NAME[levelCode];
|
132 | const data = {};
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | if (metadata.stack) {
|
146 | message += (message ? ' ' : '') + metadata.stack;
|
147 | data.serviceContext = this.serviceContext;
|
148 | }
|
149 | data.message = this.prefix ? `[${this.prefix}] ` : '';
|
150 | data.message += message;
|
151 | const entryMetadata = {
|
152 | resource: this.resource,
|
153 | };
|
154 |
|
155 |
|
156 | if (metadata.logName) {
|
157 | entryMetadata.logName = metadata.logName;
|
158 | }
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | if (metadata.httpRequest) {
|
166 | entryMetadata.httpRequest = metadata.httpRequest;
|
167 | }
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | if (metadata.timestamp instanceof Date) {
|
173 | entryMetadata.timestamp = metadata.timestamp;
|
174 | }
|
175 |
|
176 |
|
177 |
|
178 | if (this.labels || metadata.labels) {
|
179 | entryMetadata.labels = !this.labels
|
180 | ? metadata.labels
|
181 | : Object.assign({}, this.labels, metadata.labels);
|
182 | }
|
183 | const trace = metadata[exports.LOGGING_TRACE_KEY] || getCurrentTraceFromAgent();
|
184 | if (trace) {
|
185 | entryMetadata.trace = trace;
|
186 | }
|
187 | const spanId = metadata[exports.LOGGING_SPAN_KEY];
|
188 | if (spanId) {
|
189 | entryMetadata.spanId = spanId;
|
190 | }
|
191 | if (exports.LOGGING_SAMPLED_KEY in metadata) {
|
192 | entryMetadata.traceSampled = metadata[exports.LOGGING_SAMPLED_KEY] === '1';
|
193 | }
|
194 |
|
195 |
|
196 |
|
197 | data.metadata = this.inspectMetadata
|
198 | ? mapValues(metadata, util.inspect)
|
199 | : metadata;
|
200 | if (hasMetadata) {
|
201 |
|
202 | delete data.metadata[exports.LOGGING_TRACE_KEY];
|
203 | delete data.metadata[exports.LOGGING_SPAN_KEY];
|
204 | delete data.metadata[exports.LOGGING_SAMPLED_KEY];
|
205 | delete data.metadata.httpRequest;
|
206 | delete data.metadata.labels;
|
207 | delete data.metadata.timestamp;
|
208 | delete data.metadata.logName;
|
209 | }
|
210 | const entries = [];
|
211 | entries.push(this.entry(entryMetadata, data));
|
212 |
|
213 | if (instrumentationEntry) {
|
214 |
|
215 | instrumentationEntry = this.entry(instrumentationEntry.metadata, instrumentationEntry.data);
|
216 | if (levelCode !== NPM_LEVEL_NAME_TO_CODE.info) {
|
217 |
|
218 | this.cloudLog[CLOUD_LOGGING_LEVEL_CODE_TO_NAME[NPM_LEVEL_NAME_TO_CODE.info]]([instrumentationEntry], this.defaultCallback);
|
219 | }
|
220 | else
|
221 | entries.push(instrumentationEntry);
|
222 | }
|
223 |
|
224 | const newCallback = (err, apiResponse) => {
|
225 | let callbackError;
|
226 | if (callback) {
|
227 | try {
|
228 | callback(err, apiResponse);
|
229 | }
|
230 | catch (error) {
|
231 | callbackError = error;
|
232 | }
|
233 | }
|
234 | if (this.defaultCallback) {
|
235 | this.defaultCallback(err, apiResponse);
|
236 | }
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | if ((!this.defaultCallback || err === null) && callbackError) {
|
242 | throw callbackError;
|
243 | }
|
244 | };
|
245 | this.cloudLog[cloudLevel](entries, newCallback);
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | if (this.redirectToStdout) {
|
251 | newCallback(null, undefined);
|
252 | }
|
253 | }
|
254 | entry(metadata, data) {
|
255 | if (this.redirectToStdout) {
|
256 | return this.cloudLog.entry(metadata, data);
|
257 | }
|
258 | return this.cloudLog.entry(metadata, data);
|
259 | }
|
260 | }
|
261 | exports.LoggingCommon = LoggingCommon;
|
262 |
|
263 |
|
264 | LoggingCommon.LOGGING_TRACE_KEY = exports.LOGGING_TRACE_KEY;
|
265 |
|
266 |
|
267 | LoggingCommon.LOGGING_SPAN_KEY = exports.LOGGING_SPAN_KEY;
|
268 | function getNodejsLibraryVersion() {
|
269 | return exports.NODEJS_WINSTON_DEFAULT_LIBRARY_VERSION;
|
270 | }
|
271 | exports.getNodejsLibraryVersion = getNodejsLibraryVersion;
|
272 |
|
\ | No newline at end of file |