-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
54 lines (47 loc) · 1.79 KB
/
utils.js
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
47
48
49
50
51
52
53
54
import process from 'node:process';
/**
* Array of log levels in order of severity.
* @type {string[]}
*/
export const logLevels = ['verbose', 'info', 'warning', 'error'];
/**
* Regular expressions to filter log messages based on their content.
* @type {Object.<string, RegExp>}
*/
const filters = {
[logLevels[0]]: new RegExp(process.env.REGEXP_VERBOSE || '^$', 'g'),
[logLevels[1]]: new RegExp(process.env.REGEXP_INFO || '^$', 'g'),
[logLevels[2]]: new RegExp(process.env.REGEXP_WARNING || '^$', 'g'),
[logLevels[3]]: new RegExp(process.env.REGEXP_ERROR || '^$', 'g'),
};
/**
* Minimum log level to capture.
* @type {string}
* @default 'verbose'
*/
const minLogLevel = process.env.MIN_LOG_LEVEL || logLevels[0];
/**
* Maximum log level to allow before failing the action.
* @type {string}
* @default 'info'
*/
const maxLogLevel = process.env.MAX_LOG_LEVEL || logLevels[1];
/**
* Filters out parts of the message based on the regular expression.
* @param {string} level - The log level of the message.
* @param {string} message - The log message content.
* @returns {string} - The filtered message.
*/
export const filterMessage = (level, message) => message.replace(filters[level], '').trim();
/**
* Determines if a log message should be captured based on its level.
* @param {string} level - The log level of the message.
* @returns {boolean} - True if the message should be captured, false otherwise.
*/
export const shouldCapture = level => logLevels.indexOf(level) >= logLevels.indexOf(minLogLevel);
/**
* Determines if the action should fail based on the log level.
* @param {string} level - The log level of the message.
* @returns {boolean} - True if the action should fail, false otherwise.
*/
export const shouldFail = level => logLevels.indexOf(level) > logLevels.indexOf(maxLogLevel);