Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
eshengsky committed Aug 26, 2017
1 parent 303a800 commit b503d85
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 19 deletions.
65 changes: 56 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ lajax 尝试解决这些问题:

* 对于服务器动态生成的网页,系统原有的服务端日志无法和前端日志进行关联。

## 目录

* [功能特性](#功能特性)

* [在线示例](#在线示例)

* [快速开始](#快速开始)

* [Api](#api)

* [日志格式](#日志格式)

* [请求id](#请求id)

* [服务器示例](#服务器示例)

* [测试](#测试)

* [许可](#许可)

## 功能特性

* 手动记录日志,支持 `info``warn``error` 3 种日志级别;
Expand All @@ -40,7 +60,17 @@ lajax 尝试解决这些问题:

## 快速开始

### 引入插件
### 下载

* 通过 npm 下载:

```bash
> npm install lajax
```

* 或者在 [release](https://github.com/eshengsky/lajax/releases) 页面下载压缩包。

### 引入

* 你可以使用 `script` 标签引入

Expand All @@ -54,7 +84,7 @@ lajax 尝试解决这些问题:
import Lajax from './src/lajax-module';
```

### 基本使用
### 使用

```js
// 传入接口服务器地址进行实例化
Expand All @@ -76,9 +106,9 @@ try {
```
详细文档请查看 [Api](#api)

### 如何打包
### 打包

lajax 使用了 ES2015 语法,当前需要使用 webpack 打包后才能正常使用
lajax 使用了 ES2015 语法,需要使用 [webpack](https://webpack.js.org/) + [babel](http://babeljs.io/) 进行打包

* 安装必须的模块

Expand Down Expand Up @@ -150,11 +180,15 @@ const logger = new Lajax({
</tr>
<tr>
<td>logAjaxFilter</td>
<td>ajax 自动记录条件过滤,函数传参是该次请求的 url 和 method,若返回 true 代表记录日志,false 代表不记录日志</td>
<td>ajax 自动记录条件过滤函数,函数传参是该次请求的 url 和 method,若返回 true 代表记录日志,false 代表不记录日志</td>
<td>function</td>
<td><pre><code>function(ajaxUrl, ajaxMethod) {
<td><pre lang="js">/**
* @param {string} ajaxUrl - 请求url
* @param {string} ajaxMethod - 请求方式
*/
function(ajaxUrl, ajaxMethod) {
return true;
}</code></pre></td>
}</pre></td>
</tr>
<tr>
<td>stylize</td>
Expand All @@ -170,9 +204,22 @@ const logger = new Lajax({
</tr>
<tr>
<td>customDesc</td>
<td>自定义的描述信息内容,函数传参为上次未发送日志条数、当前请求 id、请求 id 是否来自服务端,返回一个字符串</td>
<td>生成自定义描述信息的函数,传参为上次页面卸载前未发送的日志数、当前请求 id、请求 id 是否来自服务端,返回一个字符串</td>
<td>function</td>
<td>-</td>
<td><pre lang="js">/**
* @param {number} lastUnsend - 上次页面卸载前未发送的日志数
* @param {string} reqId - 请求id
* @param {boolean} idFromServer - 请求id是否来自服务器
*/
function(lastUnsend, reqId, idFromServer) {
return `🚀 lajax 前端日志模块加载完成。
自动记录页面错误: ${this.autoLogError ? '✔' : '✘'}
自动记录Promise异常: ${this.autoLogRejection ? '✔' : '✘'}
自动记录Ajax请求: ${this.autoLogAjax ? '✔' : '✘'}
当前页面请求id:${reqId}${idFromServer
? ' (来自服务端)'
: ' (自动生成)'}`;
}</pre></td>
</tr>
<tr>
<td>interval</td>
Expand Down
20 changes: 18 additions & 2 deletions dist/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
this.autoLogAjax = config.autoLogAjax == null ? true : config.autoLogAjax;

// 默认的 ajax 自动记录情况过滤
var defaultLogAjaxFilter = function defaultLogAjaxFilter() {
var defaultLogAjaxFilter = function defaultLogAjaxFilter(ajaxUrl, ajaxMethod) {
return true;
};

Expand Down Expand Up @@ -260,6 +260,22 @@
}
}

/**
* 默认的描述信息方法
*
* @param {number} lastUnsend - 上次页面卸载前未发送的日志数
* @param {string} reqId - 请求id
* @param {boolean} idFromServer - 请求id是否来自服务器
* @returns 最终的描述信息
* @memberof Lajax
*/

}, {
key: '_defaultDesc',
value: function _defaultDesc(lastUnsend, reqId, idFromServer) {
return '\uD83D\uDE80 lajax \u524D\u7AEF\u65E5\u5FD7\u6A21\u5757\u52A0\u8F7D\u5B8C\u6210\u3002\n\u81EA\u52A8\u8BB0\u5F55\u9875\u9762\u9519\u8BEF\uFF1A ' + (this.autoLogError ? '✔' : '✘') + '\n\u81EA\u52A8\u8BB0\u5F55Promise\u5F02\u5E38\uFF1A ' + (this.autoLogRejection ? '✔' : '✘') + '\n\u81EA\u52A8\u8BB0\u5F55Ajax\u8BF7\u6C42\uFF1A ' + (this.autoLogAjax ? '✔' : '✘') + '\n\u5F53\u524D\u9875\u9762\u8BF7\u6C42id\uFF1A' + reqId + (idFromServer ? ' (来自服务端)' : ' (自动生成)');
}

/**
* 打印描述信息
*
Expand All @@ -276,7 +292,7 @@
desc = '%c' + this.customDesc(this.lastUnsend, this.reqId, this.idFromServer);
} else {
// 默认描述
desc = '%c\uD83D\uDE80 lajax \u524D\u7AEF\u65E5\u5FD7\u6A21\u5757\u52A0\u8F7D\u5B8C\u6210\u3002\n\u81EA\u52A8\u8BB0\u5F55\u9875\u9762\u9519\u8BEF\uFF1A ' + (this.autoLogError ? '✔' : '✘') + '\n\u81EA\u52A8\u8BB0\u5F55Promise\u5F02\u5E38\uFF1A ' + (this.autoLogRejection ? '✔' : '✘') + '\n\u81EA\u52A8\u8BB0\u5F55Ajax\u8BF7\u6C42\uFF1A ' + (this.autoLogAjax ? '✔' : '✘') + '\n\u5F53\u524D\u9875\u9762\u8BF7\u6C42id\uFF1A' + this.reqId + (this.idFromServer ? ' (来自服务端)' : ' (自动生成)');
desc = '%c' + this._defaultDesc(this.lastUnsend, this.reqId, this.idFromServer);
}
console.log(desc, 'color: ' + Lajax.colorEnum.desc + '; font-family: \u5B8B\u4F53; line-height: 1.5;');
}
Expand Down
Loading

0 comments on commit b503d85

Please sign in to comment.