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

hotfix/SUIT-16525-log-level-v3 #473

Open
wants to merge 4 commits into
base: master
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: 2 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const DEFAULT_TIMEOUT = 2000;
const overridableFields = [
'tokenId', 'tokenPassword', 'concurrency', 'preset', 'presets', 'deviceId', 'appConfigId', 'inspect', 'inspectBrk',
'logLevel', 'logDir', 'timestamp', 'configFile', 'disallowCrashReports', 'defaultTimeout', 'screenshotDir',
'includeChangelist', 'recordingOption', 'webhookUrl',
'includeChangelist', 'testLines', 'testErrors', 'networkLogs', 'consoleLogs', // launcher common extra log options
'recordingOption', 'webhookUrl',
];

const serverAddress = process.env[envVars.SUITEST_BE_SERVER] || 'the.suite.st';
Expand Down
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ declare namespace suitest {
setContinueOnFatalError(continueOnFatalError: ConfigureOptions['continueOnFatalError']): void;
setDisallowCrashReports(disallowCrashReports: ConfigureOptions['disallowCrashReports']): void;
setLogLevel(logLevel: ConfigureOptions['logLevel']): void;
setLogLevel(testLines: ConfigureOptions['testLines']): void;
setLogLevel(testErrors: ConfigureOptions['testErrors']): void;
setLogLevel(networkLogs: ConfigureOptions['networkLogs']): void;
setLogLevel(consoleLogs: ConfigureOptions['consoleLogs']): void;
setRecordingOption(recordingOption: ConfigureOptions['recordingOption']): void;
setWebhookUrl(webhookUrl: ConfigureOptions['webhookUrl']): void;

Expand Down Expand Up @@ -282,6 +286,10 @@ declare namespace suitest {

interface ConfigureOptions {
logLevel: 'silent'|'normal'|'verbose'|'debug'|'silly';
testLines: 'silent'|'normal'|'verbose'|'debug'|'silly';
testErrors: 'silent'|'normal'|'verbose'|'debug'|'silly';
networkLogs?: 'silent'|'normal'|'verbose'|'debug'|'silly';
consoleLogs?: 'silent'|'normal'|'verbose'|'debug'|'silly';
recordingOption: 'autostart'|'manualstart'|'none';
webhookUrl: string;
disallowCrashReports: boolean;
Expand Down
10 changes: 8 additions & 2 deletions lib/api/webSockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ const webSocketsFactory = (self) => {
const requestBody = logItem.request && logItem.request.requestBody;
const responseBody = logItem.response && logItem.response.responseBody;

if (config.logLevel === logLevels.debug) {
if (
(config.networkLogs && config.networkLogs === logLevels.debug) ||
(!config.networkLogs && config.logLevel === logLevels.debug)
) {
return logger.debug(
'Incoming request log (details omitted)\n' +
`ItemId: ${requestId}\n` +
Expand All @@ -91,7 +94,10 @@ const webSocketsFactory = (self) => {
(responseBody ? 'ResponseBody: [body]' : '') +
'\n',
);
} else if (config.logLevel === logLevels.silly) {
} else if (
(config.networkLogs && config.networkLogs === logLevels.silly) ||
(!config.networkLogs && config.logLevel === logLevels.silly)
) {
return logger.silly(
'Incoming request log\n' +
`ItemId: ${requestId}\n` +
Expand Down
15 changes: 13 additions & 2 deletions lib/chains/locationChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ const {
gettersComposer,
makeToJSONComposer,
} = require('../composers');
const {getRequestType} = require('../utils/socketChainHelper');
const {
getRequestType,
processServerResponse,
} = require('../utils/socketChainHelper');
const {
applyTimeout,
applyNegation,
} = require('../utils/chainUtils');

const locationFactory = (classInstance) => {
const {logger} = classInstance;

const toJSON = data => {
const type = getRequestType(data);
const subject = {type: 'location'};
Expand All @@ -47,7 +52,13 @@ const locationFactory = (classInstance) => {

// Build Composers
const toStringComposer = makeToStringComposer(toJSON);
const thenComposer = makeThenComposer(toJSON);
const thenComposer = makeThenComposer(toJSON, processServerResponse(
logger,
{
logLevel: classInstance.getConfig().logLevel,
testLines: classInstance.getConfig().testLines,
testErrors: classInstance.getConfig().testErrors,
}));
const toJSONComposer = makeToJSONComposer(toJSON);

/**
Expand Down
15 changes: 13 additions & 2 deletions lib/chains/openUrlChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ const {
makeToJSONComposer,
} = require('../composers');
const {invalidInputMessage} = require('../texts');
const {getRequestType} = require('../utils/socketChainHelper');
const {
getRequestType,
processServerResponse,
} = require('../utils/socketChainHelper');
const {validate, validators} = require('../validation');

const openUrlFactory = (classInstance) => {
const {logger} = classInstance;

const toJSON = data => ({
type: getRequestType(data, false),
request: {
Expand All @@ -24,7 +29,13 @@ const openUrlFactory = (classInstance) => {

// Build Composers
const toStringComposer = makeToStringComposer(toJSON);
const thenComposer = makeThenComposer(toJSON);
const thenComposer = makeThenComposer(toJSON, processServerResponse(
logger,
{
logLevel: classInstance.getConfig().logLevel,
testLines: classInstance.getConfig().testLines,
testErrors: classInstance.getConfig().testErrors,
}));
const toJSONComposer = makeToJSONComposer(toJSON);

/**
Expand Down
17 changes: 14 additions & 3 deletions lib/composers/thenComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const makeThenComposer = (getSocketMessage, callback, beforeSend) => makeMethodC
if (beforeSend) {
beforeSend(data);
} else {
const translation = translateLine(dataToTranslate, config.logLevel);
const translation = translateLine(dataToTranslate, config.testLines || config.logLevel);

if (translation) {
logger.log(translation);
Expand All @@ -49,11 +49,22 @@ const makeThenComposer = (getSocketMessage, callback, beforeSend) => makeMethodC
.then(() => webSockets.send(socketMessage).then(result => {
return callback
? callback(result, data, socketMessage)
: processServerResponse(logger, config.logLevel)(result, data, socketMessage, snippets);
: processServerResponse(
logger,
{
logLevel: config.logLevel,
testLines: config.testLines,
testErrors: config.testError,
})(result, data, socketMessage, snippets);
}, error => {
return callback
? callback(error, data, socketMessage)
: processServerResponse(logger, config.logLevel)(error, data, socketMessage, snippets);
: processServerResponse(logger,
{
logLevel: config.logLevel,
testLines: config.testLines,
testErrors: config.testErrors,
})(error, data, socketMessage, snippets);
}))
.catch(err => {
if (err instanceof SuitestError && err.code === SuitestError.AUTH_NOT_ALLOWED) {
Expand Down
11 changes: 9 additions & 2 deletions lib/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,12 @@ const createLogger = (config, pairedDeviceContext) => {
const log = method => (...messages) => {
// check if logging is allowed by logging level specified in config
/* istanbul ignore if */
if (!method.levels.includes(config.logLevel))
if (
!(config.networkLogs && method.levels.includes(config.networkLogs) && method.key === 'debug') &&
!(!config.networkLogs && method.levels.includes(config.logLevel))
) {
return;
}

clearTimeout(delayedLog);

Expand Down Expand Up @@ -321,7 +325,10 @@ const createLogger = (config, pairedDeviceContext) => {
*/
getAppOutput: (subtype, data) => {
// No need to display anything
if (config.logLevel === logLevels.silent) {
if (
(config.consoleLogs && config.consoleLogs === logLevels.silent) ||
(!config.consoleLogs && config.logLevel === logLevels.silent)
) {
return;
}

Expand Down
17 changes: 9 additions & 8 deletions lib/utils/socketChainHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ const assertionErrorTypes = ['queryFailed', 'appRunning', 'appNotRunning', 'quer
const processServerResponse = (logger, verbosity) =>
function processServerResponseHandler(res, data, jsonMessage, snippets) {
const isSnippet = data.type === 'runSnippet';
const isSuccess = res.result === 'success' || res.result === 'warning';
const isEval = res.contentType === 'eval';
const isTestLine = res.contentType === 'testLine';
const isQuery = res.contentType === 'query';
const isTakeScreenshot = res.contentType === 'takeScreenshot';
const isAborted = res.result === 'aborted';
const isAssertionError = isSnippet || assertionErrorTypes.includes(normalizeErrorType(res));
const responseForError = getResponseForError(res);
const message = getErrorMessage({
response: res,
verbosity,
verbosity: verbosity ? verbosity.testErrors || verbosity.logLevel : undefined,
jsonMessage,
snippets,
});
Expand All @@ -37,12 +43,6 @@ const processServerResponse = (logger, verbosity) =>
throw err;
};
const infoMessage = prefix => getInfoErrorMessage(message, prefix, res, data.stack);
const isSuccess = res.result === 'success' || res.result === 'warning';
const isEval = res.contentType === 'eval';
const isTestLine = res.contentType === 'testLine';
const isQuery = res.contentType === 'query';
const isTakeScreenshot = res.contentType === 'takeScreenshot';
const isAborted = res.result === 'aborted';

// warnings
if (res.result === 'warning') {
Expand Down Expand Up @@ -94,7 +94,8 @@ const processServerResponse = (logger, verbosity) =>
if (isSuccess) {
if (isSnippet) {
const snippet = getSnippetLogs({
verbosity: mapLogLevelsToTranslationModule[verbosity],
// eslint-disable-next-line max-len
verbosity: mapLogLevelsToTranslationModule[verbosity ? verbosity.testLines || verbosity.logLevel : undefined],
definitions: snippets,
results: [],
testId: jsonMessage.request.val,
Expand Down
4 changes: 4 additions & 0 deletions lib/validation/jsonSchemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ schemas[validationKeys.CONFIGURE] = {
'logDir': {'type': 'string'},
'defaultTimeout': {'type': 'number'},
'logLevel': {'enum': ['silent', 'normal', 'verbose', 'debug', 'silly']},
'testLines': {'enum': ['silent', 'normal', 'verbose', 'debug', 'silly']},
'testErrors': {'enum': ['silent', 'normal', 'verbose', 'debug', 'silly']},
'networkLogs': {'enum': ['silent', 'normal', 'verbose', 'debug', 'silly']},
'consoleLogs': {'enum': ['silent', 'normal', 'verbose', 'debug', 'silly']},
'orgId': {'type': 'string'},
'repl': {'type': 'boolean'},
'timestamp': {'type': 'string'},
Expand Down
36 changes: 21 additions & 15 deletions test/utils/socketChainHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,54 @@ describe('socket chain helpers', () => {
},
};

const logVerbose = {
logLevel: 'verbose',
testLines: undefined,
testErrors: undefined,
};

// query
assert.throws(() => processServerResponse(logger, 'verbose')({
assert.throws(() => processServerResponse(logger, logVerbose)({
contentType: 'query',
}, {stack: ''}, chain), SuitestError, 'query fail');
assert.strictEqual(processServerResponse(logger, 'verbose')({
assert.strictEqual(processServerResponse(logger, logVerbose)({
contentType: 'query',
cookieExists: true,
}, {stack: ''}, chain), true, 'query cookie exists');
assert.strictEqual(processServerResponse(logger, 'verbose')({
assert.strictEqual(processServerResponse(logger, logVerbose)({
contentType: 'query',
elementExists: false,
}, {stack: ''}, chain), undefined, 'query element not found');
// eval
assert.strictEqual(processServerResponse(logger, 'verbose')({
assert.strictEqual(processServerResponse(logger, logVerbose)({
contentType: 'eval',
result: 'success',
errorType: 'error',
}, {stack: ''}, chain), true, 'evals success');
assert.strictEqual(processServerResponse(logger, 'verbose')({
assert.strictEqual(processServerResponse(logger, logVerbose)({
contentType: 'eval',
result: 'fail',
errorType: 'queryFailed',
}, {stack: ''}, chain), false, 'eval fail');
// test line
assert.strictEqual(processServerResponse(logger, 'verbose')({
assert.strictEqual(processServerResponse(logger, logVerbose)({
contentType: 'testLine',
result: 'success',
}, {stack: ''}, chain), undefined, 'testLine success');
// all other
assert.throws(() => processServerResponse(logger, 'verbose')({
assert.throws(() => processServerResponse(logger, logVerbose)({
result: 'fail',
}, {stack: ''}, chain), Error, 'testLine fail');
assert.throws(() => processServerResponse(logger, 'verbose')({
assert.throws(() => processServerResponse(logger, logVerbose)({
result: 'error',
}, {stack: ''}, chain), Error, 'testLine fail');
// execution error
assert.throws(() => processServerResponse(logger, 'verbose')({
assert.throws(() => processServerResponse(logger, logVerbose)({
executionError: 'appNotRunning',
}, {stack: ''}, chain), Error, 'execution error');

assert.throws(
() => processServerResponse(logger, 'verbose')({
() => processServerResponse(logger, logVerbose)({
contentType: 'eval',
result: 'fail',
errorType: 'invalidInput',
Expand Down Expand Up @@ -147,7 +153,7 @@ describe('socket chain helpers', () => {
);

assert.throws(
() => processServerResponse(logger, 'verbose')(
() => processServerResponse(logger, logVerbose)(
{
result: 'fail',
errorType: 'queryFailed',
Expand All @@ -162,7 +168,7 @@ describe('socket chain helpers', () => {
);

assert.throws(
() => processServerResponse(logger, 'verbose')(
() => processServerResponse(logger, logVerbose)(
{
result: 'fail',
expression: [
Expand Down Expand Up @@ -240,7 +246,7 @@ describe('socket chain helpers', () => {
);

assert.throws(
() => processServerResponse(logger, 'verbose')(
() => processServerResponse(logger, logVerbose)(
{
result: 'fail',
expression: [
Expand Down Expand Up @@ -303,7 +309,7 @@ describe('socket chain helpers', () => {
);

assert.throws(
() => processServerResponse(logger, 'verbose')(
() => processServerResponse(logger, logVerbose)(
{
result: 'fail',
expression: [{
Expand Down Expand Up @@ -363,7 +369,7 @@ describe('socket chain helpers', () => {
sinon.stub(logger, 'warn');

try {
assert.strictEqual(processServerResponse(logger, 'verbose')({
assert.strictEqual(processServerResponse(logger, logVerbose)({
contentType: 'eval',
result: 'warning',
errorType: 'error',
Expand Down