Skip to content

Commit b4785b0

Browse files
committed
Merge pull request #590 from plotly/log-levels
Log levels
2 parents 0f110e1 + b478bb0 commit b4785b0

33 files changed

+141
-136
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ chart types, including 3D charts, statistical graphs, and SVG maps.
2525

2626
## Quick start options
2727

28+
**It is important to note that logging is turned off by default in v1.13.0 onwards.**
29+
To turn logging on for development, you will want to run
30+
`Plotly.setPlotConfig({ logging: 2 })` before any plotting.
31+
See [this file](https://github.com/plotly/plotly.js/blob/master/src/lib/loggers.js) for more details.
32+
2833
#### Download the latest release
2934
[Latest Release on Github](https://github.com/plotly/plotly.js/releases/)
3035

devtools/test_dashboard/devtools.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ var Tabs = {
1313

1414
// Set plot config options
1515
setPlotConfig: function() {
16-
17-
// use local topojson files
18-
Plotly.setPlotConfig({ topojsonURL: '../../dist/topojson/' });
16+
Plotly.setPlotConfig({
17+
// use local topojson files
18+
topojsonURL: '../../dist/topojson/',
19+
logging: 2
20+
});
1921
},
2022

2123
// Return the specified plot container (or default one)

src/.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"Uint8Array": true
1010
},
1111
"rules": {
12-
"strict": [2, "global"]
12+
"strict": [2, "global"],
13+
"no-console": [2]
1314
}
1415
}

src/components/drawing/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ drawing.fillGroupStyle = function(s) {
122122
shape.call(Color.fill, d[0].trace.fillcolor);
123123
}
124124
catch(e) {
125-
console.log(e, s);
125+
Lib.error(e, s);
126126
shape.remove();
127127
}
128128
});

src/components/rangeslider/range_plot.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
var d3 = require('d3');
1212

13+
var Lib = require('../../lib');
1314
var Symbols = require('../drawing/symbol_defs');
1415
var Drawing = require('../drawing');
1516

