Skip to content

Commit

Permalink
fix: Use defaultCallback in LoggingBunyan class (#601)
Browse files Browse the repository at this point in the history
* fix: Use defaultCallback in LoggingCommon class

* Add test validating write with callback
  • Loading branch information
losalex authored Mar 9, 2022
1 parent f4d988a commit f4c01ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {Logging, detectServiceContext} from '@google-cloud/logging';

import * as types from './types/core';

import {ApiResponseCallback} from '@google-cloud/logging/build/src/log';

// Map of Stackdriver logging levels.
const BUNYAN_TO_STACKDRIVER: Map<number, string> = new Map([
[60, 'CRITICAL'],
Expand Down Expand Up @@ -157,21 +159,22 @@ export class LoggingBunyan extends Writable {
private logName: string;
private resource: types.MonitoredResource | undefined;
private serviceContext?: types.ServiceContext;
private defaultCallback?: ApiResponseCallback;
stackdriverLog: types.StackdriverLog; // TODO: add type for @google-cloud/logging
constructor(options?: types.Options) {
options = options || {};
super({objectMode: true});
this.logName = options.logName || 'bunyan_log';
this.resource = options.resource;
this.serviceContext = options.serviceContext;
this.defaultCallback = options.defaultCallback;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.stackdriverLog = new Logging(options as any).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,
defaultWriteDeleteCallback: options.defaultCallback,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any;

Expand Down Expand Up @@ -347,7 +350,7 @@ export class LoggingBunyan extends Writable {
*/
_write(record: types.BunyanLogRecord, encoding: string, callback: Function) {
const entry = this.formatEntry_(record);
this.stackdriverLog.write(entry, callback);
this.stackdriverLog.write(entry, this.defaultCallback ?? callback);
}

/**
Expand All @@ -373,7 +376,7 @@ export class LoggingBunyan extends Writable {
}
);

this.stackdriverLog.write(entries, callback);
this.stackdriverLog.write(entries, this.defaultCallback ?? callback);
}
}

Expand Down
23 changes: 22 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ describe('logging-bunyan', () => {
assert.strictEqual(fakeLoggingOptions_, optionsWithoutLogName);
assert.strictEqual(fakeLogName_, 'bunyan_log');
assert.deepStrictEqual(fakeLogOptions_, {
defaultWriteDeleteCallback: undefined,
removeCircular: true,
maxEntrySize: 250000,
});
Expand Down Expand Up @@ -645,6 +644,28 @@ describe('logging-bunyan', () => {

loggingBunyan._write(RECORD, '', done);
});

it('should write the record and call default callback', done => {
let isCallbackCalled = false;
loggingBunyan.stackdriverLog.entry = () => {
return {};
};
loggingBunyan.defaultCallback = () => {
isCallbackCalled = true;
};
loggingBunyan.stackdriverLog.write =
// Writable.write used 'any' in function signature.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(entries: any, callback: Function) => {
callback();
};

loggingBunyan._write(RECORD, '', () => {
throw Error('Should never be called!');
});
assert.strictEqual(isCallbackCalled, true);
done();
});
});

describe('_writev', () => {
Expand Down

0 comments on commit f4c01ab

Please # to comment.