Skip to content

Commit 65af825

Browse files
committed
Merge branch 'dev'
2 parents a2c5c0d + ca7308f commit 65af825

File tree

9 files changed

+368
-166
lines changed

9 files changed

+368
-166
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,15 @@ sdkLog.critical('system.vanish', {
100100
});
101101
```
102102

103-
### 5. 上传日志
103+
### 5. 读取日志
104+
105+
``` javascript
106+
Logline.getAll(function(logs) {
107+
// process logs here
108+
});
109+
```
110+
111+
### 6. 上传日志(deprecated)
104112

105113
``` javascript
106114
Logline.deploy(
@@ -117,6 +125,19 @@ Logline.deploy(
117125
);
118126
```
119127

128+
自定义构建
129+
--------
130+
目前Logline一共实现了`localStorage``websql``indexedDB`三个日志协议,默认是全部打包,可能你只想使用其中某个协议而已,你可以通过`npm run configure`来自定义构建你需要的版本。这样有利于减小包的大小。
131+
132+
``` shell
133+
// 配置你需要的协议,去掉不需要的协议申明--with-xxx
134+
// 注意大小写
135+
npm run configure -- --with-localStorage --with-websql --with-indexedDB
136+
// 重新打包
137+
npm run build
138+
// 去dist目录寻找新构建的文件
139+
```
140+
120141

121142
日志分析 [logline-viewer]()
122143
-------------------------

TODO

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
WIP
2-
[] 添加 IndexedDB 协议的支持
3-
[] 允许自定义编译,只打包自己需要的协议,以减小打包文件的大小
4-
[] 不使用 webpack 编译,以减小打包文件的大小
2+
[√] 添加 IndexedDB 协议的支持
3+
[ ] 剥离日志上传实现,由使用方自行决策上传途径,改为暴露获取所有日志的方法
4+
[√] 允许自定义编译,只打包自己需要的协议,以减小打包文件的大小
5+
[ ] 不使用 webpack 编译,以减小打包文件的大小
56

67
CONSIDERING
7-
[] 上传时允许指定倒序还是正序
8-
[] 允许配置切割分段上传,有些服务器可能会单次上传有大小限制
8+
[ ] 上传时允许指定倒序还是正序
9+
[ ] 允许配置切割分段上传,有些服务器可能会单次上传有大小限制

dist/logline.js

Lines changed: 145 additions & 142 deletions
Large diffs are not rendered by default.

dist/logline.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/logline.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var pkg = require('./package.json');
4+
var rename = require('gulp-rename');
5+
var gulp = require('gulp');
6+
var handlebars = require('gulp-compile-handlebars');
7+
var argv = require('minimist')(process.argv.slice(2));
8+
9+
10+
gulp.task('configure', function() {
11+
delete argv._;
12+
gulp.src(path.join(__dirname, '/src/configure'))
13+
.pipe(handlebars(
14+
{
15+
protocols: Object.keys(argv).map(function(name) {
16+
return name.replace(/^with\-/, '');
17+
})
18+
},
19+
{
20+
helpers: {
21+
compare: function(v1, v2, options) {
22+
if (v1 - 1 > v2) {
23+
return options.fn(this);
24+
}
25+
else {
26+
return options.inverse(this);
27+
}
28+
},
29+
upper: function(str, all) {
30+
return all ? str.toUpperCase() : str.replace(/^(\w)(.*)/, function(str, cap, rest) {
31+
return cap.toUpperCase() + rest;
32+
});
33+
}
34+
}
35+
}
36+
))
37+
.pipe(rename(pkg.name + '.js'))
38+
.pipe(gulp.dest('./src'));
39+
});
40+
41+
gulp.task('default', [ 'configure' ]);

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"main": "dist/logline.min.js",
66
"scripts": {
77
"test": "mocha",
8-
"configure": "node configure",
8+
"configure": "gulp configure",
99
"build:dev": "webpack --config webpack.config.js",
1010
"build:prod": "webpack --config webpack.config.prod.js",
11-
"dist": "npm run build:dev && npm run build:prod && npm run test"
11+
"build": "npm run build:dev && npm run build:prod && npm run test"
1212
},
1313
"repository": {
1414
"type": "git",
@@ -36,17 +36,16 @@
3636
"babel-plugin-transform-runtime": "^6.15.0",
3737
"babel-preset-es2015": "^6.6.0",
3838
"babel-preset-stage-3": "^6.5.0",
39-
"babelify": "^7.3.0",
40-
"browserify": "^13.1.1",
4139
"chai": "^3.5.0",
4240
"extract-text-webpack-plugin": "^1.0.1",
4341
"gulp": "^3.9.1",
4442
"gulp-babel": "^6.1.2",
45-
"gulp-sourcemaps": "^2.2.0",
46-
"gulp-umd": "^0.2.0",
43+
"gulp-compile-handlebars": "^0.6.1",
44+
"gulp-rename": "^1.2.2",
4745
"html-loader": "^0.4.3",
4846
"jsdom": "^9.2.1",
4947
"json-loader": "^0.5.4",
48+
"minimist": "^1.2.0",
5049
"mocha": "^2.5.3",
5150
"node-storage-shim": "^1.0.1",
5251
"webpack": "~1.12.11"

src/configure

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{{#each protocols}}
2+
import {{upper this false}}Logger from './{{this}}';
3+
{{/each}}
4+
import * as util from './lib/util';
5+
6+
7+
class Logline {
8+
constructor(namespace) {
9+
Logline._checkProtocol();
10+
return new Logline._protocol(namespace);
11+
}
12+
13+
// 检查协议
14+
static _checkProtocol() {
15+
if (!Logline._protocol) {
16+
util.throwError('you must choose a protocol with "using" method.');
17+
}
18+
}
19+
20+
// 获取所有日志
21+
static getAll(readyFn) {
22+
Logline._checkProtocol();
23+
Logline._protocol.all(function(logs) {
24+
readyFn(logs);
25+
});
26+
}
27+
28+
// 发送日志
29+
static deploy(descriptor, tickerFn, readyFn, errorFn) {
30+
Logline._checkProtocol();
31+
if (Logline._reportTo) {
32+
Logline._protocol.all(function(logs) {
33+
var xhr = new XMLHttpRequest(),
34+
logsToSend = [],
35+
log, key, line;
36+
37+
xhr.upload.onprogress = tickerFn;
38+
xhr.onload = function() {
39+
if (200 === xhr.status) {
40+
'function' === typeof readyFn && readyFn();
41+
}
42+
else {
43+
'function' === typeof errorFn && errorFn();
44+
}
45+
};
46+
xhr.onerror = function() {
47+
'function' === typeof errorFn && errorFn();
48+
};
49+
xhr.open('POST', LogcatInterface._reportTo);
50+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
51+
52+
// 处理logs成常见的日志形式来上报(一行一条日志内容),避免重复键名占用空间
53+
while ((log = logs.pop())) {
54+
line = [];
55+
for (key in log) {
56+
if (log.hasOwnProperty(key) && log[key]) {
57+
line.push(log[key]);
58+
}
59+
}
60+
logsToSend.push(line.join('\t'));
61+
}
62+
63+
xhr.withCredentials = true;
64+
logsToSend.unshift(location.host + (descriptor ? (': ' + descriptor) : ''));
65+
xhr.send('data=' + (escape(logsToSend.join('\n')) || 'no data'));
66+
});
67+
}
68+
else {
69+
util.throwError('report address is not configed.');
70+
}
71+
}
72+
73+
// 清理日志
74+
static keep(daysToMaintain) {
75+
try {
76+
Logline._checkProtocol();
77+
Logline._protocol.keep(daysToMaintain);
78+
} catch (e) {
79+
util.throwError('unable to remove logs earlier than ' + daysToMaintain + 'd.');
80+
}
81+
return this;
82+
}
83+
84+
// 清空日志并删除数据库
85+
static clean() {
86+
try {
87+
Logline._checkProtocol();
88+
Logline._protocol.clean();
89+
} catch (e) { util.throwError('unable to clean log database.'); }
90+
return this;
91+
}
92+
93+
// 选择一个日志协议
94+
static using(protocol) {
95+
// 协议一旦选定即不允许在运行时更改
96+
if (Logline._protocol) {
97+
return this;
98+
}
99+
100+
if (-1 < Object.values(Logline.PROTOCOL).indexOf(protocol)) {
101+
Logline._protocol = protocol;
102+
Logline.init();
103+
}
104+
else {
105+
util.throwError('specialfied protocol is not available.');
106+
}
107+
108+
return this;
109+
}
110+
111+
// 初始化选定的协议
112+
static init() {
113+
Logline._checkProtocol();
114+
Logline._protocol.init();
115+
116+
return this;
117+
}
118+
119+
// 配置日志上报地址
120+
static reportTo(reportTo) {
121+
Logline._reportTo = reportTo;
122+
return this;
123+
}
124+
}
125+
126+
Logline.PROTOCOL = {
127+
{{#each protocols}}
128+
{{#compare ../protocols.length @index}}
129+
{{upper this true}}: {{upper this false}}Logger,
130+
{{else}}
131+
{{upper this true}}: {{upper this false}}Logger
132+
{{/compare}}
133+
{{/each}}
134+
};
135+
136+
module.exports = Logline;

src/logline.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import LocalStorageLogger from './localstorage';
21
import WebsqlLogger from './websql';
3-
import IndexedDBLogger from './indexeddb';
2+
import LocalStorageLogger from './localStorage';
3+
import IndexedDBLogger from './indexedDB';
44
import * as util from './lib/util';
55

6-
let LogcatInterface = function(namespace) {
7-
LogcatInterface._checkProtocol();
8-
return new LogcatInterface._protocol(namespace);
9-
};
106

117
class Logline {
128
constructor(namespace) {
@@ -21,6 +17,14 @@ class Logline {
2117
}
2218
}
2319

20+
// 获取所有日志
21+
static getAll(readyFn) {
22+
Logline._checkProtocol();
23+
Logline._protocol.all(function(logs) {
24+
readyFn(logs);
25+
});
26+
}
27+
2428
// 发送日志
2529
static deploy(descriptor, tickerFn, readyFn, errorFn) {
2630
Logline._checkProtocol();
@@ -42,7 +46,7 @@ class Logline {
4246
xhr.onerror = function() {
4347
'function' === typeof errorFn && errorFn();
4448
};
45-
xhr.open('POST', LogcatInterface._reportTo);
49+
xhr.open('POST', Logline._reportTo);
4650
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
4751

4852
// 处理logs成常见的日志形式来上报(一行一条日志内容),避免重复键名占用空间
@@ -119,9 +123,6 @@ class Logline {
119123
}
120124
}
121125

122-
Logline._protocol = null;
123-
Logline._reportTo = null;
124-
125126
Logline.PROTOCOL = {
126127
WEBSQL: WebsqlLogger,
127128
LOCALSTORAGE: LocalStorageLogger,

0 commit comments

Comments
 (0)