-
Notifications
You must be signed in to change notification settings - Fork 2
/
client-logger.js
51 lines (43 loc) · 1.18 KB
/
client-logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pino from 'pino'
import dayjs from 'dayjs'
const isServerRendering = (function () {
try {
return !(typeof window !== 'undefined' && document !== undefined)
} catch (e) {
return true
}
})()
const handlerServerLogger = () => {
if (isServerRendering && process.env.NODE_ENV !== 'development') {
return pino.destination(`${process.cwd()}/logger.log`)
}
}
const config = {
serverUrl:
process.env.NODE_ENV === 'development'
? 'http://localhost:3000'
: process.env.NEXT_PUBLIC_API_URL,
}
const pinoConfig = {
timestamp: () => `,"time":"${dayjs().format('YYYY-MM-DD HH:mm:ss')}"`,
browser: {
asObject: true,
},
}
if (config.serverUrl) {
pinoConfig.browser.transmit = {
level: 'info',
send: (level, logEvent) => {
const msg = logEvent.messages[0]
console.log(`Logger 日志输出地址 : ${config.serverUrl}/api/log`)
// client 端上传日志
fetch(`${config.serverUrl}/api/log`, {
method: 'POST',
body: JSON.stringify({ msg, level }),
})
},
}
}
const clientLogger = pino(pinoConfig, handlerServerLogger())
export const clientLog = (msg) => clientLogger.info(msg)
export default clientLogger