Skip to content

Commit

Permalink
fix(sfplogger): revert incorrect splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
azlam-abdulsalam committed Aug 11, 2023
1 parent 8c02198 commit 53cb98f
Showing 1 changed file with 45 additions and 62 deletions.
107 changes: 45 additions & 62 deletions packages/sfplogger/src/SFPLogger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as fs from 'fs-extra';
import { EOL } from 'os';
import chalk from 'chalk';
import stripAnsi = require('strip-ansi');

import chalk = require('chalk');

export enum LoggerLevel {
TRACE = 10,
Expand Down Expand Up @@ -66,68 +64,53 @@ export default class SFPLogger {
}

static log(message: string, logLevel = LoggerLevel.INFO, logger?: Logger) {
if (SFPLogger.isLogsDisabled)
return;
if (logLevel == null) logLevel = LoggerLevel.INFO;

if (logLevel < this.logLevel) return;

if (SFPLogger.isLogsDisabled) return;
if (logLevel == null) logLevel = LoggerLevel.INFO;

if (logLevel < this.logLevel) return;

const maxLineLength = 100;
const originalLines = message.split('\n');
const lines = [];

originalLines.forEach(line => {
while (stripAnsi(line).length > maxLineLength) {
let subLine = line.substring(0, maxLineLength);
line = line.substring(maxLineLength);
lines.push(subLine);
}
lines.push(line);
});

//Todo: Proper fix
if (logger && logger.logType === LoggerType.console) {
logger = null; // Make it nullable, so it goes to console
}

if (logger) {
if (logger.logType === LoggerType.void) {
return;
} else if (logger.logType === LoggerType.file) {
let fileLogger = logger as FileLogger;
lines.forEach(line => {
line = stripAnsi(line);
fs.appendFileSync(fileLogger.path, line + EOL, 'utf8');
});
}
} else {
lines.forEach(line => {
switch (logLevel) {
case LoggerLevel.TRACE:
console.log(COLOR_TRACE(line));
break;

case LoggerLevel.DEBUG:
console.log(COLOR_DEBUG(line));
break;

case LoggerLevel.INFO:
console.log(line);
break;

case LoggerLevel.WARN:
console.log(COLOR_WARNING(line));
break;

case LoggerLevel.ERROR:
console.log(COLOR_ERROR(line));
break;
//Todo: Proper fix
if (logger && logger.logType === LoggerType.console) {
logger = null; //Make it nullable, so it goes to console
}
});
}
}

if (logger) {
if (logger.logType === LoggerType.void) {
return;
} else if (logger.logType === LoggerType.file) {
let fileLogger = logger as FileLogger;
message = message
?.toString()
.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
fs.appendFileSync(fileLogger.path, message + EOL, 'utf8');
}
} else {
switch (logLevel) {
case LoggerLevel.TRACE:
console.log(COLOR_TRACE(message));
break;

case LoggerLevel.DEBUG:
console.log(COLOR_DEBUG(message));
break;

case LoggerLevel.INFO:
console.log(message);
break;

case LoggerLevel.WARN:
console.log(COLOR_WARNING(message));
break;

case LoggerLevel.ERROR:
console.log(COLOR_ERROR(message));
break;
}

}

}
static disableLogs() {
SFPLogger.isLogsDisabled = true;
}
Expand All @@ -152,4 +135,4 @@ export default class SFPLogger {
console.log(color(line));

}
}
}

0 comments on commit 53cb98f

Please sign in to comment.