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 disableBrowserLogging option #28 #37

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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ var $logger = beaver.Logger({

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

// Disable logs to be displayed in the browser console
disableBrowserLogging: true
});
```

Expand Down
7 changes: 4 additions & 3 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,
disableBrowserLogging? : 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, disableBrowserLogging = false } : LoggerOptions) : LoggerType {

let events : Array<{| level : $Values<typeof LOG_LEVEL>, event : string, payload : Payload |}> = [];
let tracking : Array<Payload> = [];
Expand All @@ -84,7 +85,7 @@ export function Logger({ url, prefix, logLevel = DEFAULT_LOG_LEVEL, transport =

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

if (!isBrowser() || !window.console || !window.console.log) {
if (!isBrowser() || !window.console || !window.console.log || disableBrowserLogging) {
return;
}

Expand Down
68 changes: 67 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Logger } from '../src';
patchXmlHttpRequest();

describe('beaver-logger tests', () => {

it('should log something and flush it to the buffer', () => {
// eslint-disable-next-line compat/compat
window.navigator.sendBeacon = undefined; // simulate IE 11 scenario
Expand Down Expand Up @@ -128,4 +127,71 @@ describe('beaver-logger tests', () => {
}
});
});

it('should not print logs in browser side', () => {
const loggerDB = [];

const mockConsole = (...args) => loggerDB.push(args);
const findLoggedDBEntry = (eventName) => {
const entryLog = loggerDB.find(event => event[0] === eventName);

if (entryLog) {
throw new Error('Expected log to not be printed in browser side');
}
};

window.console.info = mockConsole;
window.console.error = mockConsole;
window.console.warn = mockConsole;
window.console.debug = mockConsole;

const $logger = Logger({
url: '/test/api/log',
disableBrowserLogging: true
});

$logger.info('test_info', {
foo: 'bar',
bar: true
});
$logger.error('test_error', {
foo: 'bar',
bar: true
});
$logger.warn('test_warn', {
foo: 'bar',
bar: true
});
$logger.debug('test_debug', {
foo: 'bar',
bar: true
});

const logEndpoint = $mockEndpoint.register({
method: 'POST',
uri: '/test/api/log',
handler: (req) => {
const hasLogInfo = req.data.events.some(event => event.event === 'test_info' && event.level === 'info');
const hasLogError = req.data.events.some(event => event.event === 'test_error' && event.level === 'error');
const hasLogWarn = req.data.events.some(event => event.event === 'test_warn' && event.level === 'warn');
const hasLogDebug = req.data.events.some(event => event.event === 'test_debug' && event.level === 'debug');

if (!hasLogInfo || !hasLogError || !hasLogWarn || !hasLogDebug) {
throw new Error('Expected posted payload to contain logged log');
}

return {};
}
});

findLoggedDBEntry('test_info');
findLoggedDBEntry('test_error');
findLoggedDBEntry('test_warn');
findLoggedDBEntry('test_log');

logEndpoint.expectCalls();
return $logger.flush().then(() => {
logEndpoint.done();
});
});
});