Skip to content

Commit 5dcec2c

Browse files
committed
🍌 docs: add nuxt/server-side-logging
1 parent 6d8a889 commit 5dcec2c

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@
6969

7070
- [気をつけるポイント]()
7171
- [ログイン処理をどう実装するか]()
72+
- [サーバーサイドのログ処理](nuxt/server-side-logging.md)
7273
- [Google Tag Manager]()

docs/nuxt/server-side-logging.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## サーバーサイドのログ処理
2+
3+
サーバーサイドというのはつまり `SSR` 時ですが、サーバーのリソースにダイレクトにログを保存するのは、スケールアウトを意識していない環境かもしれませんが、
4+
5+
サクッと `AWS EC2` 一台で作るときには、必要になってくると思います。
6+
7+
## Nuxt.js に導入するには?
8+
9+
今回は拙作の logger を使っております。
10+
11+
[lemon-sour/node-log-rotate: Just a very simple logging module for your node.js application](https://github.com/lemon-sour/node-log-rotate)
12+
13+
plugin として `$log` を inject するパターンを紹介しておきます。
14+
15+
### plugin/node-log-rotate.ts
16+
17+
```javascript
18+
import { Context } from '@nuxt/vue-app'
19+
import { setup, log } from 'node-log-rotate'
20+
import { pjName } from '@/common/constants/'
21+
22+
// logger のセットアップ
23+
setup({
24+
appName: pjName
25+
})
26+
27+
export default (context: Context, inject: any): void => {
28+
// https://github.com/nuxt-community/axios-module/blob/dev/lib/plugin.js#L200
29+
context.$log = log
30+
inject('log', log)
31+
}
32+
```
33+
34+
### plugin/axios.ts
35+
36+
```javascript
37+
export default ({ $axios, $log, app, req, error }): void => {
38+
/**
39+
* $axios.onResponse
40+
*/
41+
$axios.onResponse((response): void => {
42+
const token = response.headers[app.$C.ACCESS_TOKEN_NAME]
43+
const { status, statusText, config, data } = response
44+
// SSR 時だけ log を実行する
45+
if (process.server) {
46+
$log(`${config.url}:${status}`)
47+
}
48+
})
49+
50+
/**
51+
* $axios.onResponseError
52+
*/
53+
$axios.onResponseError((response: AxiosError): void => {
54+
console.log('$axios.onResponseError')
55+
// 通信エラー
56+
if (!response.response) {
57+
return
58+
}
59+
60+
const { status, statusText, config } = response.response
61+
62+
// SSR 時だけ log を実行する
63+
if (process.server) {
64+
$log(`${config.url}:${status}`)
65+
}
66+
})
67+
}
68+
```
69+
70+
### nuxt.confit.ts
71+
72+
`fs` などの node.js 組み込みモジュールを使う場合は、 webpack のバンドルを意識する必要があります。
73+
74+
以下のようにすることで、 fs を browserify されないようにできます。
75+
76+
```json
77+
build: {
78+
// https://github.com/nuxt-community/dotenv-module/issues/11
79+
config.node = {
80+
fs: 'empty'
81+
}
82+
}
83+
```
84+
85+
plugin は配列に入れる順番を意識しないと plugin から plugin を呼ぶことはできません。
86+
87+
たとえば、 axios.ts から node-log-rotate.ts の plugin を呼ぶには以下のようにしてください。
88+
89+
```json
90+
plugins: [
91+
{ src: '@/plugins/libraries/node-log-rotate.ts', mode: 'server' },
92+
'@/plugins/libraries/axios.ts'
93+
]
94+
```
95+
96+
## サンプルのプルリク
97+
98+
[Feature/add server side logger by hisasann · Pull Request #3 · typescript-nuxtjs-boilerplate/typescript-nuxtjs-boilerplate](https://github.com/typescript-nuxtjs-boilerplate/typescript-nuxtjs-boilerplate/pull/3)

0 commit comments

Comments
 (0)