Skip to content

Commit fca4d03

Browse files
Merge pull request #743 from pattern-lab/566-logs
feat(logs): Improve logging
2 parents 2775814 + 1a1ef62 commit fca4d03

29 files changed

+416
-504
lines changed

core/lib/annotation_exporter.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const glob = require('glob');
44
const fs = require('fs-extra');
55
const _ = require('lodash');
66
const mp = require('./markdown_parser');
7+
const logger = require('./log');
78

89
const annotations_exporter = function (pl) {
910

@@ -18,9 +19,7 @@ const annotations_exporter = function (pl) {
1819
try {
1920
oldAnnotations = fs.readFileSync(path.resolve(paths.source.annotations, 'annotations.js'), 'utf8');
2021
} catch (ex) {
21-
if (pl.config.debug) {
22-
console.log('annotations.js file missing from ' + paths.source.annotations + '. This may be expected.');
23-
}
22+
logger.debug(`annotations.js file missing from ${paths.source.annotations}. This may be expected if you do not use annotations or are using markdown.`);
2423
return [];
2524
}
2625

@@ -31,8 +30,7 @@ const annotations_exporter = function (pl) {
3130
try {
3231
var oldAnnotationsJSON = JSON.parse(oldAnnotations);
3332
} catch (ex) {
34-
console.log('There was an error parsing JSON for ' + paths.source.annotations + 'annotations.js');
35-
console.log(ex);
33+
logger.error(`There was an error parsing JSON for ${paths.source.annotations}annotations.js`);
3634
return [];
3735
}
3836
return oldAnnotationsJSON.comments;

core/lib/asset_copy.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const _ = require('lodash');
33
const path = require('path');
44
const process = require('process');
5+
const logger = require('./log');
56

67
let copy = require('recursive-copy'); // eslint-disable-line prefer-const
78
let chokidar = require('chokidar'); // eslint-disable-line prefer-const
@@ -40,9 +41,7 @@ const asset_copier = () => {
4041
dest,
4142
options
4243
).on(copy.events.COPY_FILE_COMPLETE, () => {
43-
if (options.debug) {
44-
console.log(`Moved ${p} to ${dest}`);
45-
}
44+
logger.debug(`Moved ${p} to ${dest}`);
4645
options.emitter.emit('patternlab-asset-change', {
4746
file: p,
4847
dest: dest
@@ -70,9 +69,7 @@ const asset_copier = () => {
7069

7170
//if we want to watch files, do so, otherwise just copy each file
7271
if (options.watch) {
73-
if (patternlab.config.debug) {
74-
console.log(`Pattern Lab is watching ${path.resolve(basePath, dir.source)} for changes`);
75-
}
72+
logger.info(`Pattern Lab is watching ${path.resolve(basePath, dir.source)} for changes`);
7673

7774
if (patternlab.watchers[key]) {
7875
patternlab.watchers[key].close();
@@ -130,9 +127,7 @@ const asset_copier = () => {
130127

131128
_.each(globalPaths, (globalPath) => {
132129

133-
if (patternlab.config.debug) {
134-
console.log(`Pattern Lab is watching ${globalPath} for changes`);
135-
}
130+
logger.info(`Pattern Lab is watching ${globalPath} for changes`);
136131

137132
if (patternlab.watchers[globalPath]) {
138133
patternlab.watchers[globalPath].close();
@@ -180,9 +175,8 @@ const asset_copier = () => {
180175
)
181176
);
182177
_.each(patternWatches, (patternWatchPath) => {
183-
if (patternlab.config.debug) {
184-
console.log(`Pattern Lab is watching ${patternWatchPath} for changes`);
185-
}
178+
179+
logger.info(`Pattern Lab is watching ${patternWatchPath} for changes`);
186180

187181
const patternWatcher = chokidar.watch(
188182
path.resolve(patternWatchPath),

core/lib/json_copy.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"use strict";
22

3-
const plutils = require('./utilities');
3+
const logger = require('./log');
44
const json_copy = (data, callee) => {
55
try {
66
return JSON.parse(JSON.stringify(data));
77
} catch (e) {
88
//this is unlikely to be hit due to the passed in data already being loaded using JSON parsers
9-
plutils.error(`JSON provided by ${callee} is invalid and cannot be copied`);
10-
return {};
9+
logger.warning(`JSON provided by ${callee} is invalid and cannot be copied`);
10+
throw (e);
1111
}
1212
};
1313

core/lib/lineage_hunter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const extend = require("util")._extend;
3+
const logger = require('./log');
34

45
const lineage_hunter = function () {
56

@@ -119,9 +120,8 @@ const lineage_hunter = function () {
119120
const patternReverseStateIndex = patternStateCascade.indexOf(lineageRPattern.patternState);
120121
if (lineageRPattern.patternState === '' || (patternStateIndex < patternReverseStateIndex)) {
121122

122-
if (patternlab.config.debug) {
123-
console.log('Found a lower common denominator pattern state: ' + pattern.patternState + ' on ' + pattern.patternPartial + '. Setting reverse lineage pattern ' + lineageRPattern.patternPartial + ' from ' + (lineageRPattern.patternState === '' ? '<<blank>>' : lineageRPattern.patternState));
124-
}
123+
const oldState = lineageRPattern.patternState === '' ? '<<blank>>' : lineageRPattern.patternState;
124+
logger.info(`Found a lower common denominator pattern state: ${pattern.patternState} on ${pattern.patternPartial}. Setting reverse lineage pattern ${lineageRPattern.patternPartial} from ${oldState}`);
125125

126126
lineageRPattern.patternState = pattern.patternState;
127127

core/lib/list_item_hunter.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const list_item_hunter = function () {
77
const smh = require('./style_modifier_hunter');
88
const jsonCopy = require('./json_copy');
99
const Pattern = require('./object_factory').Pattern;
10+
const logger = require('./log');
1011

1112
const pattern_assembler = new pa();
1213
const style_modifier_hunter = new smh();
@@ -19,9 +20,7 @@ const list_item_hunter = function () {
1920
if (matches !== null) {
2021
matches.forEach(function (liMatch) {
2122

22-
if (patternlab.config.debug) {
23-
console.log('found listItem of size ' + liMatch + ' inside ' + pattern.patternPartial);
24-
}
23+
logger.debug(`found listItem of size ${liMatch} inside ${pattern.patternPartial}`);
2524

2625
//find the boundaries of the block
2726
const loopNumberString = liMatch.split('.')[1].split('}')[0].trim();
@@ -32,9 +31,8 @@ const list_item_hunter = function () {
3231
const repeatedBlockTemplate = [];
3332
let repeatedBlockHtml = '';
3433
for (let i = 0; i < items.indexOf(loopNumberString); i++) {
35-
if (patternlab.config.debug) {
36-
console.log('list item(s) in pattern', pattern.patternPartial, 'adding', patternBlock, 'to repeatedBlockTemplate');
37-
}
34+
35+
logger.debug(`list item(s) in pattern ${pattern.patternPartial}, adding ${patternBlock} to repeatedBlockTemplate`);
3836
repeatedBlockTemplate.push(patternBlock);
3937
}
4038

@@ -43,8 +41,8 @@ const list_item_hunter = function () {
4341
try {
4442
listData = jsonCopy(patternlab.listitems, 'config.paths.source.data listitems');
4543
} catch (err) {
46-
console.log('There was an error parsing JSON for ' + pattern.relPath);
47-
console.log(err);
44+
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
45+
logger.warning(err);
4846
}
4947

5048
listData = _.merge(listData, pattern.listitems);
@@ -64,8 +62,8 @@ const list_item_hunter = function () {
6462
globalData = jsonCopy(patternlab.data, 'config.paths.source.data global data');
6563
localData = jsonCopy(pattern.jsonFileData, `${pattern.patternPartial} data`);
6664
} catch (err) {
67-
console.log('There was an error parsing JSON for ' + pattern.relPath);
68-
console.log(err);
65+
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
66+
logger.warning(err);
6967
}
7068

7169
let allData = _.merge(globalData, localData);
@@ -89,8 +87,8 @@ const list_item_hunter = function () {
8987
cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern));
9088
cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partialName}`);
9189
} catch (err) {
92-
console.log('There was an error parsing JSON for ' + pattern.relPath);
93-
console.log(err);
90+
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
91+
logger.warning(err);
9492
}
9593

9694
//if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #356

core/lib/utilities.js renamed to core/lib/log.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@ const log = Object.assign({
3131
*/
3232
const debug = log.debug.bind(log);
3333

34+
/**
35+
* @func info
36+
* @desc Coloured info log
37+
* @param {*} msg - The variadic messages to log out.
38+
* @return {void}
39+
*/
40+
const info = log.info.bind(log);
41+
3442
/**
3543
* @func warning
36-
* @desc Coloured error log
44+
* @desc Coloured warning log
3745
* @param {*} e - The variadic messages to log out.
3846
* @return {void}
3947
*/
@@ -62,6 +70,7 @@ const reportError = function (message) {
6270

6371
module.exports = {
6472
debug,
73+
info,
6574
warning,
6675
error,
6776
log,

core/lib/markdown_parser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const md = require('markdown-it')();
33
const yaml = require('js-yaml');
4+
const logger = require('./log');
45

56
const markdown_parser = function () {
67

@@ -36,8 +37,8 @@ const markdown_parser = function () {
3637
returnObject.markdown = md.render(block);
3738
}
3839
} catch (ex) {
39-
console.log(ex);
40-
console.log('error parsing markdown block', block);
40+
logger.warning(ex);
41+
logger.warning(`error parsing markdown block ${block}`);
4142
return undefined;
4243
}
4344

core/lib/parameter_hunter.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const parameter_hunter = function () {
88
const smh = require('./style_modifier_hunter');
99
const style_modifier_hunter = new smh();
1010
const pattern_assembler = new pa();
11+
const logger = require('./log');
1112

1213
/**
1314
* This function is really to accommodate the lax JSON-like syntax allowed by
@@ -49,10 +50,9 @@ const parameter_hunter = function () {
4950
* * Return paramStringWellFormed.
5051
*
5152
* @param {string} pString
52-
* @param {object} patternlab
5353
* @returns {string} paramStringWellFormed
5454
*/
55-
function paramToJson(pString, patternlab) {
55+
function paramToJson(pString) {
5656
let colonPos = -1;
5757
const keys = [];
5858
let paramString = pString; // to not reassign param
@@ -67,10 +67,7 @@ const parameter_hunter = function () {
6767
paramStringWellFormed = JSON.stringify(JSON.parse(pString));
6868
return paramStringWellFormed;
6969
} catch (err) {
70-
//todo this might be a good candidate for a different log level, should we implement that someday
71-
if (patternlab.config.debug) {
72-
console.log(`Not valid JSON found for passed pattern parameter ${pString} will attempt to parse manually...`);
73-
}
70+
logger.debug(`Not valid JSON found for passed pattern parameter ${pString} will attempt to parse manually...`);
7471
}
7572

7673
//replace all escaped double-quotes with escaped unicode
@@ -256,15 +253,13 @@ const parameter_hunter = function () {
256253
//if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #190
257254
partialPattern.extendedTemplate = partialPattern.template;
258255

259-
if (patternlab.config.debug) {
260-
console.log('found patternParameters for ' + partialName);
261-
}
256+
logger.debug(`found patternParameters for ${partialName}`);
262257

263258
//strip out the additional data, convert string to JSON.
264259
const leftParen = pMatch.indexOf('(');
265260
const rightParen = pMatch.lastIndexOf(')');
266261
const paramString = '{' + pMatch.substring(leftParen + 1, rightParen) + '}';
267-
const paramStringWellFormed = paramToJson(paramString, patternlab);
262+
const paramStringWellFormed = paramToJson(paramString);
268263

269264
let paramData = {};
270265
let globalData = {};
@@ -275,8 +270,8 @@ const parameter_hunter = function () {
275270
globalData = jsonCopy(patternlab.data, 'config.paths.source.data global data');
276271
localData = jsonCopy(pattern.jsonFileData || {}, `pattern ${pattern.patternPartial} data`);
277272
} catch (err) {
278-
console.log('There was an error parsing JSON for ' + pattern.relPath);
279-
console.log(err);
273+
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
274+
logger.warning(err);
280275
}
281276

282277
// resolve any pattern links that might be present

0 commit comments

Comments
 (0)