Skip to content

Commit

Permalink
Merge branch dev
Browse files Browse the repository at this point in the history
  • Loading branch information
latel committed Mar 2, 2017
2 parents 33492df + f925b69 commit ed4a471
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 104 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v1.0.5
A output logs in developer tool's console
F IndexedDB: unable to remove special targeted logs

v1.0.4
U use rollup.js bundler to reduce dist package size(33% off)
A add makefile, use make for automatic procedures
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ Features
-------

+ No extra dependencies
+ Record logs
+ Client-side
+ websql, localstorage and indexeddb protocol
+ namespace
+ degree
+ client-side(reach when acctually needed, save bandwith and traffic)
+ multiple filter dimension(namespace, degree and keyword)
+ multiple persistence storage solution(IndexDB, Websql, localStorage)
+ cleanable(in case take too much user space)

Quick to get started
-------------------
Expand Down
12 changes: 5 additions & 7 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ logline是一个轻量,实用和客户端级的前端日志记录工具。
特性
---

+ 无外部依赖
+ 日志记录
+ 客户端级
+ websql/localstorage/indexeddb三种日志协议
+ 命名空间
+ 日志等级
+ 日志清理(防止日志过多,占用上传带宽和占满用户允许的内存)
+ 零外部依赖
+ 客户端存放(需要时再获取,节省移动带宽、流量和连接数)
+ 多维度过滤(命名空间、日志等级和关键词)
+ 多个存储方案(Websql、localStorage和IndexedDB)
+ 可清理(防止日志过多,占用上传带宽和占满用户允许的内存)

快速上手
------
Expand Down
10 changes: 5 additions & 5 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
WIP
none
[ ] IndexedDB: cursor.continue() meet `throw InvalidStateError when the cursor is being iterated`

