From 6fcda1e641546fcf948b2fe1a0eb1ebc1f83e11c Mon Sep 17 00:00:00 2001 From: losalex <90795544+losalex@users.noreply.github.com> Date: Tue, 15 Feb 2022 23:03:55 -0800 Subject: [PATCH] fix: Update dependency @google-cloud/logging from 9.0.0 to 9.6.9 (#667) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Update dependency @google-cloud/logging from 9.0.0 to 9.6.9 * Update README and fix typo * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update comment * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fixed comment * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- .readme-partials.yaml | 23 +++++++++++++++++++++++ README.md | 23 +++++++++++++++++++++++ package.json | 2 +- src/common.ts | 1 + src/index.ts | 5 +++++ test/common.ts | 14 ++++++++++++++ 6 files changed, 67 insertions(+), 1 deletion(-) diff --git a/.readme-partials.yaml b/.readme-partials.yaml index cfa3de06..85ec97f3 100644 --- a/.readme-partials.yaml +++ b/.readme-partials.yaml @@ -100,6 +100,29 @@ body: |- You may also want to see the [@google-cloud/error-reporting](https://github.com/googleapis/nodejs-error-reporting) module which provides direct access to the Error Reporting API. + ### Error handling with a default callback + + The `LoggingWinston` class creates an instance of `LoggingCommon` which uses the `Log` class from `@google-cloud/logging` package to write log entries. + The `Log` class writes logs asynchronously and there are cases when log entries cannot be written and an error is + thrown - if error is not handled properly, it could crash the application. One possible way to handle the error is to provide a default callback + to the `LoggingWinston` constructor which will be used to initialize `Log` object with that callback like in example below: + + ```js + // Imports the Google Cloud client library for Winston + const {LoggingWinston} = require('@google-cloud/logging-winston'); + + // Creates a client + const loggingWinston = new LoggingWinston({ + projectId: 'your-project-id', + keyFilename: '/path/to/key.json', + defaultCallback: err => { + if (err) { + console.log('Error occured: ' + err); + } + }, + }); + ``` + ### Formatting Request Logs **NOTE: The express middleware provided by this library handles this automatically for you. These instructions are for there case where you may want to handle this manually.** diff --git a/README.md b/README.md index e1cc4375..b25c8dcd 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,29 @@ Make sure to add logs to your [uncaught exception][uncaught] and [unhandled reje You may also want to see the [@google-cloud/error-reporting](https://github.com/googleapis/nodejs-error-reporting) module which provides direct access to the Error Reporting API. +### Error handling with a default callback + +The `LoggingWinston` class creates an instance of `LoggingCommon` which uses the `Log` class from `@google-cloud/logging` package to write log entries. +The `Log` class writes logs asynchronously and there are cases when log entries cannot be written and an error is +thrown - if error is not handled properly, it could crash the application. One possible way to handle the error is to provide a default callback +to the `LoggingWinston` constructor which will be used to initialize `Log` object with that callback like in example below: + +```js +// Imports the Google Cloud client library for Winston +const {LoggingWinston} = require('@google-cloud/logging-winston'); + +// Creates a client +const loggingWinston = new LoggingWinston({ +projectId: 'your-project-id', +keyFilename: '/path/to/key.json', +defaultCallback: err => { + if (err) { + console.log('Error occured: ' + err); + } +}, +}); +``` + ### Formatting Request Logs **NOTE: The express middleware provided by this library handles this automatically for you. These instructions are for there case where you may want to handle this manually.** diff --git a/package.json b/package.json index 8649b245..be7155cb 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "precompile": "gts clean" }, "dependencies": { - "@google-cloud/logging": "^9.0.0", + "@google-cloud/logging": "^9.6.9", "google-auth-library": "^7.0.0", "lodash.mapvalues": "^4.6.0", "winston-transport": "^4.3.0" diff --git a/src/common.ts b/src/common.ts index 8943ce66..5e8d2aba 100644 --- a/src/common.ts +++ b/src/common.ts @@ -140,6 +140,7 @@ export class LoggingCommon { // 256,000 limit. maxEntrySize: options.maxEntrySize || 250000, // eslint-disable-next-line @typescript-eslint/no-explicit-any + defaultWriteDeleteCallback: options.defaultCallback, }); this.resource = options.resource; this.serviceContext = options.serviceContext; diff --git a/src/index.ts b/src/index.ts index 9c6f8c8a..e8814e53 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,6 +28,7 @@ import { ServiceContext, LoggingOptions, } from '@google-cloud/logging'; +import {ApiResponseCallback} from '@google-cloud/logging/build/src/log'; const LEVEL = Symbol.for('level'); @@ -85,6 +86,10 @@ export interface Options extends LoggingOptions { // An attempt will be made to truncate messages larger than maxEntrySize. maxEntrySize?: number; + + // A default global callback to be used for {@link LoggingWinston#log} when callback is + // not supplied by caller in function parameters + defaultCallback?: ApiResponseCallback; } /** diff --git a/test/common.ts b/test/common.ts index 25a24776..9e9c6ba3 100644 --- a/test/common.ts +++ b/test/common.ts @@ -158,6 +158,20 @@ describe('logging-common', () => { assert.deepStrictEqual(fakeLogOptions_, { removeCircular: true, maxEntrySize: 250000, + defaultWriteDeleteCallback: undefined, + }); + }); + + it('should set default callback', () => { + const optionsWithDefaultCallback = Object.assign({}, OPTIONS, { + defaultCallback: () => {}, + }); + new loggingCommonLib.LoggingCommon(optionsWithDefaultCallback); + + assert.deepStrictEqual(fakeLogOptions_, { + removeCircular: true, + maxEntrySize: 250000, + defaultWriteDeleteCallback: optionsWithDefaultCallback.defaultCallback, }); });