aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers/logger.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-07-15 15:30:14 +0200
committerChocobozzz <me@florianbigard.com>2022-07-18 11:37:18 +0200
commit42b40636991b97fe818007fab19091764fc5db73 (patch)
treedb431787c06ce898d22e91ff771f795219274fc6 /client/src/root-helpers/logger.ts
parent654d4ede7fa4d0faa71e49bcfab6b65a686397b2 (diff)
downloadPeerTube-42b40636991b97fe818007fab19091764fc5db73.tar.gz
PeerTube-42b40636991b97fe818007fab19091764fc5db73.tar.zst
PeerTube-42b40636991b97fe818007fab19091764fc5db73.zip
Add ability for client to create server logs
Diffstat (limited to 'client/src/root-helpers/logger.ts')
-rw-r--r--client/src/root-helpers/logger.ts138
1 files changed, 138 insertions, 0 deletions
diff --git a/client/src/root-helpers/logger.ts b/client/src/root-helpers/logger.ts
new file mode 100644
index 000000000..cd559cfa7
--- /dev/null
+++ b/client/src/root-helpers/logger.ts
@@ -0,0 +1,138 @@
1import { ClientLogCreate } from '@shared/models/server'
2import { peertubeLocalStorage } from './peertube-web-storage'
3import { UserTokens } from './users'
4
5export type LoggerHook = (message: LoggerMessage, meta?: LoggerMeta) => void
6export type LoggerLevel = 'info' | 'warn' | 'error'
7
8export type LoggerMessage = string | Error | object
9export type LoggerMeta = Error | { [ id: string ]: any, err?: Error }
10
11declare global {
12 interface Window {
13 logger: Logger
14 }
15}
16
17class Logger {
18 private readonly hooks: { level: LoggerLevel, hook: LoggerHook }[] = []
19
20 info (message: LoggerMessage, meta?: LoggerMeta) {
21 this.runHooks('info', message, meta)
22
23 if (meta) console.log(message, meta)
24 else console.log(message)
25 }
26
27 warn (message: LoggerMessage, meta?: LoggerMeta) {
28 this.runHooks('warn', message, meta)
29
30 if (meta) console.warn(message, meta)
31 else console.warn(message)
32 }
33
34 error (message: LoggerMessage, meta?: LoggerMeta) {
35 this.runHooks('error', message, meta)
36
37 if (meta) console.error(message, meta)
38 else console.error(message)
39 }
40
41 addHook (level: LoggerLevel, hook: LoggerHook) {
42 this.hooks.push({ level, hook })
43 }
44
45 registerServerSending (serverUrl: string) {
46 this.addHook('warn', (message, meta) => this.sendClientLog(serverUrl, this.buildServerLogPayload('warn', message, meta)))
47 this.addHook('error', (message, meta) => this.sendClientLog(serverUrl, this.buildServerLogPayload('error', message, meta)))
48 }
49
50 sendClientLog (serverUrl: string, payload: ClientLogCreate | null) {
51 if (!payload) return
52
53 const headers = new Headers({
54 Accept: 'application/json',
55 'Content-Type': 'application/json'
56 })
57
58 try {
59 const tokens = UserTokens.getUserTokens(peertubeLocalStorage)
60
61 if (tokens) headers.set('Authorization', `${tokens.tokenType} ${tokens.accessToken}`)
62 } catch (err) {
63 console.error('Cannot set tokens to client log sender.', { err })
64 }
65
66 try {
67 fetch(serverUrl + '/api/v1/server/logs/client', {
68 headers,
69 method: 'POST',
70 body: JSON.stringify(payload)
71 })
72 } catch (err) {
73 console.error('Cannot send client warn/error to server.', err)
74 }
75 }
76
77 private buildServerLogPayload (level: Extract<LoggerLevel, 'warn' | 'error'>, message: LoggerMessage, meta?: LoggerMeta) {
78 if (!message) return null
79
80 return {
81 message: this.buildMessageServerLogPayload(message),
82 userAgent: navigator.userAgent,
83 url: window.location.href,
84 level,
85 stackTrace: this.buildStackServerLogPayload(message, meta),
86 meta: this.buildMetaServerLogPayload(meta)
87 }
88 }
89
90 private buildMessageServerLogPayload (message: LoggerMessage) {
91 if (typeof message === 'string') return message
92 if (message instanceof Error) return message.message
93
94 return JSON.stringify(message)
95 }
96
97 private buildStackServerLogPayload (message: LoggerMessage, meta?: LoggerMeta) {
98 if (message instanceof Error) return message.stack
99 if (meta instanceof Error) return meta.stack
100 if (meta?.err instanceof Error) return meta.err.stack
101
102 return undefined
103 }
104
105 private buildMetaServerLogPayload (meta?: LoggerMeta) {
106 if (!meta) return undefined
107 if (meta instanceof Error) return undefined
108
109 let result: string
110
111 try {
112 result = JSON.stringify(meta, (key, value) => {
113 if (key === 'err') return undefined
114
115 return value
116 })
117 } catch (err) {
118 console.error('Cannot stringify meta.', err)
119 }
120
121 return result
122 }
123
124 private runHooks (level: LoggerLevel, message: LoggerMessage, meta?: LoggerMeta) {
125 for (const hookObj of this.hooks) {
126 if (hookObj.level !== level) continue
127
128 hookObj.hook(message, meta)
129 }
130 }
131}
132
133const logger = window.logger || new Logger()
134window.logger = logger
135
136export {
137 logger
138}