Skip to content

Commit 6a6a076

Browse files
authored
Merge pull request #6 from hmdoganjf/style/log-singleton
style: singleton logger created and replaced console logs
2 parents dafe1e4 + b22f415 commit 6a6a076

7 files changed

Lines changed: 52 additions & 42 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@jotforminc/zenith",
33
"packageManager": "pnpm@7.18.0",
4-
"version": "0.4.0",
4+
"version": "0.4.1",
55
"description": "",
66
"main": "./build/index.js",
77
"files": [

src/classes/BuildHelper.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Hasher from './Hasher';
66
import WorkerHelper from './WorkerHelper';
77
import ConfigHelper from './ConfigHelper';
88
import { formatMissingProjects, formatTimeDiff } from '../utils/functions';
9+
import Logger from '../utils/logger'
910

1011
export default class BuildHelper extends WorkerHelper {
1112
projects = new Map();
@@ -18,11 +19,9 @@ export default class BuildHelper extends WorkerHelper {
1819
super(command);
1920
}
2021

21-
async init(debug, compareWith, logLevel, logFunction) {
22+
async init(debug, compareWith) {
2223
this.cacher = new Cacher().cacher;
23-
this.logLevel = logLevel;
2424
this.startTime = process.hrtime();
25-
this.log = logFunction(this.logLevel)
2625
if (debug) {
2726
this.debug = debug;
2827
this.compareWith = compareWith;
@@ -43,7 +42,7 @@ export default class BuildHelper extends WorkerHelper {
4342
});
4443
} catch (error) {
4544
if (error.code === 'ENOENT') {
46-
this.log(2, 'Package.json file not found in the project!');
45+
Logger.log(2, 'Package.json file not found in the project!');
4746
throw error;
4847
} else {
4948
throw error;
@@ -94,12 +93,12 @@ export default class BuildHelper extends WorkerHelper {
9493
if (this.compareWith) {
9594
const [changedFiles, newFiles] = Hasher.getUpdatedHashes();
9695
if (changedFiles.length || newFiles.length) {
97-
this.log(3, `Hash mismatched: \n Changed files => \n - ${changedFiles.join('\n')} \n New files => \n - ${newFiles.join('\n')}`);
96+
Logger.log(3, `Hash mismatched: \n Changed files => \n - ${changedFiles.join('\n')} \n New files => \n - ${newFiles.join('\n')}`);
9897
Hasher.emptyUpdatedHashes();
9998
}
10099
}
101100
if (!isCached) {
102-
this.log(3, 'Cache does not exist for => ', buildProject, hash);
101+
Logger.log(3, 'Cache does not exist for => ', buildProject, hash);
103102

104103
const startTime = process.hrtime();
105104
const output = await this.execute(buildPath, script, hash, root, outputs);
@@ -113,7 +112,7 @@ export default class BuildHelper extends WorkerHelper {
113112
if (outputs.length) {
114113
for (const output of outputs) {
115114
// const outputPath = path.join(ROOT_PATH, root, output);
116-
this.log(3, 'Recovering from cache', buildProject, 'with hash => ', hash);
115+
Logger.log(3, 'Recovering from cache', buildProject, 'with hash => ', hash);
117116
const recoverResponse = await this.anotherJob(hash, root, output);
118117
if (recoverResponse instanceof Error) {
119118
throw new Error(recoverResponse);
@@ -138,7 +137,7 @@ export default class BuildHelper extends WorkerHelper {
138137
if (!projects.length) {
139138
if (!stats.pendingTasks && !stats.activeTasks) {
140139
this.pool.terminate();
141-
this.log(2, `
140+
Logger.log(2, `
142141
▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄ ▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄ ▄
143142
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌ ▐░░▌▐░░░░░░░░░░░▌▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▐░▌
144143
▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌░▌ ▐░▐░▌▐░█▀▀▀▀▀▀▀█░▌▐░▌ ▐░█▀▀▀▀▀▀▀▀▀ ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌
@@ -151,14 +150,14 @@ export default class BuildHelper extends WorkerHelper {
151150
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▐░▌
152151
▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀
153152
`);
154-
this.log(2, `Total of ${this.totalCount} project${this.totalCount === 1 ? '' : 's'} has built.`);
155-
this.log(2, `${this.fromCache} project built from cache,`);
156-
this.log(2, `${this.built} project built without cache.`);
157-
this.log(2, `Cache is missing for following projects => ${formatMissingProjects(this.missingProjects)}`);
158-
this.log(2, `Total build took ${formatTimeDiff(process.hrtime(this.startTime))}.`);
153+
Logger.log(2, `Total of ${this.totalCount} project${this.totalCount === 1 ? '' : 's'} has built.`);
154+
Logger.log(2, `${this.fromCache} project built from cache,`);
155+
Logger.log(2, `${this.built} project built without cache.`);
156+
Logger.log(2, `Cache is missing for following projects => ${formatMissingProjects(this.missingProjects)}`);
157+
Logger.log(2, `Total build took ${formatTimeDiff(process.hrtime(this.startTime))}.`);
159158
if (this.debug && process.env.ZENITH_DEBUG_ID) {
160159
this.cacher.updateDebugFile(Hasher.getDebugJSON());
161-
this.log(2, 'DEBUG JSON UPDATED');
160+
Logger.log(2, 'DEBUG JSON UPDATED');
162161
}
163162
}
164163
return;

src/classes/RemoteCacher.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { S3 } from '@aws-sdk/client-s3';
44
import zipper from 'zip-local';
55
import unzipper from 'unzipper';
66
import { ROOT_PATH } from '../utils/constants';
7+
import Logger from '../utils/logger'
78
import Hasher from './Hasher';
89

910
class RemoteCacher {
@@ -25,7 +26,7 @@ class RemoteCacher {
2526
const debugFileString = await response.Body.transformToString();
2627
return JSON.parse(debugFileString);
2728
} catch (error) {
28-
console.log(error);
29+
Logger.log(2, error);
2930
}
3031
}
3132
}
@@ -35,9 +36,9 @@ class RemoteCacher {
3536
const debugBuff = Buffer.from(JSON.stringify(debugJSON));
3637
this.s3Client.putObject({ Bucket: process.env.S3_BUCKET_NAME, Key: `frontend-build/debug/debug.${process.env.ZENITH_DEBUG_ID}.json`, Body: debugBuff }, err => {
3738
if (err) {
38-
console.log(err);
39+
Logger.log(2, err);
3940
}
40-
console.log('Cache successfully stored');
41+
Logger.log(3, 'Cache successfully stored');
4142
});
4243
}
4344

@@ -55,14 +56,14 @@ class RemoteCacher {
5556
const outputBuff = Buffer.from(outputHash);
5657
this.s3Client.putObject({ Bucket: process.env.S3_BUCKET_NAME, Key: `${cachePath}/${output}.txt`, Body: outputBuff }, err => {
5758
if (err) {
58-
console.log(err);
59+
Logger.log(2, err);
5960
reject(err)
6061
}
61-
console.log('Cache successfully stored')
62+
Logger.log(3, 'Cache successfully stored')
6263
resolve();
6364
});
6465
} catch (error) {
65-
console.log(error);
66+
Logger.log(2, error);
6667
}
6768
})
6869
}
@@ -83,18 +84,18 @@ class RemoteCacher {
8384
const buff = zipped.memory();
8485
this.s3Client.putObject({ Bucket: process.env.S3_BUCKET_NAME, Key: `${cachePath}/${output}.zip`, Body: buff }, err => {
8586
if (err) {
86-
console.log(err);
87+
Logger.log(2, err);
8788
reject(err)
8889
}
89-
console.log('Cache successfully stored')
90+
Logger.log(3, 'Cache successfully stored')
9091
resolve();
9192
});
9293
} else {
93-
console.log('ERROR => ', error);
94+
Logger.log(2, 'ERROR => ', error);
9495
}
9596
})
9697
} catch (error) {
97-
console.log(error);
98+
Logger.log(2, error);
9899
reject(error);
99100
}
100101
})
@@ -121,7 +122,7 @@ class RemoteCacher {
121122
const hash = await this.pipeEnd(response.Body, outputPath);
122123
return hash;
123124
} catch (error) {
124-
console.log(error);
125+
Logger.log(2, error);
125126
}
126127
}
127128

@@ -132,7 +133,7 @@ class RemoteCacher {
132133
const remoteHash = await response.Body.transformToString();
133134
return remoteHash;
134135
} catch (error) {
135-
console.log(error);
136+
Logger.log(2, error);
136137
}
137138
}
138139

src/classes/Runner.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Command } from 'commander';
22
import BuildHelper from './BuildHelper';
3+
import Logger from '../utils/logger'
34

45
export default class Runner {
56
constructor(...args) {
@@ -30,23 +31,16 @@ export default class Runner {
3031
if (flags['--compareWith']) {
3132
this.compareWith = flags['--compareWith'];
3233
}
33-
if (options.logLevel) {
34-
this.logLevel = Number(options.logLevel);
35-
}
36-
37-
this.logFunction = logLevel => (level, ...args) => {
38-
if (logLevel >= level) console.log(...args);
39-
}
40-
this.log = this.logFunction(this.logLevel)
34+
Logger.setLogLevel(Number(options.logLevel));
4135
}
4236

4337
async run() {
4438
if (this.command !== 'build') {
4539
return;
4640
}
4741
const Builder = new BuildHelper('build');
48-
await Builder.init(this.debug, this.compareWith, this.logLevel, this.logFunction);
49-
this.log(2, 'Zenith started. Building...')
42+
await Builder.init(this.debug, this.compareWith);
43+
Logger.log(2, 'Zenith started. Building...')
5044
if (this.project === 'all') {
5145
Builder.buildAll();
5246
} else {

src/classes/WorkerHelper.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import workerpool from 'workerpool';
22
import ConfigHelper from './ConfigHelper';
3+
import Logger from '../utils/logger'
34

45
export default class WorkerHelper {
56
started = new Set();
@@ -12,21 +13,23 @@ export default class WorkerHelper {
1213
async execute(buildPath, command, hash, root, outputs) {
1314
try {
1415
return await this.pool.exec('execute', [buildPath, command, hash, root, outputs], {
15-
on: message => this.log(3, message)
16+
on: message => Logger.log(3, message)
1617
})
1718
} catch (error) {
18-
console.log(error);
19+
console.log('execute msg in')
20+
Logger.log(2, error);
1921
throw error;
2022
}
2123
}
2224

2325
async anotherJob(hash, root, output) {
2426
try {
2527
return await this.pool.exec('anotherJob', [hash, root, output], {
26-
on: message => this.log(3, message)
28+
on: message => Logger.log(3, message)
2729
});
2830
} catch (error) {
29-
console.log(error);
31+
console.log('another job msg in')
32+
Logger.log(2, error);
3033
throw error;
3134
}
3235
}

src/utils/logger.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Logger {
2+
setLogLevel(level) {
3+
this.logLevel = level;
4+
}
5+
6+
log(level, ...args) {
7+
if (this.logLevel >= level) console.log(...args);
8+
}
9+
}
10+
11+
const singleton = new Logger();
12+
export default singleton;

src/worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import workerpool from 'workerpool';
22
import { execSync } from 'child_process';
33
import RemoteCacher from './classes/RemoteCacher';
4+
import Logger from './utils/logger'
45

56
const execute = async (buildPath, targetCommand, hash, root, outputs) => {
67
try {
@@ -25,7 +26,7 @@ const anotherJob = async (hash, root, output) => {
2526
workerpool.workerEmit(outputHash === remoteHash ? `Hash hit for ${root}` : `Hashes mismatched for ${root}, ${outputHash} !== ${remoteHash}`);
2627
return remoteHash === outputHash;
2728
} catch (error) {
28-
console.log(error, 'IN RECOVERING CACHE');
29+
Logger.log(2, error, 'IN RECOVERING CACHE');
2930
return error;
3031
}
3132
}

0 commit comments

Comments
 (0)