Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
fix(logs): remove pino to use simpler sfplogger, as this is not mixin…
Browse files Browse the repository at this point in the history
…g nicely with progrss bars (#633)

* fix(logs): remove pino to use simpler sfplogger, as this is not mixing nicely with progrss bars

* Remove pino

* Remove console.log statement
  • Loading branch information
azlam-abdulsalam authored Feb 14, 2022
1 parent 74d00a0 commit 473b5cc
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 44 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
"ignore": "^5.1.8",
"nested-object-diff": "^1.1.0",
"node-cache": "^5.1.2",
"pino": "^7.6.2",
"pino-pretty": "^7.3.0",
"request": "^2.88.2",
"request-promise-native": "^1.0.9",
"simple-git": "2.38.0",
Expand Down
47 changes: 5 additions & 42 deletions src/sfpowerkit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SfdxProject } from "@salesforce/core";
import { UX } from "@salesforce/command";
import chalk = require("chalk");
const Logger = require("pino");
import * as fs from "fs-extra";
//import pino from 'pino'
import SQLITEKeyValue from "./utils/sqlitekv"
import SFPLogger from "./utils/sfpLogger";
const NodeCache = require("node-cache");

export enum LoggerLevel {
Expand Down Expand Up @@ -83,27 +83,9 @@ export class SFPowerkit {


public static setLogLevel(logLevel: string, isJsonFormatEnabled: boolean) {
logLevel = logLevel.toLowerCase();
this.logLevel = LoggerLevel[logLevel.toUpperCase()];
this.logLevelString=logLevel;
this.isJsonFormatEnabled = isJsonFormatEnabled;
if (!isJsonFormatEnabled) {

SFPowerkit.logger = Logger({
name: "sfpowerkit",
level: logLevel,
transport: {
target: 'pino-pretty',
options: {
levelFirst: true, // --levelFirst
colorize: true,
translateTime: true,
ignore: "pid,hostname", // --ignore
}
}
});
} else {
//do nothing for now, need to put pino to move to file
}
this.isJsonFormatEnabled = isJsonFormatEnabled?true:false;
}

public static setProjectDirectories(packagedirectories: string[]) {
Expand Down Expand Up @@ -164,28 +146,9 @@ export class SFPowerkit {
* @param messageLoglevel Log level for the message
*/
public static log(message: any, logLevel: LoggerLevel) {
if (!this.logger) return;

if (this.isJsonFormatEnabled) return;
switch (logLevel) {
case LoggerLevel.TRACE:
this.logger.trace(message);
break;
case LoggerLevel.DEBUG:
this.logger.debug(message);
break;
case LoggerLevel.INFO:
this.logger.info(message);
break;
case LoggerLevel.WARN:
this.logger.warn(message);
break;
case LoggerLevel.ERROR:
this.logger.error(message);
break;
case LoggerLevel.FATAL:
this.logger.fatal(message);
break;
}
SFPLogger.log(message,logLevel);
}
public static setUx(ux: UX) {
this.ux = ux;
Expand Down
112 changes: 112 additions & 0 deletions src/utils/sfpLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import * as fs from "fs-extra";
import { EOL } from "os";
import chalk = require("chalk");
export enum LoggerLevel {
TRACE = 10,
DEBUG = 20,
INFO = 30,
WARN = 40,
ERROR = 50,
FATAL = 60,
HIDE = 70
}

const enum LoggerType {
console = 1,
file = 2,
void = 3,
}
export class ConsoleLogger implements Logger {
public logType: LoggerType;
constructor() {
this.logType = LoggerType.console;
}
}
export class VoidLogger implements Logger {
public logType: LoggerType;
constructor() {
this.logType = LoggerType.void;
}
}
export class FileLogger implements Logger {
public logType: LoggerType;
constructor(public path: string) {
this.logType = LoggerType.file;
}
}
export interface Logger {
logType: LoggerType;
path?: string;
}

export const COLOR_ERROR = chalk.bold.red;
export const COLOR_WARNING = chalk.keyword("orange");
export const COLOR_INFO = chalk.white;
export const COLOR_TRACE = chalk.gray;
export const COLOR_DEBUG = chalk.blue;
export const COLOR_HEADER = chalk.yellowBright.bold;
export const COLOR_SUCCESS = chalk.green.bold;
export const COLOR_TIME=chalk.magentaBright
export const COLOR_KEY_MESSAGE=chalk.magentaBright.bold
export const COLOR_KEY_VALUE=chalk.black.bold.bgGreenBright;

export default class SFPLogger {
public static logLevel: LoggerLevel = LoggerLevel.INFO;


static enableColor() {
chalk.level = 2;
}

static disableColor() {
chalk.level = 0;
}


static log(message: string, logLevel = LoggerLevel.INFO, logger?: Logger) {



if (logLevel == null) logLevel = LoggerLevel.INFO;

if (logLevel < this.logLevel) return;

//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;
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;
}
}
}
}

0 comments on commit 473b5cc

Please sign in to comment.