Skip to content

Commit b3de4c6

Browse files
committed
feat(logs): Refactor logs for patternlab.js
closes #566
1 parent f6a7e59 commit b3de4c6

File tree

2 files changed

+70
-79
lines changed

2 files changed

+70
-79
lines changed

core/lib/patternlab.js

Lines changed: 69 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ class PatternLab {
4848
constructor(config) {
4949
// Either use the config we were passed, or load one up from the config file ourselves
5050
this.config = config || fs.readJSONSync(path.resolve(__dirname, '../../patternlab-config.json'));
51+
52+
//register our log events
53+
this.registerLogger(config.debug);
54+
55+
logger.info(`Pattern Lab Node v${packageInfo.version}`);
56+
5157
// Load up engines please
5258
this.engines = patternEngines;
5359
this.engines.loadAllEngines(config);
@@ -58,9 +64,6 @@ class PatternLab {
5864
// Make ye olde event emitter
5965
this.events = new PatternLabEventEmitter();
6066

61-
//register our log events
62-
this.registerLogger(config.debug);
63-
6467
// Make a place for the pattern graph to sit
6568
this.graph = null;
6669

@@ -103,7 +106,7 @@ class PatternLab {
103106
this.patternSectionSubType = fs.readFileSync(path.resolve(paths.source.patternlabFiles.patternSectionSubtype), 'utf8');
104107
this.viewAll = fs.readFileSync(path.resolve(paths.source.patternlabFiles.viewall), 'utf8');
105108
} catch (ex) {
106-
console.log(ex);
109+
logger.info(ex);
107110
logger.error('\nERROR: missing an essential file from ' + paths.source.patternlabFiles + '. Pattern Lab won\'t work without this file.\n');
108111

109112
// GTP: it seems increasingly naughty as we refactor to just unilaterally do this here,
@@ -129,9 +132,7 @@ class PatternLab {
129132

130133
setCacheBust() {
131134
if (this.config.cacheBust) {
132-
if (this.config.debug) {
133-
console.log('setting cacheBuster value for frontend assets.');
134-
}
135+
logger.debug('setting cacheBuster value for frontend assets.');
135136
this.cacheBuster = new Date().getTime();
136137
} else {
137138
this.cacheBuster = 0;
@@ -168,8 +169,8 @@ class PatternLab {
168169
this.userHead = headPattern.extendedTemplate;
169170
}
170171
catch (ex) {
171-
logger.error('\nWARNING: Could not find the user-editable header template, currently configured to be at ' + path.join(this.config.paths.source.meta, '_00-head.mustache') + '. Your configured path may be incorrect (check this.config.paths.source.meta in your config file), the file may have been deleted, or it may have been left in the wrong place during a migration or update.\n');
172-
if (this.config.debug) { console.log(ex); }
172+
logger.warning(`Could not find the user-editable header template, currently configured to be at ${path.join(this.config.paths.source.meta, '_00-head.ext')}. Your configured path may be incorrect (check this.config.paths.source.meta in your config file), the file may have been deleted, or it may have been left in the wrong place during a migration or update.`);
173+
logger.warning(ex);
173174

174175
// GTP: it seems increasingly naughty as we refactor to just unilaterally do this here,
175176
// but whatever. For now.
@@ -191,8 +192,8 @@ class PatternLab {
191192
this.userFoot = footPattern.extendedTemplate;
192193
}
193194
catch (ex) {
194-
logger.error('\nWARNING: Could not find the user-editable footer template, currently configured to be at ' + path.join(this.config.paths.source.meta, '_01-foot.mustache') + '. Your configured path may be incorrect (check this.config.paths.source.meta in your config file), the file may have been deleted, or it may have been left in the wrong place during a migration or update.\n');
195-
if (this.config.debug) { console.log(ex); }
195+
logger.error(`Could not find the user-editable footer template, currently configured to be at ${path.join(this.config.paths.source.meta, '_01-foot.ext')}. Your configured path may be incorrect (check this.config.paths.source.meta in your config file), the file may have been deleted, or it may have been left in the wrong place during a migration or update.`);
196+
logger.warning(ex);
196197

197198
// GTP: it seems increasingly naughty as we refactor to just unilaterally do this here,
198199
// but whatever. For now.
@@ -207,7 +208,7 @@ class PatternLab {
207208
return this.package.version;
208209
}
209210
logVersion() {
210-
console.log(this.package.version);
211+
logger.info(this.package.version);
211212
}
212213
getSupportedTemplateExtensions() {
213214
return this.engines.getSupportedFileExtensions();
@@ -258,22 +259,22 @@ class PatternLab {
258259
// handle the legacy value, which is boolean
259260
if (typeof logLevel === 'boolean') {
260261
if (logLevel) {
261-
logger.log.on('debug', msg => console.log(msg));
262+
logger.log.on('debug', msg => console.info(msg));
262263
}
263-
logger.log.on('info', msg => console.log(msg));
264-
logger.log.on('warning', msg => console.log(msg));
265-
logger.log.on('error', msg => console.log(msg));
264+
logger.log.on('info', msg => console.info(msg));
265+
logger.log.on('warning', msg => console.info(msg));
266+
logger.log.on('error', msg => console.info(msg));
266267
} else {
267268
if ( logLevel === 'quiet') { return; }
268269
switch (logLevel) {
269270
case 'debug':
270-
logger.log.on('debug', msg => console.log(msg));
271+
logger.log.on('debug', msg => console.info(msg));
271272
case 'info':
272-
logger.log.on('info', msg => console.log(msg));
273+
logger.log.on('info', msg => console.info(msg));
273274
case 'warning':
274-
logger.log.on('warning', msg => console.log(msg));
275+
logger.log.on('warning', msg => console.info(msg));
275276
case 'error':
276-
logger.log.on('error', msg => console.log(msg));
277+
logger.log.on('error', msg => console.info(msg));
277278
}
278279
}
279280
}
@@ -305,7 +306,7 @@ function processAllPatternsIterative(patterns_dir, patternlab) {
305306
(err, file) => {
306307
//log any errors
307308
if (err) {
308-
console.log('error in processAllPatternsIterative():', err);
309+
logger.info('error in processAllPatternsIterative():', err);
309310
return;
310311
}
311312

@@ -341,7 +342,7 @@ function processAllPatternsRecursive(patterns_dir, patternlab) {
341342
function (err, file) {
342343
//log any errors
343344
if (err) {
344-
console.log(err);
345+
logger.info(err);
345346
return;
346347
}
347348
pattern_assembler.process_pattern_recursive(path.relative(patterns_dir, file), patternlab);
@@ -361,7 +362,7 @@ function checkConfiguration(patternlab) {
361362
if (!patternlab.config.outputFileSuffixes) {
362363
logger.warning('');
363364
logger.warning('Configuration key [outputFileSuffixes] not found, and defaulted to the following:');
364-
console.log(outputFileSuffixes);
365+
logger.info(outputFileSuffixes);
365366
logger.warning('Since Pattern Lab Node Core 2.3.0 this configuration option is required. Suggest you add it to your patternlab-config.json file.');
366367
logger.warning('');
367368
}
@@ -402,10 +403,8 @@ function initializePlugins(patternlab) {
402403

403404
const pluginKey = foundPlugins[i];
404405

405-
if (patternlab.config.debug) {
406-
console.log('Found plugin: ', pluginKey);
407-
console.log('Attempting to load and initialize plugin.');
408-
}
406+
logger.info(`Found plugin: ${pluginKey}`);
407+
logger.info(`Attempting to load and initialize plugin.`);
409408

410409
const plugin = plugin_manager.load_plugin(pluginKey);
411410
plugin(patternlab);
@@ -437,56 +436,56 @@ const patternlab_engine = function (config) {
437436

438437
function help() {
439438

440-
console.log('');
439+
logger.info('');
441440

442-
console.log('|=======================================|');
441+
logger.info('|=======================================|');
443442
logger.debug(' Pattern Lab Node Help v' + patternlab.package.version);
444-
console.log('|=======================================|');
443+
logger.info('|=======================================|');
445444

446-
console.log('');
447-
console.log('Command Line Interface - usually consumed by an edition');
448-
console.log('');
445+
logger.info('');
446+
logger.info('API - usually consumed by an edition');
447+
logger.info('');
449448

450449
logger.debug(' patternlab:build');
451-
console.log(' > Compiles the patterns and frontend, outputting to config.paths.public');
452-
console.log('');
450+
logger.info(' > Compiles the patterns and frontend, outputting to config.paths.public');
451+
logger.info('');
453452

454453
logger.debug(' patternlab:patternsonly');
455-
console.log(' > Compiles the patterns only, outputting to config.paths.public');
456-
console.log('');
454+
logger.info(' > Compiles the patterns only, outputting to config.paths.public');
455+
logger.info('');
457456

458457
logger.debug(' patternlab:version');
459-
console.log(' > Return the version of patternlab-node you have installed');
460-
console.log('');
458+
logger.info(' > Return the version of patternlab-node you have installed');
459+
logger.info('');
461460

462461
logger.debug(' patternlab:help');
463-
console.log(' > Get more information about patternlab-node, pattern lab in general, and where to report issues.');
464-
console.log('');
462+
logger.info(' > Get more information about patternlab-node, pattern lab in general, and where to report issues.');
463+
logger.info('');
465464

466465
logger.debug(' patternlab:liststarterkits');
467-
console.log(' > Returns a url with the list of available starterkits hosted on the Pattern Lab organization Github account');
468-
console.log('');
466+
logger.info(' > Returns a url with the list of available starterkits hosted on the Pattern Lab organization Github account');
467+
logger.info('');
469468

470469
logger.debug(' patternlab:loadstarterkit');
471-
console.log(' > Load a starterkit into config.paths.source/*');
472-
console.log(' > NOTE: Overwrites existing content, and only cleans out existing directory if --clean=true argument is passed.');
473-
console.log(' > NOTE: In most cases, `npm install starterkit-name` will precede this call.');
474-
console.log(' > arguments:');
475-
console.log(' -- kit ');
476-
console.log(' > the name of the starter kit to load');
477-
console.log(' -- clean ');
478-
console.log(' > removes all files from config.paths.source/ prior to load');
479-
console.log(' > example (gulp):');
480-
console.log(' `gulp patternlab:loadstarterkit --kit=starterkit-mustache-demo`');
481-
console.log('');
482-
483-
console.log('===============================');
484-
console.log('');
485-
console.log('Visit http://patternlab.io/ for more info about Pattern Lab');
486-
console.log('Visit https://github.com/pattern-lab/patternlab-node/issues to open an issue.');
487-
console.log('Visit https://github.com/pattern-lab/patternlab-node/wiki to view the changelog, roadmap, and other info.');
488-
console.log('');
489-
console.log('===============================');
470+
logger.info(' > Load a starterkit into config.paths.source/*');
471+
logger.info(' > NOTE: Overwrites existing content, and only cleans out existing directory if --clean=true argument is passed.');
472+
logger.info(' > NOTE: In most cases, `npm install starterkit-name` will precede this call.');
473+
logger.info(' > arguments:');
474+
logger.info(' -- kit ');
475+
logger.info(' > the name of the starter kit to load');
476+
logger.info(' -- clean ');
477+
logger.info(' > removes all files from config.paths.source/ prior to load');
478+
logger.info(' > example (gulp):');
479+
logger.info(' `gulp patternlab:loadstarterkit --kit=starterkit-mustache-demo`');
480+
logger.info('');
481+
482+
logger.info('===============================');
483+
logger.info('');
484+
logger.info('Visit http://patternlab.io/ for more info about Pattern Lab');
485+
logger.info('Visit https://github.com/pattern-lab/patternlab-node/issues to open an issue.');
486+
logger.info('Visit https://github.com/pattern-lab/patternlab-node/wiki to view the changelog, roadmap, and other info.');
487+
logger.info('');
488+
logger.info('===============================');
490489
}
491490

492491
function renderSinglePattern(pattern, head) {
@@ -512,8 +511,8 @@ const patternlab_engine = function (config) {
512511
try {
513512
allData = jsonCopy(patternlab.data, 'config.paths.source.data global data');
514513
} catch (err) {
515-
console.log('There was an error parsing JSON for ' + pattern.relPath);
516-
console.log(err);
514+
logger.info('There was an error parsing JSON for ' + pattern.relPath);
515+
logger.info(err);
517516
}
518517
allData = _.merge(allData, pattern.jsonFileData);
519518
allData.cacheBuster = patternlab.cacheBuster;
@@ -570,8 +569,8 @@ const patternlab_engine = function (config) {
570569
try {
571570
allFooterData = jsonCopy(patternlab.data, 'config.paths.source.data global data');
572571
} catch (err) {
573-
console.log('There was an error parsing JSON for ' + pattern.relPath);
574-
console.log(err);
572+
logger.info('There was an error parsing JSON for ' + pattern.relPath);
573+
logger.info(err);
575574
}
576575
allFooterData = _.merge(allFooterData, pattern.jsonFileData);
577576
allFooterData.patternLabFoot = footerPartial;
@@ -587,7 +586,7 @@ const patternlab_engine = function (config) {
587586

588587
// Allows serializing the compile state
589588
patternlab.graph.node(pattern).compileState = pattern.compileState = CompileState.CLEAN;
590-
logger.log.info("Built pattern: " + pattern.patternPartial);
589+
logger.info("Built pattern: " + pattern.patternPartial);
591590

592591
return Promise.resolve(true);
593592
}
@@ -624,14 +623,6 @@ const patternlab_engine = function (config) {
624623
function buildPatterns(deletePatternDir) {
625624
patternlab.events.emit('patternlab-build-pattern-start', patternlab);
626625

627-
if (patternlab.config.debug) {
628-
console.log(
629-
chalk.bold('\n====[ Pattern Lab / Node'),
630-
`- v${packageInfo.version}`,
631-
chalk.bold(']====\n')
632-
);
633-
}
634-
635626
//
636627
// CHECK INCREMENTAL BUILD GRAPH
637628
//
@@ -733,7 +724,7 @@ const patternlab_engine = function (config) {
733724
pattern_exporter.export_patterns(patternlab);
734725
});
735726
}).catch((err) => {
736-
console.log('Error in buildPatterns():', err);
727+
logger.info('Error in buildPatterns():', err);
737728
});
738729
}
739730

@@ -765,7 +756,7 @@ const patternlab_engine = function (config) {
765756
*/
766757
build: function (callback, options) {
767758
if (patternlab && patternlab.isBusy) {
768-
console.log('Pattern Lab is busy building a previous run - returning early.');
759+
logger.info('Pattern Lab is busy building a previous run - returning early.');
769760
return Promise.resolve();
770761
}
771762
patternlab.isBusy = true;
@@ -813,7 +804,7 @@ const patternlab_engine = function (config) {
813804
*/
814805
patternsonly: function (callback, options) {
815806
if (patternlab && patternlab.isBusy) {
816-
console.log('Pattern Lab is busy building a previous run - returning early.');
807+
logger.info('Pattern Lab is busy building a previous run - returning early.');
817808
return Promise.resolve();
818809
}
819810
patternlab.isBusy = true;

patternlab-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"defaultPattern": "all",
3030
"ignored-extensions" : ["scss", "DS_Store", "less"],
3131
"ignored-directories" : ["scss"],
32-
"debug": false,
32+
"debug": "info",
3333
"ishControlsHide": {
3434
"s": false,
3535
"m": false,

0 commit comments

Comments
 (0)