Skip to content

Commit f925b69

Browse files
committed
build for release v1.0.5
1 parent 647ffd5 commit f925b69

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

dist/logline.js

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* logline v1.0.4 (https://github.com/latel/logline#readme)
2+
* logline v1.0.5 (https://github.com/latel/logline#readme)
33
* Copyright 2017, latel <latelx64@icloud.com>
44
* MIT license
55
*/
@@ -10,10 +10,21 @@
1010
(global.Logline = factory());
1111
}(this, (function () { 'use strict';
1212

13-
// throw out Errors, with global prefix 'Logline: ' ahead of err.message
14-
function throwError(errMessage) {
15-
throw new Error('Logline: ' + errMessage);
16-
}
13+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
14+
return typeof obj;
15+
} : function (obj) {
16+
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
17+
};
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
1728

1829
var classCallCheck = function (instance, Constructor) {
1930
if (!(instance instanceof Constructor)) {
@@ -104,6 +115,52 @@ var possibleConstructorReturn = function (self, call) {
104115
return call && (typeof call === "object" || typeof call === "function") ? call : self;
105116
};
106117

118+
var HAS_CONSOLE = window.console;
119+
var LEVEL_CONSOLE_MAP = {
120+
INFO: 'log',
121+
WARN: 'warn',
122+
ERROR: 'error',
123+
CRITICAL: 'error'
124+
};
125+
var LEVEL_STYLE_MAP = {
126+
INFO: 'color:#FFF;background:gray',
127+
WARN: 'color:#FFF;background:orange',
128+
ERROR: 'color:#FFF;background:red',
129+
CRITICAL: 'color:#FFF;background:black'
130+
};
131+
132+
// throw out Errors, with global prefix 'Logline: ' ahead of err.message
133+
function throwError(errMessage) {
134+
throw new Error('Logline: ' + errMessage);
135+
}
136+
137+
// print debug info in develper's console
138+
// if WechatFE/vConsole is detected, will not use %c feature, as it is not well supported
139+
function debug(namespace, level, descriptor, data) {
140+
if (HAS_CONSOLE) {
141+
window.console[LEVEL_CONSOLE_MAP[level.toUpperCase()] || LEVEL_CONSOLE_MAP.INFO]('%c %s %s %c %s. ' + ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) === 'object' ? '%O' : '%s'), LEVEL_STYLE_MAP[level.toUpperCase()] || LEVEL_STYLE_MAP.INFO, level, namespace, 'color:initial', descriptor, data || '');
142+
}
143+
}
144+
145+
// filter any function in a object
146+
function filterFunction(obj) {
147+
var newObj = {},
148+
i;
149+
150+
if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') {
151+
return obj;
152+
}
153+
154+
for (i in obj) {
155+
if (obj.hasOwnProperty(i)) {
156+
if (typeof obj[i] !== 'function') {
157+
newObj[i] = filterFunction(obj[i]);
158+
}
159+
}
160+
}
161+
return newObj;
162+
}
163+
107164
/**
108165
* Logline Interface
109166
* @class Interface
@@ -118,7 +175,7 @@ var Interface = function () {
118175
function Interface(namespace) {
119176
classCallCheck(this, Interface);
120177

121-
this._namesapce = namespace;
178+
this._namespace = namespace;
122179
}
123180

124181
/**
@@ -400,17 +457,20 @@ var IndexedDBLogger = function (_LoggerInterface) {
400457
return;
401458
}
402459

460+
debug(this._namespace, level, descriptor, data);
403461
var transaction = IndexedDBLogger.db.transaction(['logs'], IDBTransaction.READ_WRITE || 'readwrite');
404462
transaction.onerror = function (event) {
405463
return throwError(event.target.error);
406464
};
407465

408466
var store = transaction.objectStore('logs');
467+
// should not contains any function in data
468+
// otherwise 'DOMException: Failed to execute 'add' on 'IDBObjectStore': An object could not be cloned.' will be thrown
409469
var request = store.add({
410470
time: Date.now(),
411-
namespace: this._namesapce,
471+
namespace: this._namespace,
412472
descriptor: descriptor,
413-
data: data
473+
data: filterFunction(data)
414474
});
415475

416476
request.onerror = function (event) {
@@ -660,8 +720,9 @@ var LocalStorageLogger = function (_LoggerInterface) {
660720
key: '_record',
661721
value: function _record(level, descriptor, data) {
662722
var logs = window.localStorage.getItem(LocalStorageLogger._database) ? JSON.parse(window.localStorage.getItem(LocalStorageLogger._database)) : [];
663-
logs.push([Date.now(), this._namesapce, level, descriptor, data]);
723+
logs.push([Date.now(), this._namespace, level, descriptor, data]);
664724
try {
725+
debug(this._namespace, level, descriptor, data);
665726
window.localStorage.setItem(LocalStorageLogger._database, JSON.stringify(logs));
666727
} catch (e) {
667728
throwError('error inserting record');
@@ -817,8 +878,9 @@ var WebsqlLogger = function (_LoggerInterface) {
817878
}
818879

819880
try {
881+
debug(this._namespace, level, descriptor, data);
820882
WebsqlLogger._db.transaction(function (tx) {
821-
tx.executeSql('INSERT INTO logs (time, namespace, level, descriptor, data) VALUES(?, ?, ?, ? ,?)', [Date.now(), _this2._namesapce, level, descriptor, data === undefined || data === '' ? '' : JSON.stringify(data) || ''], function () {/* empty func */}, function (tx, e) {
883+
tx.executeSql('INSERT INTO logs (time, namespace, level, descriptor, data) VALUES(?, ?, ?, ? ,?)', [Date.now(), _this2._namespace, level, descriptor, data === undefined || data === '' ? '' : JSON.stringify(data) || ''], function () {/* empty func */}, function (tx, e) {
822884
throw e.message;
823885
});
824886
});

0 commit comments

Comments
 (0)