@@ -53,7 +54,7 @@ module.exports = function rangePlot(gd, w, h) {
5354
pointPairs = [];
5455

5556
if(allowedTypes.indexOf(trace.type) < 0) {
56-
console.log('Trace type ' + trace.type + ' not supported for range slider!');
57+
Lib.warn('Trace type ' + trace.type + ' not supported for range slider!');
5758
continue;
5859
}
5960

@@ -161,7 +162,7 @@ function makeScatter(trace, pointPairs, w, h) {
161162
break;
162163

163164
default:
164-
console.log('Fill type ' + trace.fill + ' not supported for range slider! (yet...)');
165+
Lib.warn('Fill type ' + trace.fill + ' not supported for range slider! (yet...)');
165166
break;
166167
}
167168

src/components/shapes/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ function getShapeLayer(gd, index) {
351351
shapeLayer = gd._fullLayout._shapeUpperLayer;
352352

353353
if(!shape) {
354-
console.log('getShapeLayer: undefined shape: index', index);
354+
Lib.log('getShapeLayer: undefined shape: index', index);
355355
}
356356
else if(shape.layer === 'below') {
357357
shapeLayer = (shape.xref === 'paper' && shape.yref === 'paper') ?
@@ -491,7 +491,7 @@ shapes.convertPath = function(pathIn, x2p, y2p) {
491491

492492
if(paramNumber > nParams) {
493493
paramString = paramString.replace(/[\s,]*X.*/, '');
494-
console.log('ignoring extra params in segment ' + segment);
494+
Lib.log('Ignoring extra params in segment ' + segment);
495495
}
496496

497497
return segmentType + paramString;

src/lib/dates.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
var d3 = require('d3');
1313
var isNumeric = require('fast-isnumeric');
1414

15+
var Lib = require('../lib');
16+
1517

1618
/**
1719
* dateTime2ms - turn a date object or string s of the form
@@ -122,7 +124,7 @@ function lpad(val, digits) {
122124
*/
123125
exports.ms2DateTime = function(ms, r) {
124126
if(typeof(d3) === 'undefined') {
125-
console.log('d3 is not defined');
127+
Lib.error('d3 is not defined.');
126128
return;
127129
}
128130

src/lib/geo_location_utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
var countryRegex = require('country-regex');
13-
var Lib = require('./');
13+
var Lib = require('../lib');
1414

1515

1616
// make list of all country iso3 ids from at runtime
@@ -32,8 +32,8 @@ exports.locationToFeature = function(locationmode, location, features) {
3232
if(feature.id === locationId) return feature;
3333
}
3434

35-
console.warn([
36-
'location with id', locationId,
35+
Lib.warn([
36+
'Location with id', locationId,
3737
'does not have a matching topojson feature at this resolution.'
3838
].join(' '));
3939
};
@@ -53,5 +53,5 @@ function countryNameToISO3(countryName) {
5353
if(regex.test(countryName.toLowerCase())) return iso3;
5454
}
5555

56-
console.warn('unrecognized country name: ' + countryName + '.');
56+
Lib.warn('Unrecognized country name: ' + countryName + '.');
5757
}

src/lib/index.js

+8-33
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ lib.extendFlat = extendModule.extendFlat;
5858
lib.extendDeep = extendModule.extendDeep;
5959
lib.extendDeepAll = extendModule.extendDeepAll;
6060

61+
var loggersModule = require('./loggers');
62+
lib.log = loggersModule.log;
63+
lib.warn = loggersModule.warn;
64+
lib.error = loggersModule.error;
65+
6166
lib.notifier = require('./notifier');
6267

6368
/**
@@ -92,35 +97,6 @@ lib.pauseEvent = function(e) {
9297
return false;
9398
};
9499

95-
/**
96-
* ------------------------------------------
97-
* debugging tools
98-
* ------------------------------------------
99-
*/
100-
101-
// set VERBOSE to true to get a lot more logging and tracing
102-
lib.VERBOSE = false;
103-
104-
// first markTime call will return time from page load
105-
lib.TIMER = new Date().getTime();
106-
107-
// console.log that only runs if VERBOSE is on
108-
lib.log = function() {
109-
if(lib.VERBOSE) console.log.apply(console, arguments);
110-
};
111-
112-
/**
113-
* markTime - for debugging, mark the number of milliseconds
114-
* since the previous call to markTime and log arbitrary info too
115-
*/
116-
lib.markTime = function(v) {
117-
if(!lib.VERBOSE) return;
118-
var t2 = new Date().getTime();
119-
console.log(v, t2 - lib.TIMER, '(msec)');
120-
if(lib.VERBOSE === 'trace') console.trace();
121-
lib.TIMER = t2;
122-
};
123-
124100
// constrain - restrict a number v to be between v0 and v1
125101
lib.constrain = function(v, v0, v1) {
126102
if(v0 > v1) return Math.max(v1, Math.min(v0, v));
@@ -271,18 +247,17 @@ lib.syncOrAsync = function(sequence, arg, finalStep) {
271247
var ret, fni;
272248

273249
function continueAsync() {
274-
lib.markTime('async done ' + fni.name);
275250
return lib.syncOrAsync(sequence, arg, finalStep);
276251
}
252+
277253
while(sequence.length) {
278254
fni = sequence.splice(0, 1)[0];
279255
ret = fni(arg);
280-
// lib.markTime('done calling '+fni.name)
256+
281257
if(ret && ret.then) {
282258
return ret.then(continueAsync)
283259
.then(undefined, lib.promiseError);
284260
}
285-
lib.markTime('sync done ' + fni.name);
286261
}
287262

288263
return finalStep && finalStep(arg);
@@ -433,7 +408,7 @@ lib.addStyleRule = function(selector, styleString) {
433408
else if(styleSheet.addRule) {
434409
styleSheet.addRule(selector, styleString, 0);
435410
}
436-
else console.warn('addStyleRule failed');
411+
else lib.warn('addStyleRule failed');
437412
};
438413

439414
lib.getTranslate = function(element) {

src/lib/loggers.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var config = require('../plot_api/plot_config');
12+
13+
var loggers = module.exports = {};
14+
15+
/**
16+
* ------------------------------------------
17+
* debugging tools
18+
* ------------------------------------------
19+
*/
20+
21+
/* eslint-disable no-console */
22+
loggers.log = function() {
23+
if(config.logging > 1) {
24+
var messages = ['LOG:'];
25+
26+
for(var i = 0; i < arguments.length; i++) {
27+
messages.push(arguments[i]);
28+
}
29+
30+
if(console.trace) {
31+
console.trace.apply(console, messages);
32+
} else {
33+
console.log.apply(console, messages);
34+
}
35+
}
36+
};
37+
38+
loggers.warn = function() {
39+
if(config.logging > 0) {
40+
var messages = ['WARN:'];
41+
42+
for(var i = 0; i < arguments.length; i++) {
43+
messages.push(arguments[i]);
44+
}
45+
46+
if(console.trace) {
47+
console.trace.apply(console, messages);
48+
} else {
49+
console.log.apply(console, messages);
50+
}
51+
}
52+
};
53+
54+
loggers.error = function() {
55+
if(config.logging > 0) {
56+
var messages = ['ERROR:'];
57+
58+
for(var i = 0; i < arguments.length; i++) {
59+
messages.push(arguments[i]);
60+
}
61+
62+
console.error.apply(console, arguments);
63+
}
64+
};
65+
/* eslint-enable no-console */

src/lib/search.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
var isNumeric = require('fast-isnumeric');
1313

14+
var Lib = require('../lib');
15+
1416

1517
/**
1618
* findBin - find the bin for val - note that it can return outside the
@@ -45,7 +47,7 @@ exports.findBin = function(val, bins, linelow) {
4547
if(test(bins[n], val)) n1 = n + 1;
4648
else n2 = n;
4749
}
48-
if(c > 90) console.log('Long binary search...');
50+
if(c > 90) Lib.log('Long binary search...');
4951
return n1 - 1;
5052
}
5153
};

src/lib/svg_text_utils.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
var Plotly = require('../plotly');
1515
var d3 = require('d3');
1616

17+
var Lib = require('../lib');
1718
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
1819

1920
var util = module.exports = {};
@@ -36,7 +37,7 @@ d3.selection.prototype.appendSVG = function(_svgString) {
3637
childNode = childNode.nextSibling;
3738
}
3839
if(dom.querySelector('parsererror')) {
39-
console.log(dom.querySelector('parsererror div').textContent);
40+
Lib.log(dom.querySelector('parsererror div').textContent);
4041
return null;
4142
}
4243
return d3.select(this.node().lastChild);
@@ -202,7 +203,7 @@ function texToSVG(_texString, _config, _callback) {
202203
var glyphDefs = d3.select('body').select('#MathJax_SVG_glyphs');
203204

204205
if(tmpDiv.select('.MathJax_SVG').empty() || !tmpDiv.select('svg').node()) {
205-
console.log('There was an error in the tex syntax.', _texString);
206+
Lib.log('There was an error in the tex syntax.', _texString);
206207
_callback();
207208
}
208209
else {

0 commit comments

Comments
 (0)