Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add extraData parameter to info function #762

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions __tests__/Logger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const Log = new Logger({
clientLoggingCallback: mockClientLoggingCallback,
});

const DebugLog = new Logger({
serverLoggingCallback: mockServerLoggingCallback,
clientLoggingCallback: mockClientLoggingCallback,
isDebug: true,
});

test('Test Log.info()', () => {
Log.info('Test1', false);
expect(mockServerLoggingCallback).toHaveBeenCalledTimes(0);
Expand All @@ -24,9 +30,14 @@ test('Test Log.info()', () => {
delete packet[0].timestamp;
delete packet[1].timestamp;
expect(packet).toEqual([
{message: "[info] Test1", parameters: "", },
{message: "[info] Test2", parameters: "", },
{message: "[info] Test1", parameters: "",},
{message: "[info] Test2", parameters: "",},
]);

// Test the case where `isDebug` is `true` in `Log` instance and we pass `extraData` parameter
DebugLog.info('Test2', false, {test: 'test'}, false, {test: 'test'});
expect(mockServerLoggingCallback).toHaveBeenCalled();
expect(mockClientLoggingCallback).toHaveBeenCalledWith('[info] Test2 - {"test":"test"}', {test: 'test'});
});

test('Test Log.alert()', () => {
Expand Down Expand Up @@ -85,5 +96,5 @@ test('Test Log.hmmm()', () => {
test('Test Log.client()', () => {
Log.client('Test');
expect(mockClientLoggingCallback).toHaveBeenCalled();
expect(mockClientLoggingCallback).toHaveBeenCalledWith('Test');
expect(mockClientLoggingCallback).toHaveBeenCalledWith('Test', '');
});
14 changes: 7 additions & 7 deletions lib/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type Parameters = string | Record<string, unknown> | Array<Record<string, unknown>> | Error;
type ServerLoggingCallbackOptions = {api_setCookie: boolean; logPacket: string};
type ServerLoggingCallback = (logger: Logger, options: ServerLoggingCallbackOptions) => Promise<{requestID: string}> | undefined;
type ClientLoggingCallBack = (message: string) => void;
type ClientLoggingCallBack = (message: string, extraData: Parameters) => void;
type LogLine = {message: string; parameters: Parameters; onlyFlushWithOthers?: boolean; timestamp: Date};
type LoggerOptions = {serverLoggingCallback: ServerLoggingCallback; isDebug: boolean; clientLoggingCallback: ClientLoggingCallBack};

Expand Down Expand Up @@ -66,7 +66,7 @@ export default class Logger {
* @param forceFlushToServer Should we force flushing all logs to server?
* @param onlyFlushWithOthers A request will never be sent to the server if all loglines have this set to true
*/
add(message: string, parameters: Parameters, forceFlushToServer: boolean, onlyFlushWithOthers = false) {
add(message: string, parameters: Parameters, forceFlushToServer: boolean, onlyFlushWithOthers = false, extraData: Parameters = '') {
const length = this.logLines.push({
message,
parameters,
Expand All @@ -75,7 +75,7 @@ export default class Logger {
});

if (this.isDebug) {
this.client(`${message} - ${JSON.stringify(parameters)}`);
this.client(`${message} - ${JSON.stringify(parameters)}`, extraData);
}

// If we're over the limit, flush the logs
Expand All @@ -93,9 +93,9 @@ export default class Logger {
* @param parameters The parameters to send along with the message
* @param onlyFlushWithOthers A request will never be sent to the server if all loglines have this set to true
*/
info(message: string, sendNow = false, parameters: Parameters = '', onlyFlushWithOthers = false) {
info(message: string, sendNow = false, parameters: Parameters = '', onlyFlushWithOthers = false, extraData: Parameters = '') {
const msg = `[info] ${message}`;
this.add(msg, parameters, sendNow, onlyFlushWithOthers);
this.add(msg, parameters, sendNow, onlyFlushWithOthers, extraData);
}

/**
Expand Down Expand Up @@ -143,11 +143,11 @@ export default class Logger {
*
* @param message The message to log.
*/
client(message: string) {
client(message: string, extraData: Parameters = '') {
if (!this.clientLoggingCallback) {
return;
}

this.clientLoggingCallback(message);
this.clientLoggingCallback(message, extraData);
}
}
Loading