@google-cloud/logging-bunyan
Advanced tools
Comparing version 3.2.2 to 3.3.0
@@ -20,3 +20,6 @@ /*! | ||
export { express }; | ||
import { Entry } from '@google-cloud/logging'; | ||
import * as types from './types/core'; | ||
import { ApiResponseCallback } from '@google-cloud/logging/build/src/log'; | ||
import { LogSeverityFunctions } from '@google-cloud/logging/build/src/utils/log-common'; | ||
/** | ||
@@ -125,3 +128,4 @@ * Key to use in the Bunyan payload to allow users to indicate a trace for the | ||
private defaultCallback?; | ||
stackdriverLog: types.StackdriverLog; | ||
cloudLog: LogSeverityFunctions; | ||
redirectToStdout: boolean; | ||
constructor(options?: types.Options); | ||
@@ -162,2 +166,14 @@ /** | ||
}>, callback: Function): void; | ||
/** | ||
* Creates a combined callback which calls the this.defaultCallback and the Writable.write supplied callback | ||
* @param callback The callback function provided by Writable | ||
* @returns Combined callback which executes both, this.defaultCallback and one supplied by Writable.write | ||
*/ | ||
generateCallback(callback: Function): ApiResponseCallback; | ||
/** | ||
* A helper function to make a write call | ||
* @param entries The entries to be written | ||
* @param callback The callback supplied by Writable.write | ||
*/ | ||
_writeCall(entries: Entry | Entry[], callback: Function): void; | ||
} |
@@ -147,2 +147,3 @@ "use strict"; | ||
constructor(options) { | ||
var _a; | ||
options = options || {}; | ||
@@ -154,10 +155,16 @@ super({ objectMode: true }); | ||
this.defaultCallback = options.defaultCallback; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this.stackdriverLog = new logging_1.Logging(options).log(this.logName, { | ||
removeCircular: true, | ||
// See: https://cloud.google.com/logging/quotas, a log size of | ||
// 250,000 has been chosen to keep us comfortably within the | ||
// 256,000 limit. | ||
maxEntrySize: options.maxEntrySize || 250000, | ||
}); | ||
this.redirectToStdout = (_a = options.redirectToStdout) !== null && _a !== void 0 ? _a : false; | ||
if (!this.redirectToStdout) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this.cloudLog = new logging_1.Logging(options).log(this.logName, { | ||
removeCircular: true, | ||
// See: https://cloud.google.com/logging/quotas, a log size of | ||
// 250,000 has been chosen to keep us comfortably within the | ||
// 256,000 limit. | ||
maxEntrySize: options.maxEntrySize || 250000, | ||
}); | ||
} | ||
else { | ||
this.cloudLog = new logging_1.Logging(options).logSync(this.logName); | ||
} | ||
// serviceContext.service is required by the Error Reporting | ||
@@ -176,3 +183,4 @@ // API. Without it, errors that are logged with level 'error' | ||
if (!this.serviceContext) { | ||
logging_1.detectServiceContext(this.stackdriverLog.logging.auth).then(serviceContext => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
logging_1.detectServiceContext(this.cloudLog.logging.auth).then(serviceContext => { | ||
this.serviceContext = serviceContext; | ||
@@ -253,3 +261,7 @@ }, () => { | ||
} | ||
return this.stackdriverLog.entry(entryMetadata, record); | ||
return ((this.redirectToStdout | ||
? this.cloudLog | ||
: this.cloudLog) | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
.entry(entryMetadata, record)); | ||
} | ||
@@ -299,5 +311,4 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
_write(record, encoding, callback) { | ||
var _a; | ||
const entry = this.formatEntry_(record); | ||
this.stackdriverLog.write(entry, (_a = this.defaultCallback) !== null && _a !== void 0 ? _a : callback); | ||
this._writeCall(entry, callback); | ||
} | ||
@@ -310,8 +321,40 @@ /** | ||
_writev(chunks, callback) { | ||
var _a; | ||
const entries = chunks.map((request) => { | ||
return this.formatEntry_(request.chunk); | ||
}); | ||
this.stackdriverLog.write(entries, (_a = this.defaultCallback) !== null && _a !== void 0 ? _a : callback); | ||
this._writeCall(entries, callback); | ||
} | ||
/** | ||
* Creates a combined callback which calls the this.defaultCallback and the Writable.write supplied callback | ||
* @param callback The callback function provided by Writable | ||
* @returns Combined callback which executes both, this.defaultCallback and one supplied by Writable.write | ||
*/ | ||
generateCallback(callback) { | ||
// Make sure that both callbacks are called in case if provided | ||
const newCallback = (err, apiResponse) => { | ||
if (callback) { | ||
callback(err, apiResponse); | ||
} | ||
if (this.defaultCallback) { | ||
this.defaultCallback(err, apiResponse); | ||
} | ||
}; | ||
return newCallback; | ||
} | ||
/** | ||
* A helper function to make a write call | ||
* @param entries The entries to be written | ||
* @param callback The callback supplied by Writable.write | ||
*/ | ||
_writeCall(entries, callback) { | ||
if (this.redirectToStdout) { | ||
this.cloudLog.write(entries); | ||
// The LogSync class does not supports callback. However if callback provided and | ||
// if this.defaultCallback exists, we should call it | ||
this.generateCallback(callback)(null, undefined); | ||
} | ||
else { | ||
this.cloudLog.write(entries, this.generateCallback(callback)); | ||
} | ||
} | ||
} | ||
@@ -318,0 +361,0 @@ exports.LoggingBunyan = LoggingBunyan; |
@@ -40,3 +40,4 @@ "use strict"; | ||
}); | ||
const auth = loggingBunyanApp.stackdriverLog.logging.auth; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const auth = loggingBunyanApp.cloudLog.logging.auth; | ||
const [env, projectId] = await Promise.all([ | ||
@@ -43,0 +44,0 @@ auth.getEnv(), |
@@ -82,7 +82,14 @@ /*! | ||
// An attempt will be made to truncate messages larger than maxEntrySize. | ||
// Please note that this parameter is ignored when redirectToStdout is set. | ||
maxEntrySize?: number; | ||
// A default global callback to be used for {@link LoggingBunyan} write calls | ||
// when callback is not supplied by caller in function parameters | ||
defaultCallback?: ApiResponseCallback; | ||
/** | ||
* Boolen flag that opts-in redirecting the output to STDOUT instead of ingesting logs to Cloud | ||
* Logging using Logging API. Defaults to {@code false}. Redirecting logs can be used in | ||
* Google Cloud environments with installed logging agent to delegate log ingestions to the | ||
* agent. Redirected logs are formatted as one line Json string following the structured logging guidelines. | ||
*/ | ||
redirectToStdout?: boolean; | ||
} | ||
@@ -89,0 +96,0 @@ |
@@ -7,2 +7,9 @@ # Changelog | ||
## [3.3.0](https://github.com/googleapis/nodejs-logging-bunyan/compare/v3.2.2...v3.3.0) (2022-03-21) | ||
### Features | ||
* Logging provider for Cloud Functions that outputs structured logs to process.stdout ([#605](https://github.com/googleapis/nodejs-logging-bunyan/issues/605)) ([f3ed3aa](https://github.com/googleapis/nodejs-logging-bunyan/commit/f3ed3aa973d0e2cd1f7311ef48a69cbd72df80f1)) | ||
### [3.2.2](https://github.com/googleapis/nodejs-logging-bunyan/compare/v3.2.1...v3.2.2) (2022-03-09) | ||
@@ -9,0 +16,0 @@ |
{ | ||
"name": "@google-cloud/logging-bunyan", | ||
"description": "Cloud Logging stream for Bunyan", | ||
"version": "3.2.2", | ||
"version": "3.3.0", | ||
"license": "Apache-2.0", | ||
@@ -6,0 +6,0 @@ "author": "Google Inc.", |
@@ -239,3 +239,26 @@ [//]: # "This README.md file is auto-generated, all changes to this file will be lost." | ||
### Alternative way to ingest logs in Google Cloud managed environments | ||
If you use this library with the Cloud Logging Agent, you can configure the handler to output logs to `process.stdout` using | ||
the [structured logging Json format](https://cloud.google.com/logging/docs/structured-logging#special-payload-fields). | ||
To do this, add `redirectToStdout: true` parameter to the `LoggingBunyan` constructor as in sample below. | ||
You can use this parameter when running applications in Google Cloud managed environments such as AppEngine, Cloud Run, | ||
Cloud Function or GKE. The logger agent installed on these environments can capture `process.stdout` and ingest it into Cloud Logging. | ||
The agent can parse structured logs printed to `process.stdout` and capture additional log metadata beside the log payload. | ||
It is recommended to set `redirectToStdout: true` in serverless environments like Cloud Functions since it could | ||
decrease logging record loss upon execution termination - since all logs are written to `process.stdout` those | ||
would be picked up by the Cloud Logging Agent running in Google Cloud managed environment. | ||
```js | ||
// Imports the Google Cloud client library for Bunyan | ||
const {LoggingBunyan} = require('@google-cloud/logging-bunyan'); | ||
// Creates a client | ||
const loggingBunyan = new LoggingBunyan({ | ||
projectId: 'your-project-id', | ||
keyFilename: '/path/to/key.json', | ||
redirectToStdout: true, | ||
}); | ||
``` | ||
## Samples | ||
@@ -242,0 +265,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89695
970
342