Skip to content

Commit

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

* Remove obsolete test

* Fax test failure

* Add test to validate callback is called
  • Loading branch information
losalex authored Mar 9, 2022
1 parent 52676bf commit 4bc7baa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
9 changes: 6 additions & 3 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class LoggingCommon {
private serviceContext: ServiceContext | undefined;
private prefix: string | undefined;
private labels: object | undefined;
private defaultCallback?: Callback;
// LOGGING_TRACE_KEY is Cloud Logging specific and has the format:
// logging.googleapis.com/trace
static readonly LOGGING_TRACE_KEY = LOGGING_TRACE_KEY;
Expand All @@ -139,13 +140,12 @@ export class LoggingCommon {
// 250,000 has been chosen to keep us comfortably within the
// 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;
this.prefix = options.prefix;
this.labels = options.labels;
this.defaultCallback = options.defaultCallback;
}

log(
Expand Down Expand Up @@ -257,7 +257,10 @@ export class LoggingCommon {
}

const entry = this.stackdriverLog.entry(entryMetadata, data);
this.stackdriverLog[stackdriverLevel](entry, callback);
this.stackdriverLog[stackdriverLevel](
entry,
this.defaultCallback ?? callback
);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ 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 @@ -89,7 +88,7 @@ export interface Options extends LoggingOptions {

// A default global callback to be used for {@link LoggingWinston#log} when callback is
// not supplied by caller in function parameters
defaultCallback?: ApiResponseCallback;
defaultCallback?: Callback;
}

/**
Expand Down
14 changes: 0 additions & 14 deletions test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,6 @@ 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
19 changes: 19 additions & 0 deletions test/stackdriver-trace-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ describe('Stackdriver Trace Log Correlation', () => {
let setCurrentContextId: (id: string) => void;
// The Stackdriver Logging Winston transport library.
let loggingWinstonLib: typeof loggingWinstonLibTypes;
// The flag indicating if callback was called or not
let isCallbackCalled: boolean;

class FakeLogging {
constructor() {}
Expand Down Expand Up @@ -63,6 +65,7 @@ describe('Stackdriver Trace Log Correlation', () => {

beforeEach(() => {
seenContextIds.length = 0;
isCallbackCalled = false;
setCurrentContextId = (() => {
let currentContextId: string;
global._google_trace_agent = {
Expand Down Expand Up @@ -128,6 +131,22 @@ describe('Stackdriver Trace Log Correlation', () => {
});
});

it('Calls default callback when present', done => {
const transport = new loggingWinstonLib.LoggingWinston({
defaultCallback: () => {
isCallbackCalled = true;
},
});
const logger = winston.createLogger({
transports: [transport],
});
logger.log({level: 'info', message: 'hello'});
setImmediate(() => {
assert.strictEqual(isCallbackCalled, true);
done();
});
});

[null, {}, {getWriterProjectId: () => 'project1'}].forEach(testCase => {
it(`Doesn't crash when a non-compatible Trace Agent is present: ${testCase}`, done => {
global._google_trace_agent = testCase;
Expand Down

0 comments on commit 4bc7baa

Please sign in to comment.