CONSIDERING
[ ] fullfill error handling LOW
[ ] 修复phantomjs无法正确测试indexeddb的问题,phantomjs在关闭数据后才能读取到新存储的内容 LOW
[ ] add a tag, to indicate pending operations LOW
[ ] ability to get desired NICE TO HAVE
[ ] fullfill error handling
[ ] 修复phantomjs无法正确测试indexeddb的问题,phantomjs在关闭数据后才能读取到新存储的内容
[ ] add a tag, to indicate pending operations
[ ] selecte random background color per namespace for the outpus on developer tool's console panel
141 changes: 100 additions & 41 deletions dist/logline.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* logline v1.0.4 (https://github.com/latel/logline#readme)
* logline v1.0.5 (https://github.com/latel/logline#readme)
* Copyright 2017, latel <[email protected]>
* MIT license
*/
Expand All @@ -10,10 +10,21 @@
(global.Logline = factory());
}(this, (function () { 'use strict';

// throw out Errors, with global prefix 'Logline: ' ahead of err.message
function throwError(errMessage) {
throw new Error('Logline: ' + errMessage);
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};











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

var HAS_CONSOLE = window.console;
var LEVEL_CONSOLE_MAP = {
INFO: 'log',
WARN: 'warn',
ERROR: 'error',
CRITICAL: 'error'
};
var LEVEL_STYLE_MAP = {
INFO: 'color:#FFF;background:gray',
WARN: 'color:#FFF;background:orange',
ERROR: 'color:#FFF;background:red',
CRITICAL: 'color:#FFF;background:black'
};

// throw out Errors, with global prefix 'Logline: ' ahead of err.message
function throwError(errMessage) {
throw new Error('Logline: ' + errMessage);
}

// print debug info in develper's console
// if WechatFE/vConsole is detected, will not use %c feature, as it is not well supported
function debug(namespace, level, descriptor, data) {
if (HAS_CONSOLE) {
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 || '');
}
}

// filter any function in a object
function filterFunction(obj) {
var newObj = {},
i;

if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') {
return obj;
}

for (i in obj) {
if (obj.hasOwnProperty(i)) {
if (typeof obj[i] !== 'function') {
newObj[i] = filterFunction(obj[i]);
}
}
}
return newObj;
}

/**
* Logline Interface
* @class Interface
Expand All @@ -118,7 +175,7 @@ var Interface = function () {
function Interface(namespace) {
classCallCheck(this, Interface);

this._namesapce = namespace;
this._namespace = namespace;
}

/**
Expand Down Expand Up @@ -392,25 +449,28 @@ var IndexedDBLogger = function (_LoggerInterface) {

if (IndexedDBLogger.status !== Interface.STATUS.INITED) {
IndexedDBLogger._pool.push(function () {
_this2._record(level, descriptor, data);
return _this2._record(level, descriptor, data);
});
if (IndexedDBLogger.status !== Interface.STATUS.INITING) {
IndexedDBLogger.init();
}
return;
}

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

var store = transaction.objectStore('logs');
// should not contains any function in data
// otherwise 'DOMException: Failed to execute 'add' on 'IDBObjectStore': An object could not be cloned.' will be thrown
var request = store.add({
time: Date.now(),
namespace: this._namesapce,
namespace: this._namespace,
descriptor: descriptor,
data: data
data: filterFunction(data)
});

request.onerror = function (event) {
Expand Down Expand Up @@ -481,10 +541,9 @@ var IndexedDBLogger = function (_LoggerInterface) {
key: 'get',
value: function get$$1(from, to, readyFn) {
if (IndexedDBLogger.status !== get(IndexedDBLogger.__proto__ || Object.getPrototypeOf(IndexedDBLogger), 'STATUS', this).INITED) {
IndexedDBLogger._pool.push(function () {
IndexedDBLogger.get(from, to, readyFn);
return IndexedDBLogger._pool.push(function () {
return IndexedDBLogger.get(from, to, readyFn);
});
return;
}

from = Interface.transTimeFormat(from);
Expand Down Expand Up @@ -529,10 +588,9 @@ var IndexedDBLogger = function (_LoggerInterface) {
key: 'keep',
value: function keep(daysToMaintain) {
if (IndexedDBLogger.status !== get(IndexedDBLogger.__proto__ || Object.getPrototypeOf(IndexedDBLogger), 'STATUS', this).INITED) {
IndexedDBLogger._pool.push(function () {
IndexedDBLogger.keep(daysToMaintain);
return IndexedDBLogger._pool.push(function () {
return IndexedDBLogger.keep(daysToMaintain);
});
return;
}

var store = IndexedDBLogger._getTransactionStore(IDBTransaction.READ_WRITE);
Expand All @@ -541,18 +599,20 @@ var IndexedDBLogger = function (_LoggerInterface) {
return throwError(event.target.error);
};
} else {
var range = IDBKeyRange.upperBound(Date.now() - (daysToMaintain || 2) * 24 * 3600 * 1000, true);
var _request = store.openCursor(range);
_request.onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
store.delete(cursor.primaryKey);
cursor.continue();
}
};
_request.onerror = function (event) {
return throwError('unable to locate logs earlier than ' + daysToMaintain + 'd.');
};
(function () {
var range = Date.now() - (daysToMaintain || 2) * 24 * 3600 * 1000;
var request = store.openCursor();
request.onsuccess = function (event) {
var cursor = event.target.result;
if (cursor && cursor.value.time < range) {
store.delete(cursor.primaryKey);
cursor.continue();
}
};
request.onerror = function (event) {
return throwError('unable to locate logs earlier than ' + daysToMaintain + 'd.');
};
})();
}
}

Expand All @@ -566,10 +626,9 @@ var IndexedDBLogger = function (_LoggerInterface) {
key: 'clean',
value: function clean() {
if (IndexedDBLogger.status !== get(IndexedDBLogger.__proto__ || Object.getPrototypeOf(IndexedDBLogger), 'STATUS', this).INITED) {
IndexedDBLogger._pool.push(function () {
IndexedDBLogger.clean();
return IndexedDBLogger._pool.push(function () {
return IndexedDBLogger.clean();
});
return;
}

// database can be removed only after all connections are closed
Expand Down Expand Up @@ -604,7 +663,7 @@ var IndexedDBLogger = function (_LoggerInterface) {
};
return transaction.objectStore('logs');
} else {
throwError('log database is not created or connections is closed, considering init it.');
throwError('log database is not created or connections are closed, considering init it.');
}
}

Expand Down Expand Up @@ -661,8 +720,9 @@ var LocalStorageLogger = function (_LoggerInterface) {
key: '_record',
value: function _record(level, descriptor, data) {
var logs = window.localStorage.getItem(LocalStorageLogger._database) ? JSON.parse(window.localStorage.getItem(LocalStorageLogger._database)) : [];
logs.push([Date.now(), this._namesapce, level, descriptor, data]);
logs.push([Date.now(), this._namespace, level, descriptor, data]);
try {
debug(this._namespace, level, descriptor, data);
window.localStorage.setItem(LocalStorageLogger._database, JSON.stringify(logs));
} catch (e) {
throwError('error inserting record');
Expand Down Expand Up @@ -809,7 +869,7 @@ var WebsqlLogger = function (_LoggerInterface) {

if (WebsqlLogger.status !== Interface.STATUS.INITED) {
WebsqlLogger._pool.push(function () {
_this2._record(level, descriptor, data);
return _this2._record(level, descriptor, data);
});
if (WebsqlLogger.status !== Interface.STATUS.INITING) {
WebsqlLogger.init();
Expand All @@ -818,8 +878,9 @@ var WebsqlLogger = function (_LoggerInterface) {
}

try {
debug(this._namespace, level, descriptor, data);
WebsqlLogger._db.transaction(function (tx) {
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) {
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) {
throw e.message;
});
});
Expand Down Expand Up @@ -881,10 +942,9 @@ var WebsqlLogger = function (_LoggerInterface) {
key: 'get',
value: function get$$1(from, to, readyFn) {
if (WebsqlLogger.status !== get(WebsqlLogger.__proto__ || Object.getPrototypeOf(WebsqlLogger), 'STATUS', this).INITED) {
WebsqlLogger._pool.push(function () {
WebsqlLogger.get(from, to, readyFn);
return WebsqlLogger._pool.push(function () {
return WebsqlLogger.get(from, to, readyFn);
});
return;
}

from = Interface.transTimeFormat(from);
Expand Down Expand Up @@ -933,10 +993,9 @@ var WebsqlLogger = function (_LoggerInterface) {
key: 'keep',
value: function keep(daysToMaintain) {
if (WebsqlLogger.status !== get(WebsqlLogger.__proto__ || Object.getPrototypeOf(WebsqlLogger), 'STATUS', this).INITED) {
WebsqlLogger._pool.push(function () {
WebsqlLogger.keep(daysToMaintain);
return WebsqlLogger._pool.push(function () {
return WebsqlLogger.keep(daysToMaintain);
});
return;
}

try {
Expand Down Expand Up @@ -967,7 +1026,7 @@ var WebsqlLogger = function (_LoggerInterface) {
value: function clean() {
if (WebsqlLogger.status !== get(WebsqlLogger.__proto__ || Object.getPrototypeOf(WebsqlLogger), 'STATUS', this).INITED) {
WebsqlLogger._pool.push(function () {
WebsqlLogger.clean();
return WebsqlLogger.clean();
});
return;
}
Expand Down
Loading

0 comments on commit ed4a471

Please sign in to comment.