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

Add disableServerLogging option #39

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ var $logger = beaver.Logger({

// Use sendBeacon if supported rather than XHR to send logs; defaults to false
enableSendBeacon: true,

// Stop log events from being sent to server
disableServerLogging: false,

});
```

Expand Down
9 changes: 7 additions & 2 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type LoggerOptions = {|
logLevel? : $Values<typeof LOG_LEVEL>,
transport? : Transport,
flushInterval? : number,
enableSendBeacon? : boolean
enableSendBeacon? : boolean,
disableServerLogging? : boolean
|};

type ClientPayload = { [string] : ?string | ?boolean };
Expand Down Expand Up @@ -72,7 +73,7 @@ function extendIfDefined(target : { [string] : string | boolean }, source : { [s
}
}

export function Logger({ url, prefix, logLevel = DEFAULT_LOG_LEVEL, transport = httpTransport, flushInterval = FLUSH_INTERVAL, enableSendBeacon = false } : LoggerOptions) : LoggerType {
export function Logger({ url, prefix, logLevel = DEFAULT_LOG_LEVEL, transport = httpTransport, flushInterval = FLUSH_INTERVAL, enableSendBeacon = false, disableServerLogging = false } : LoggerOptions) : LoggerType {

let events : Array<{| level : $Values<typeof LOG_LEVEL>, event : string, payload : Payload |}> = [];
let tracking : Array<Payload> = [];
Expand Down Expand Up @@ -154,6 +155,10 @@ export function Logger({ url, prefix, logLevel = DEFAULT_LOG_LEVEL, transport =

function enqueue(level : $Values<typeof LOG_LEVEL>, event : string, payload : Payload) {

if (disableServerLogging) {
return;
}

events.push({
level,
event,
Expand Down
44 changes: 44 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,48 @@ describe('beaver-logger tests', () => {
}
});
});

it('should not send event to server if disableServerLogging is true', () => {
const $logger = Logger({
url: '/test/api/log',
disableServerLogging: true
});

const browserConsoleLogs = [];

window.console.info = (args) => browserConsoleLogs.push(args);

$logger.info('browser_only_log', {
foo: 'bar',
bar: true
});

let handlerCalled = false;

const logEndpoint = $mockEndpoint.register({
method: 'POST',
uri: '/test/api/log',
handler: () => {
handlerCalled = true;
return {};
}
});

let apiCallsFired = true;
logEndpoint.expectCalls();
return $logger.flush().then(() => {
try {
logEndpoint.done(); // will throw if no API calls received by logEndpoint
} catch (e) {
apiCallsFired = false;
}
if (handlerCalled || apiCallsFired) {
throw new Error('Expected no API calls to be fired');
}

if (!browserConsoleLogs.includes('browser_only_log')) {
throw new Error('Expected events to be logged on browser');
}
});
});
});