Repository navigation
Expand file tree
/
Copy pathlogger.ts
More file actions
46 lines (44 loc) · 1.34 KB
/
Copy pathlogger.ts
File metadata and controls
46 lines (44 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* eslint-disable turbo/no-undeclared-env-vars */
import { default as chalk } from 'chalk';
const getRandomChalkColor = () => {
const colors = [
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'redBright',
'greenBright',
'yellowBright',
'blueBright',
'magentaBright',
'cyanBright',
];
return colors[Math.floor(Math.random() * colors.length)];
};
type CreateLoggerOptions = { prefix: string; color?: string };
export const createLogger = (opts: CreateLoggerOptions) => {
const { color, prefix } = opts;
const prefixColor = color || getRandomChalkColor();
return {
info: (msg: string) => {
if (
process.env.E2E_DEBUG === 'true' ||
process.env.E2E_DEBUG === '1' ||
process.env.ACTIONS_STEP_DEBUG ||
process.env.ACTIONS_RUNNER_DEBUG
) {
console.info(`${chalk[prefixColor](`[${prefix}]`)} ${msg}`);
}
},
warn: (msg: string, error?: unknown) => {
const errorMsg = error instanceof Error ? error.message : typeof error === 'string' ? error : '';
const fullMsg = errorMsg ? `${msg} ${errorMsg}` : msg;
console.warn(`${chalk.yellow(`[${prefix}]`)} ${fullMsg}`);
},
child: (childOpts: CreateLoggerOptions) => {
return createLogger({ prefix: `${prefix} :: ${childOpts.prefix}`, color: prefixColor });
},
};
};