Skip to content

Commit ade8ef2

Browse files
authored
Merge pull request #140 from joomcode/fix/customize-number-of-lines-displayed-in-logs
feat: customize number of lines displayed in logs
2 parents e22de31 + aa8c2e7 commit ade8ef2

9 files changed

Lines changed: 62 additions & 23 deletions

File tree

src/constants/inspect.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ const averageLineLength = 100;
66
*/
77
export const DEFAULT_INSPECT_OPTIONS = {
88
colors: false,
9-
depth: 16,
10-
numericSeparator: true,
9+
depth: 12,
10+
getters: false,
11+
numericSeparator: false,
1112
showHidden: true,
1213
} as const;
1314

15+
/**
16+
* Default max number of lines in printed stringify values (in logs).
17+
* @internal
18+
*/
19+
export const DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE = 300;
20+
1421
/**
1522
* Inspect options for output to console.
1623
* @internal
@@ -26,15 +33,9 @@ export const CONSOLE_INSPECT_OPTIONS = {
2633
*/
2734
export const MAX_ELEMENTS_COUNT_IN_PRINTED_ARRAY = 8;
2835

29-
/**
30-
* Max number of lines in printed stringify values (in logs).
31-
* @internal
32-
*/
33-
export const MAX_LINES_COUNT_IN_PRINTED_VALUE = 300;
34-
3536
/**
3637
* Max string length in printed stringify values (in logs).
3738
* @internal
3839
*/
3940
export const MAX_STRING_LENGTH_IN_PRINTED_VALUE =
40-
averageLineLength * MAX_LINES_COUNT_IN_PRINTED_VALUE;
41+
averageLineLength * DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE;

