Skip to content

Commit

Permalink
Merge pull request #36336 from software-mansion-labs/@szymczak/logger
Browse files Browse the repository at this point in the history
[No QA] [TS migration] Migrate logger.js to typescript
  • Loading branch information
amyevans authored Mar 8, 2024
2 parents b91d53b + c250bdc commit f550e84
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions tests/e2e/utils/logger.js → tests/e2e/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable import/no-import-module-exports */
import fs from 'fs';
import path from 'path';
import _ from 'underscore';
import CONFIG from '../config';

const COLOR_DIM = '\x1b[2m';
Expand All @@ -12,7 +11,7 @@ const COLOR_GREEN = '\x1b[32m';

const getDateString = () => `[${Date()}] `;

const writeToLogFile = (...args) => {
const writeToLogFile = (...args: string[]) => {
if (!fs.existsSync(CONFIG.LOG_FILE)) {
// Check that the directory exists
const logDir = path.dirname(CONFIG.LOG_FILE);
Expand All @@ -24,45 +23,46 @@ const writeToLogFile = (...args) => {
}
fs.appendFileSync(
CONFIG.LOG_FILE,
`${_.map(args, (arg) => {
if (typeof arg === 'string') {
// Remove color codes from arg, because they are not supported in log files
// eslint-disable-next-line no-control-regex
return arg.replace(/\x1b\[\d+m/g, '');
}
return arg;
})
`${args
.map((arg) => {
if (typeof arg === 'string') {
// Remove color codes from arg, because they are not supported in log files
// eslint-disable-next-line no-control-regex
return arg.replace(/\x1b\[\d+m/g, '');
}
return arg;
})
.join(' ')
.trim()}\n`,
);
};

const log = (...args) => {
const log = (...args: string[]) => {
const argsWithTime = [getDateString(), ...args];
console.debug(...argsWithTime);
writeToLogFile(...argsWithTime);
};

const info = (...args) => {
const info = (...args: string[]) => {
log('▶️', ...args);
};

const success = (...args) => {
const success = (...args: string[]) => {
const lines = ['✅', COLOR_GREEN, ...args, COLOR_RESET];
log(...lines);
};

const warn = (...args) => {
const warn = (...args: string[]) => {
const lines = ['⚠️', COLOR_YELLOW, ...args, COLOR_RESET];
log(...lines);
};

const note = (...args) => {
const note = (...args: string[]) => {
const lines = [COLOR_DIM, ...args, COLOR_RESET];
log(...lines);
};

const error = (...args) => {
const error = (...args: string[]) => {
const lines = ['🔴', COLOR_RED, ...args, COLOR_RESET];
log(...lines);
};
Expand Down

0 comments on commit f550e84

Please sign in to comment.