Skip to content

Commit

Permalink
✨ Added log collection to logger of sdk utils (#1628)
Browse files Browse the repository at this point in the history
* Added log collection to logger of sdk utils

* Removed extra log

* Fixed tests
  • Loading branch information
ninadbstack committed Jun 21, 2024
1 parent 441fc09 commit 208bdae
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/sdk-utils/src/logger.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import request from './request.js';

// Used when determining if a message should be logged
const LOG_LEVELS = { debug: 0, info: 1, warn: 2, error: 3 };

Expand All @@ -15,7 +17,8 @@ Object.assign(logger, {
},

// Track and send/write logs for the specified namespace and log level
log: (ns, lvl, msg, meta) => {
// remote should only be false in case of sensitive/self call for errors
log: (ns, lvl, msg, meta, remote = true) => {
let err = typeof msg !== 'string' && (lvl === 'error' || lvl === 'debug');

// check if the specific level is within the local loglevel range
Expand All @@ -34,6 +37,13 @@ Object.assign(logger, {
// use process[stdout|stderr].write in node
process[lvl === 'info' ? 'stdout' : 'stderr'].write(msg + '\n');
}
if (remote && (lvl === 'error' || debug)) {
return request.post('/percy/log', {
level: lvl, message: msg, meta
}).catch(_ => {
logger.log(ns, 'error', 'Could not send logs to cli', meta, false);
});
}
}
}
});
Expand Down
51 changes: 51 additions & 0 deletions packages/sdk-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,5 +434,56 @@ describe('SDK Utils', () => {
'[percy:test] Error stack'
]);
});

it('sends logs to cli if log level is error', async () => {
// we never want to await in real sdk but we await in test for validation
await log.error('Some error', { name: 'abcd' });

await expectAsync(helpers.get('requests')).toBeResolvedTo([{
url: '/percy/log',
method: 'POST',
body: {
level: 'error',
message: jasmine.stringContaining('Some error'),
meta: { name: 'abcd' }
}
}]);
});

it('sends all logs to cli if log level is debug', async () => {
logger.loglevel('debug');
// we never want to await in real sdk but we await in test for validation
await log.error('Some error', { name: 'abcd' });
await log.info('Some info', { name: 'abcd' });

await expectAsync(helpers.get('requests')).toBeResolvedTo([{
url: '/percy/log',
method: 'POST',
body: {
level: 'error',
message: jasmine.stringContaining('Some error'),
meta: { name: 'abcd' }
}
}, {
url: '/percy/log',
method: 'POST',
body: {
level: 'info',
message: jasmine.stringContaining('Some info'),
meta: { name: 'abcd' }
}
}]);
});

it('sends logs error if sending to cli fails', async () => {
await helpers.test('error', '/percy/log');
// we never want to await in real sdk but we await in test for validation
await log.error('Some error', { name: 'abcd' });

expect(stderr).toEqual([
'[percy] Some error',
'[percy] Could not send logs to cli'
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ export default class PlaywrightProvider extends GenericProvider {
const osName = normalizeTags.osRollUp(automateCaps.os);
const device = mobileOS.includes(osName.toUpperCase());
tagData.device = device;
console.log(`Device: ${device}`);
return await super.getTag(tagData);
}
}

0 comments on commit 208bdae

Please sign in to comment.