src/constants/internal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export {
3535
export {
3636
CONSOLE_INSPECT_OPTIONS,
3737
DEFAULT_INSPECT_OPTIONS,
38+
DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE,
3839
MAX_ELEMENTS_COUNT_IN_PRINTED_ARRAY,
39-
MAX_LINES_COUNT_IN_PRINTED_VALUE,
4040
MAX_STRING_LENGTH_IN_PRINTED_VALUE,
4141
} from './inspect';
4242
export {BACKEND_RESPONSES_LOG_MESSAGE, LogEventStatus, LogEventType} from './log';

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export type {
4545
Url,
4646
} from './http';
4747
export type {KeyboardPressKey} from './keyboard';
48-
export type {Log, LogContext, LogParams, LogPayload, LogTag} from './log';
48+
export type {Log, LogContext, LogParams, LogPayload, LogTag, ValueToStringOptions} from './log';
4949
export type {
5050
Dimensions,
5151
DimensionsString,

src/types/internal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export type {
7676
MapLogPayload,
7777
MapLogPayloadInReport,
7878
Payload,
79+
ValueToStringOptions,
7980
} from './log';
8081
export type {
8182
Dimensions,

src/types/log.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type {InspectOptions} from 'node:util';
2+
13
import type {LogEventStatus, LogEventType} from '../constants/internal';
24

35
import type {ResponseWithRequest} from './http';
@@ -32,6 +34,7 @@ export type LogPayload = Readonly<{
3234
filePath?: unknown;
3335
logEventStatus?: LogEventStatus;
3436
logTag?: LogTag;
37+
maxLinesCountInPrintedValue?: number;
3538
successful?: unknown;
3639
}> &
3740
Payload;
@@ -77,3 +80,8 @@ export type MapLogPayloadInReport = (
7780
* Object with some payload.
7881
*/
7982
export type Payload = Readonly<Record<string, unknown>>;
83+
84+
/**
85+
* Options of `valueToString` function.
86+
*/
87+
export type ValueToStringOptions = Readonly<InspectOptions & {maxLines?: number}>;

src/utils/events/registerStartE2edRunEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const registerStartE2edRunEvent = async (): Promise<void> => {
8181
const isLocalRun = runEnvironment !== RunEnvironment.Docker;
8282
const startMessage = `Run tests ${isLocalRun ? 'local' : 'in docker'} with e2ed@${e2ed.version}`;
8383

84-
generalLog(startMessage, startInfo);
84+
generalLog(startMessage, {...startInfo, maxLinesCountInPrintedValue: 1024});
8585
} finally {
8686
await writeStartInfo(startInfo);
8787
await writeLogsToFile();

src/utils/generalLog/getLogMessageBody.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {CONSOLE_INSPECT_OPTIONS} from '../../constants/internal';
1+
import {CONSOLE_INSPECT_OPTIONS, DEFAULT_INSPECT_OPTIONS} from '../../constants/internal';
22

33
import {valueToString} from '../valueToString';
44

5-
import type {LogContext, LogPayload} from '../../types/internal';
5+
import type {LogContext, LogPayload, ValueToStringOptions} from '../../types/internal';
66

77
/**
88
* Get body of log message by context, isLogInConsole flag and log payload.
@@ -20,5 +20,15 @@ export const getLogMessageBody = (
2020
return '';
2121
}
2222

23-
return ` ${valueToString(printedPayload, isLogInConsole ? CONSOLE_INSPECT_OPTIONS : undefined)}`;
23+
const maxLines = payload == null ? undefined : payload.maxLinesCountInPrintedValue;
24+
25+
let options: ValueToStringOptions = isLogInConsole
26+
? CONSOLE_INSPECT_OPTIONS
27+
: DEFAULT_INSPECT_OPTIONS;
28+
29+
if (maxLines !== undefined) {
30+
options = {...options, maxLines};
31+
}
32+
33+
return ` ${valueToString(printedPayload, options)}`;
2434
};

src/utils/valueToString/getLinesArrayTrimmedToMaxLength.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import {MAX_LINES_COUNT_IN_PRINTED_VALUE} from '../../constants/internal';
1+
import {DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE} from '../../constants/internal';
22

33
const additionalLinesForRepeatedTrimmerRuns = 4;
44

55
/**
66
* Get lines array trimmed to max lines count in printed value.
77
* @internal
88
*/
9-
export const getLinesArrayTrimmedToMaxLength = (lines: readonly string[]): readonly string[] => {
10-
if (lines.length <= MAX_LINES_COUNT_IN_PRINTED_VALUE + additionalLinesForRepeatedTrimmerRuns) {
9+
export const getLinesArrayTrimmedToMaxLength = (
10+
lines: readonly string[],
11+
maxLines = DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE,
12+
): readonly string[] => {
13+
if (lines.length <= maxLines + additionalLinesForRepeatedTrimmerRuns) {
1114
return lines;
1215
}
1316

14-
const halfOfLines = Math.floor(MAX_LINES_COUNT_IN_PRINTED_VALUE / 2);
17+
const halfOfLines = Math.floor(maxLines / 2);
1518
const cuttedLinesCount = lines.length - 2 * halfOfLines;
1619

1720
return [

src/utils/valueToString/valueToString.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1-
import {inspect, type InspectOptions} from 'node:util';
1+
import {inspect} from 'node:util';
22

3-
import {DEFAULT_INSPECT_OPTIONS, MAX_LINES_COUNT_IN_PRINTED_VALUE} from '../../constants/internal';
3+
import {
4+
DEFAULT_INSPECT_OPTIONS,
5+
DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE,
6+
} from '../../constants/internal';
47

58
import {cutVerboseLinesFromPrintedLines} from './cutVerboseLinesFromPrintedLines';
69
import {getLinesArrayTrimmedToMaxLength} from './getLinesArrayTrimmedToMaxLength';
710
import {getStringTrimmedToMaxLength} from './getStringTrimmedToMaxLength';
811

12+
import type {ValueToStringOptions} from '../../types/internal';
13+
914
/**
1015
* Returns string presentation of arbitrary value.
1116
*/
1217
export const valueToString = (
1318
value: unknown,
14-
options: InspectOptions = DEFAULT_INSPECT_OPTIONS,
19+
options: ValueToStringOptions = DEFAULT_INSPECT_OPTIONS,
1520
): string => {
21+
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE;
22+
23+
if (value instanceof Error) {
24+
for (const symbol of Object.getOwnPropertySymbols(value)) {
25+
// We remove symbol fields from Playwright error objects, since printing them creates gigantic logs.
26+
// @ts-expect-error: cannot set symbol to Error
27+
// eslint-disable-next-line no-param-reassign
28+
value[symbol] = {};
29+
}
30+
}
31+
1632
const valueAsString = inspect(value, options);
1733
const lines = valueAsString.split('\n');
1834

19-
if (lines.length <= MAX_LINES_COUNT_IN_PRINTED_VALUE) {
35+
if (lines.length <= maxLines) {
2036
return getStringTrimmedToMaxLength(valueAsString);
2137
}
2238

0 commit comments

Comments
 (0)