Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from freee/feature/APITEAM-3341_logger
Browse files Browse the repository at this point in the history
logger追加
  • Loading branch information
soga-yuichi authored Apr 28, 2021
2 parents b10d02c + 828e810 commit 3f45864
Show file tree
Hide file tree
Showing 21 changed files with 8,316 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
extends: ['eslint-config-freee-typescript'],
rules: {
'import/no-duplicates': 'off',
'import/namespace': 'off',
'import/no-unresolved': 'off'
},
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2021 freee K.K.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
247 changes: 247 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
# 会計freee logger

出力をマスキングするためのloggerです。

## できること

- 下記のメソッドを提供します。
- debug
- info
- warn
- error
- fatal
- 下記キーワードがloggerの引数に含まれる時はマスキングします。
- RAW_POST_DATA
- password
- card_number
- cvc
- my_number
- token
- secret
- Authorization
- payee_code
- email
- phone
- street_name
- contact_name
- first_name
- last_name
- given_name
- family_name
- display_name
- transfer_name
- expire_month
- expire_year
- wallet_keys
- wallet_login_key
- wallet_login_secrets
- question
- answer
- approvalcode
- approval_code
- identificationnumber
- identification_number
- application_uid
- application_secret
- credentials
- common_key
- nonce
- certificate
- passphrase
- crt_file
- crt_pass
- amount
- balance
- Signature
- AccessKey
- assertion
- accessToken
- refreshToken

## できないこと
- コンソール以外へのログ出力

## このloggerについて

本 loggerは、β版のステータスとなっており、制限およびバグがある可能性があります。また、このステータスの記載が更新されるまでは、インターフェイスに変更が入る可能性がありますので、ご利用の際はご留意ください。


本 logger を利用する前に下記をご確認ください。

- node上で動作すること


### インストール

該当のプロジェクトディレクトリで以下のコマンドを実行してください。

`npm install freee-logger`

### 開発

- リリースファイル作成時
- lib/index.d.ts, lib/index.js, lib/index.js.mapが更新されます。

```
npm run build
```

- ローカルでの修正、動作確認時

```
npm run build
```


- eslintチェック

```
npm run lint
```

- eslint fix
- 自動修正可能なファイルは修正されます。

```
npm run lint_fix
```


### サンプル


- デフォルトログ出力

```
const logger = getLogger('logger');
const maskingData = {
password: 'pass',
test_1: { accessToken: 'test' },
test2_1: { test2_2: { card_number: 'card_number' } },
test3_1: {
common_key: 'common_key',
test3_2: {
payee_code: 'payee_code',
test3_3: {
street_name: 'street_name',
test3_4: {
AccessKey: 'AccessKey', // eslint-disable-line @typescript-eslint/naming-convention
test3_5: {
first_name: 'first_name',
test3_6: {
last_name: 'last_name',
test3_7: {
expire_year: 'expire_year'
}
}
}
}
}
}
},
'test.test': 'test.test'
};
logger.debug(maskingData);
# キーワードがマスキングされて出力されます。
[2021-04-26T15:36:33.098] [DEBUG] logger - {"password":"****","test_1":{"accessToken":"****"},"test2_1":{"test2_2":{"card_number":"***********"}},"test3_1":{"common_key":"**********","test3_2":{"payee_code":"**********","test3_3":{"street_name":"***********","test3_4":{"AccessKey":"*********","test3_5":{"first_name":"too deep","test3_6":"too deep"}}}}},"test.test":"test.test"}
```

- マスキングキーワードを変更して、ログ出力
- 下記ではキーワードを```test2_1```, ```test.test```のみに変更しています。

```
const maskingFields = ['test2_1', 'test.test'];
const logger = getLogger('masking', maskingFields);
const maskingData = {
password: 'pass',
test_1: { accessToken: 'test' },
test2_1: { test2_2: { card_number: 'card_number' } },
test3_1: {
common_key: 'common_key',
test3_2: {
payee_code: 'payee_code',
test3_3: {
street_name: 'street_name',
test3_4: {
AccessKey: 'AccessKey', // eslint-disable-line @typescript-eslint/naming-convention
test3_5: {
first_name: 'first_name',
test3_6: {
last_name: 'last_name',
test3_7: {
expire_year: 'expire_year'
}
}
}
}
}
}
},
'test.test': 'test.test'
};
logger.debug(maskingData);
# 指定したキーワードがマスキングされて出力されます。
[2021-04-26T15:31:19.035] [DEBUG] masking - {"password":"pass","test_1":{"accessToken":"test"},"test2_1":"***************","test3_1":{"common_key":"common_key","test3_2":{"payee_code":"payee_code","test3_3":{"street_name":"street_name","test3_4":{"AccessKey":"AccessKey","test3_5":{"first_name":"too deep","test3_6":"too deep"}}}}},"test.test":"*********"}
```


- マスキングの深さ(デフォルト5)を変更
- getLoggerの第3引数に深さを指定できます。
- 第3引数のマスキングキーワードをデフォルトのままにする場合はundefinedを指定します。

```
const maxDepth = 6;
const logger = getLogger('masking', undefined, maxDepth);
const maskingData = {
password: 'pass',
test_1: { accessToken: 'test' },
test2_1: { test2_2: { card_number: 'card_number' } },
test3_1: {
common_key: 'common_key',
test3_2: {
payee_code: 'payee_code',
test3_3: {
street_name: 'street_name',
test3_4: {
AccessKey: 'AccessKey', // eslint-disable-line @typescript-eslint/naming-convention
test3_5: {
first_name: 'first_name',
test3_6: {
last_name: 'last_name',
test3_7: {
expire_year: 'expire_year'
}
}
}
}
}
}
},
'test.test': 'test.test'
};
logger.debug(maskingData);
# 指定した改装までマスキングされ、以降は切り落とされます。
[2021-04-26T15:28:42.563] [DEBUG] masking - {"password":"****","test_1":{"accessToken":"****"},"test2_1":{"test2_2":{"card_number":"***********"}},"test3_1":{"common_key":"**********","test3_2":{"payee_code":"**********","test3_3":{"street_name":"***********","test3_4":{"AccessKey":"*********","test3_5":{"first_name":"**********","test3_6":{"last_name":"too deep","test3_7":"too deep"}}}}}},"test.test":"test.test"}
```


## ライセンス

ライセンスについては下記をご参照ください。

[MIT License](./LICENSE)
17 changes: 17 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
"roots": [
`${__dirname}/src`
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
}
Empty file added lib/.keep
Empty file.
21 changes: 21 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
declare module 'freee-logger' {
/* eslint-disable @typescript-eslint/no-explicit-any */
export type Logger = {
debug(message: any, ...args: any[]): void;

info(message: any, ...args: any[]): void;

warn(message: any, ...args: any[]): void;

error(message: any, ...args: any[]): void;

fatal(message: any, ...args: any[]): void;
};
/* eslint-enable @typescript-eslint/no-explicit-any */

export function getLogger(
category: string,
maskingFields?: string[],
maxDepth?: number
): Logger;
}
2 changes: 2 additions & 0 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f45864

Please sign in to comment.