Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增interceptTransform 方法的meta参数 #12

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ module.exports = {
],
devdep: [
'egg',
'egg-ci',
'egg-bin',
'autod',
'autod-egg',
'eslint',
'eslint-config-egg',
'webstorm-disable-index',
],
exclude: [
'./test/fixtures',
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ coverage/
run/
.DS_Store
*.swp

.env
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
sudo: false
language: node_js
node_js:
- '8'
- '10'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
env:
global:
- secure: bfww4kWiyqD1bzAFiA6GXiOP5dWkSGEZMJp7ED0jRvNmn5aJUEeah+kpkE5BZzQD6u9XGOnSRZU3AkuAZsyeOAooom+wvE1LLYGwclQrVJGjJ9i4N/fvnQkfo9XCYbEn7V12flXKChTLE0kAhWHwU6g/JPYLs8rIh0MzOTIF2W93NiqskuPn1UtNVO+6T573uie/vkfVhCgct88kckZz+oT7Tba+lBfbbGLR/t7Jynn9MkE/nIMX74fNQ09DSpeQIf1QgWlbK6znpi47sakEFnMOJuy6x04YUfrHBAgYVKKSoanO6OT/sUhV2pTchwThWbmvMyx43nY7i9/TNy3wJ1lHZvxV1socsrZP1I6hUsggNfoDrxZ9OOh1gsIpFbTwL7T00JRuGet2gbsVv9/saCoj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RsxaZgf6H1mDubFNW8ErMyjuh5WrJ8V6pnpExak/1eqzebHA57LrOjXOYnbmNusMnP0jxH0XhBjfqRxroSpVW6OCSImuIFvaM+SiGkrx5gV4ndlhkHS0tKgznoa63a0ggJ1axWKkHEzh/74tfm91w1XBqfNEUa/X4CzuWoKtnT7UCn8U1tntZe6Rt738bs/ifP4AS8qBddlNvWoFeUQfho/picu6BkT58beGJ7Cvb9X/YAUTM=
42 changes: 42 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

1.3.0 / 2018-09-18
==================

**features**
* [[`a9b26f7`](http://github.com/eggjs/egg-logger-sls/commit/a9b26f7ded2b1c0eaa8fe7ad662f1373e75b8a65)] - feat: use slsLevel for slsTransport (#10) (Haoliang Gao <<[email protected]>>)

1.2.1 / 2018-09-15
==================

**fixes**
* [[`af5213e`](http://github.com/eggjs/egg-logger-sls/commit/af5213ecd1f33a650fdabc7d302cc39d11639437)] - fix: the level of transport should extend from logger (#9) (Haoliang Gao <<[email protected]>>)

1.2.0 / 2018-09-14
==================

**features**
* [[`dea554d`](http://github.com/eggjs/egg-logger-sls/commit/dea554d9aaae53b6d6fa6363d61445be1915899b)] - feat: transform has the second argument (#8) (Haoliang Gao <<[email protected]>>)

1.1.0 / 2018-08-26
==================

**features**
* [[`97ebc0c`](http://github.com/eggjs/egg-logger-sls/commit/97ebc0c5fe3d8cdf1cf7e26516e9b9f057439f94)] - feat: support transform (#7) (Haoliang Gao <<[email protected]>>)
* [[`c6089fd`](http://github.com/eggjs/egg-logger-sls/commit/c6089fd7e4a4381830288f29daf4d8e42b7c1184)] - feat: add env (#5) (Haoliang Gao <<[email protected]>>)
* [[`1256096`](http://github.com/eggjs/egg-logger-sls/commit/125609682871c6def6c4aca5fb6d4ce132e0e1a7)] - feat: logger can be disabled (#4) (Haoliang Gao <<[email protected]>>)

**fixes**
* [[`dac0ad4`](http://github.com/eggjs/egg-logger-sls/commit/dac0ad4a83642ae2032e635922c4e83062cf64dc)] - fix: sls transport level is INFO (#6) (Haoliang Gao <<[email protected]>>)
* [[`5cf1bac`](http://github.com/eggjs/egg-logger-sls/commit/5cf1bac755fb5e28f6ff2f687188af274c1cd2fc)] - fix: loggerName should be logger key name not class name (#3) (Haoliang Gao <<[email protected]>>)

**others**
* [[`517edd0`](http://github.com/eggjs/egg-logger-sls/commit/517edd037d2f7e9107306e4e14461f5672b5ec42)] - chore: remove egg-ci from autod (#2) (Haoliang Gao <<[email protected]>>)

1.0.0 / 2018-08-26
==================

**features**
* [[`60f773c`](http://github.com/eggjs/egg-logger-sls/commit/60f773cdb76055a9b5d4370e2f7d3edbc3074727)] - feat: first implement (#1) (Haoliang Gao <<[email protected]>>),fatal: No names found, cannot describe anything.

**others**

130 changes: 125 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@
[download-image]: https://img.shields.io/npm/dm/egg-logger-sls.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-logger-sls

<!--
Description here.
-->
Logger transport for aliyun sls.

## Install

```bash
$ npm i egg-sls --save
$ npm i egg-logger-sls --save
```

## Usage

```js
// {app_root}/config/plugin.js
exports.sls = {
enable: true,
package: 'egg-sls',
};
exports.loggerSLS = {
enable: true,
package: 'egg-logger-sls',
Expand All @@ -42,17 +45,132 @@ exports.loggerSLS = {

## Configuration

You should configure [egg-sls] first.

```js
// {app_root}/config/config.default.js
exports.loggerSLS = {
// sls client name
client: null,
// sls project name
project: '',
// sls logstore name
logstore: '',
// the list of logger name that can be disabled
disable: [],
// the function that can modify and filter the logs
transform: null,
};
```

If client is not specified, it will use `app.sls` as default client, otherwise it will get the sls client with the specified name in multiple client case.

```js
// {app_root}/config/config.default.js
exports.sls = {
clients: {
sls: {
endpoint: process.env.SLS_ENDPOINT,
accessKeyId: process.env.SLS_ACCESS_KEY_ID,
accessKeySecret: process.env.SLS_ACCESS_KEY_SECRET,
},
},
};
exports.loggerSLS = {
client: 'sls',
project: '',
logstore: '',
};
```

see [config/config.default.js](config/config.default.js) for more detail.

## Example
## Usage

The only thing you should do is configuration, this module will upload log automatically.

### Disable Logger

You and disable logger with `disable` config.

```js
exports.customLogger = {
myLogger: {
file: '/path/to/log',
},
};
exports.loggerSLS = {
disable: [
// won't upload this logger
'myLogger',
],
}
```

### Transform and Filter

You can transform the log data before upload.

```js
exports.loggerSLS = {
transform(log, args, meta) {
return log;
},
}
```

If you want to ignore some log, you can return false when transform.

```js
exports.loggerSLS = {
transform(log) {
if (some condition) return false;
return log;
},
}
```

There is two arguments that transform have.

1. One is log object, it contains the default property, which is `ip`, `hostname`, `env`, `appName`, `loggerName`, `loggerFileName`, `level`. The last one is `content` that is the log string formated from the arguments when you call log method.
1. The other is original arguments when you call log method, it will not format to content.

### Data Structure

The data structure uploaded in below, you can create index in aliyun console as your wish.

- level: logger level
- content: the infomation that you logged
- ip: the host ip
- hostname: the host name
- env: the egg environment
- appName: the package name
- loggerName: the logger name defined by Egg
- loggerFileName: the logger file path

### Logger Level

The default logger in Egg contains two level: level and consoleLevel, If you want set level for sls, you can use `slsLevel`.

```js
// config/config.default.js
module.exports = {
logger: {
// for all logs
slsLevel: 'DEBUG',
},

customLogger: {
// only for one log
testLogger: {
slsLevel: 'ERROR',
},
},
};
```

If `slsLevel` is not specified, it will use level instead.

<!-- example here -->

## Questions & Suggestions

Expand All @@ -61,3 +179,5 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
## License

[MIT](LICENSE)

[egg-sls]: https://github.com/eggjs/egg-sls
76 changes: 0 additions & 76 deletions README.zh_CN.md

This file was deleted.

24 changes: 24 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const SLSLoggerClient = require('./lib/sls_logger_client');
const SLSTransport = require('./lib/sls_transport');

module.exports = app => {
const config = app.config.loggerSLS;
const client = new SLSLoggerClient(app);

for (const [ name, logger ] of app.loggers.entries()) {
if (config.disable.includes(name)) continue;

const transport = new SLSTransport({
level: logger.options.slsLevel || logger.options.level,
client,
transform: config.transform,
env: app.config.env,
appName: app.config.name,
loggerName: name,
loggerFileName: logger.options.file,
});
logger.set('sls', transport);
}
};
14 changes: 11 additions & 3 deletions config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
'use strict';

/**
* egg-logger-sls default config
* default config
* @member Config#loggerSLS
* @property {String} SOME_KEY - some description
* @property {String} client - sls client name in egg-sls
* @property {String} project - sls project name
* @property {String} logstore - sls logstore name
* @property {Array} disable - the list of logger name that can be disabled
* @property {Function} transform - the function that can modify and filter the logs
*/
exports.loggerSLS = {

client: null,
project: '',
logstore: '',
disable: [],
transform: null,
};
Loading