Skip to content

Commit

Permalink
fix(logs): refactor to use printHeader from sfpLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
azlam-abdulsalam committed Aug 7, 2023
1 parent 60b4e9d commit 4291e91
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 132 deletions.
11 changes: 6 additions & 5 deletions packages/core/src/package/SfpPackageBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import PackageVersionUpdater from './version/PackageVersionUpdater';
import { AnalyzerRegistry } from './analyser/AnalyzerRegistry';
import { ComponentSet } from '@salesforce/source-deploy-retrieve';
import CreateDiffPackageImp from './packageCreators/CreateDiffPackageImpl';
import { COLOR_WARNING } from '@dxatscale/sfp-logger';

export default class SfpPackageBuilder {
public static async buildPackageFromProjectDirectory(
Expand Down Expand Up @@ -238,16 +239,16 @@ export default class SfpPackageBuilder {
sfpPackage.isApexFound == true &&
sfpPackage.apexTestClassses == null)
) {
SFPLogger.printHeaderLine('WARNING! NON OPTIMAL DEPLOYMENT',COLOR_WARNING,LoggerLevel.INFO,logger);
SFPLogger.log(
` ----------------------------------WARNING! NON OPTIMAL DEPLOYMENT--------------------------------------------${EOL}` +
`This package has apex classes/triggers, In order to deploy optimally, each class need to have a minimum ${EOL}` +
`75% test coverage,We are unable to find any test classes in the given package, hence will be deploying ${EOL}` +
`This package has apex classes/triggers, In order to deploy optimally, each class need to have a minimum` +
`75% test coverage,We are unable to find any test classes in the given package, hence will be deploying` +
`via triggering all local tests,This definitely is not optimal approach on large orgs` +
`Please consider adding test classes for the classes in the package ${EOL}` +
`-------------------------------------------------------------------------------------------------------------`,
`Please consider adding test classes for the classes in the package` +
LoggerLevel.INFO,
logger
);
SFPLogger.printHeaderLine('',COLOR_WARNING,LoggerLevel.INFO,logger);
return true;
} else return false;
}
Expand Down
23 changes: 5 additions & 18 deletions packages/core/src/package/packageCreators/CreatePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ export abstract class CreatePackage {
abstract isEmptyPackage(projectDirectory: string, packageDirectory: string);

protected printEmptyArtifactWarning() {
SFPLogger.log(
`${COLOR_WARNING(
`---------------------WARNING! Empty aritfact encountered-------------------------------`
)}`,
SFPLogger.printHeaderLine(
`WARNING! Empty aritfact encountered`,
COLOR_WARNING,
LoggerLevel.INFO,
this.logger
);
Expand All @@ -116,13 +115,7 @@ export abstract class CreatePackage {
this.logger
);
SFPLogger.log('Proceeding to create an empty artifact', LoggerLevel.INFO, this.logger);
SFPLogger.log(
`${COLOR_WARNING(
`---------------------------------------------------------------------------------------`
)}`,
LoggerLevel.INFO,
this.logger
);
SFPLogger.printHeaderLine('',COLOR_WARNING,LoggerLevel.INFO,this.logger);
}

private printHeader() {
Expand All @@ -146,13 +139,7 @@ export abstract class CreatePackage {

this.printAdditionalPackageSpecificHeaders();

SFPLogger.log(
`${COLOR_HEADER(
`-------------------------------------------------------------------------------------------`
)}`,
LoggerLevel.INFO,
this.logger
);
SFPLogger.printHeaderLine('',COLOR_HEADER,LoggerLevel.INFO,this.logger);
}

abstract printAdditionalPackageSpecificHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import SfpPackage, { PackageType, SfpPackageParams } from '../SfpPackage';
import { CreatePackage } from './CreatePackage';
import { PackageCreationParams } from '../SfpPackageBuilder';
import { ZERO_BORDER_TABLE } from '../../display/TableConstants';
import { COLOR_INFO } from '@dxatscale/sfp-logger';
import { COLOR_HEADER } from '@dxatscale/sfp-logger';
import { COLOR_WARNING } from '@dxatscale/sfp-logger';
const Table = require('cli-table');

export default class CreateSourcePackageImpl extends CreatePackage {
Expand Down Expand Up @@ -51,11 +54,7 @@ export default class CreateSourcePackageImpl extends CreatePackage {
sfpPackage.isTriggerAllTests = true;
} else if (sfpPackage.isApexFound && classTypes?.testClass?.length > 0) {
if (classTypes?.parseError?.length > 0) {
SFPLogger.log(
'---------------------------------------------------------------------------------------',
LoggerLevel.INFO,
this.logger
);
SFPLogger.printHeaderLine('',COLOR_HEADER,LoggerLevel.INFO,this.logger);
SFPLogger.log(
'Unable to parse these classes to correctly identify test classes, Its not your issue, its ours!'+
'Please raise a issue in our repo!',
Expand All @@ -77,43 +76,27 @@ export default class CreateSourcePackageImpl extends CreatePackage {
}

private printHintForOptimizedDeployment() {
SFPLogger.log(
`---------------- OPTION FOR DEPLOYMENT OPTIMIZATION AVAILABLE-----------------------------------`,
null,
this.logger
);
SFPLogger.printHeaderLine('OPTION FOR DEPLOYMENT OPTIMIZATION AVAILABLE',COLOR_HEADER,LoggerLevel.INFO,this.logger);
SFPLogger.log(
`Following apex test classes were identified and can be used for deploying this package,${EOL}` +
`in an optimal manner, provided each individual class meets the test coverage requirement of 75% and above${EOL}` +
`Ensure each apex class/trigger is validated for coverage in the validation stage`,
null,
this.logger
);
SFPLogger.log(
`-----------------------------------------------------------------------------------------------`,
LoggerLevel.INFO,
this.logger
);
SFPLogger.printHeaderLine('',COLOR_HEADER,LoggerLevel.INFO,this.logger);
}

private printSlowDeploymentWarning() {
SFPLogger.printHeaderLine('WARNING! YOU MIGHT NOT BE ABLE TO DEPLOY OR WILL HAVE A SLOW DEPLOYMENT',COLOR_WARNING,LoggerLevel.INFO,this.logger);
SFPLogger.log(
`-------WARNING! YOU MIGHT NOT BE ABLE TO DEPLOY OR WILL HAVE A SLOW DEPLOYMENT---------------`,
LoggerLevel.INFO,
this.logger
);
SFPLogger.log(
`This package has apex classes/triggers, however apex test classes were not found, You would not be able to deploy${EOL}` +
`to production org optimally if each class do not have coverage of 75% and above,We will attempt deploying${EOL}` +
`this package by triggering all local tests in the org which could be realy costly in terms of deployment time!${EOL}`,
`This package has apex classes/triggers, however apex test classes were not found, You would not be able to deploy` +
`to production org optimally if each class do not have coverage of 75% and above,We will attempt deploying` +
`this package by triggering all local tests in the org which could be realy costly in terms of deployment time!`,
null,
this.logger
);
SFPLogger.log(
`---------------------------------------------------------------------------------------------`,
LoggerLevel.INFO,
this.logger
);
SFPLogger.printHeaderLine('',COLOR_HEADER,LoggerLevel.INFO,this.logger);
}

private printClassesIdentified(fetchedClasses: FileDescriptor[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export default class InstallSourcePackageImpl extends InstallPackage {
have dependency to components in the manifest. \
Please check whether this manifest works! \
Acutal Error Observed: \
----------------------------------------------------------------------- \
-------------------------------------- \
${COLOR_ERROR(error.message)}`,
LoggerLevel.INFO,
this.logger
Expand Down
187 changes: 107 additions & 80 deletions packages/sfplogger/src/SFPLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@ 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,
TRACE = 10,
DEBUG = 20,
INFO = 30,
WARN = 40,
ERROR = 50,
FATAL = 60,
HIDE = 70,
}

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

export const COLOR_ERROR = chalk.bold.red;
Expand All @@ -52,67 +52,94 @@ 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;
public static isLogsDisabled: boolean = false;
public static logLevel: LoggerLevel = LoggerLevel.INFO;
public static isLogsDisabled: boolean = false;

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

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

static log(message: string, logLevel = LoggerLevel.INFO, logger?: Logger) {
if (SFPLogger.isLogsDisabled)
return;
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;
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 log(message: string, logLevel = LoggerLevel.INFO, logger?: Logger) {
if (SFPLogger.isLogsDisabled) return;
if (logLevel == null) logLevel = LoggerLevel.INFO;

if (logLevel < this.logLevel) return;

// Split message into lines of 90 characters
const maxLineLength = 120;
const lines = [];
for (let i = 0; i < message.length; i += maxLineLength) {
lines.push(message.substring(i, i + maxLineLength));
}

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

static disableLogs() {
SFPLogger.isLogsDisabled = true;
if (logger) {
if (logger.logType === LoggerType.void) {
return;
} else if (logger.logType === LoggerType.file) {
let fileLogger = logger as FileLogger;
lines.forEach(line => {
line = line.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
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;
}
});
}
}

static disableLogs() {
SFPLogger.isLogsDisabled = true;
}

static printHeaderLine(header, color: chalk.Chalk, logLevel, logger?: Logger) {
if (header == null)
header = '';
const lineLength = 90;
const leftPadLength = Math.floor((lineLength - header.length) / 2);
const rightPadLength = lineLength - leftPadLength - header.length;
const line = '-'.repeat(leftPadLength) + `${header}` + '-'.repeat(rightPadLength);
if (logger) {
if (logger.logType === LoggerType.void) {
return;
} else if (logger.logType === LoggerType.file) {
return;
}
else
console.log(color(line));
}
else
console.log(color(line));

}
}

0 comments on commit 4291e91

Please sign in to comment.