Skip to content

Commit

Permalink
fix: Update dependency @google-cloud/logging from 9.0.0 to 9.6.9 (#667)
Browse files Browse the repository at this point in the history
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
losalex and gcf-owl-bot[bot] committed Feb 16, 2022
1 parent 2519fcd commit 6fcda1e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .readme-partials.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.**
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});

Expand Down

0 comments on commit 6fcda1e

Please sign in to comment.