@bifold/remote-logs 2.4.6 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@bifold/remote-logs",
3
- "version": "2.4.6",
3
+ "version": "2.5.0",
4
4
  "description": "Remote logging for credo-ts agents",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
7
7
  "source": "src/index.ts",
8
8
  "files": [
9
- "src",
10
9
  "build"
11
10
  ],
12
11
  "license": "Apache-2.0",
@@ -48,7 +47,7 @@
48
47
  "hoistingLimits": "workspaces"
49
48
  },
50
49
  "dependencies": {
51
- "@bifold/core": "2.4.6",
50
+ "@bifold/core": "2.5.0",
52
51
  "@credo-ts/core": "0.5.13",
53
52
  "axios": "~1.4.0",
54
53
  "buffer": "~6.0.3",
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './logger'
2
- export { RemoteLoggerOptions } from './transports'
package/src/logger.ts DELETED
@@ -1,174 +0,0 @@
1
- import { BifoldError, BifoldLogger } from '@bifold/core'
2
- import { DeviceEventEmitter, EmitterSubscription } from 'react-native'
3
- import { consoleTransport, logger } from 'react-native-logs'
4
-
5
- import { RemoteLoggerOptions, lokiTransport } from './transports'
6
-
7
- export enum RemoteLoggerEventTypes {
8
- ENABLE_REMOTE_LOGGING = 'RemoteLogging.Enable',
9
- }
10
-
11
- export class RemoteLogger extends BifoldLogger {
12
- private _remoteLoggingEnabled = false
13
- private _sessionId: number | undefined
14
- private _autoDisableRemoteLoggingIntervalInMinutes = 0
15
- private lokiUrl: string | undefined
16
- private lokiLabels: Record<string, string>
17
- private remoteLoggingAutoDisableTimer: ReturnType<typeof setTimeout> | undefined
18
- private eventListener: EmitterSubscription | undefined
19
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
- protected _log: any
21
- protected _config = {
22
- levels: {
23
- test: 0,
24
- trace: 0,
25
- debug: 0,
26
- info: 1,
27
- warn: 2,
28
- error: 3,
29
- fatal: 4,
30
- },
31
- severity: 'debug',
32
- async: true,
33
- dateFormat: 'time',
34
- printDate: false,
35
- }
36
-
37
- constructor(options: RemoteLoggerOptions) {
38
- super()
39
-
40
- this.lokiUrl = options.lokiUrl ?? undefined
41
- this.lokiLabels = options.lokiLabels ?? {}
42
- this._autoDisableRemoteLoggingIntervalInMinutes = options.autoDisableRemoteLoggingIntervalInMinutes ?? 0
43
-
44
- this.configureLogger()
45
- }
46
-
47
- get sessionId(): number {
48
- if (!this._sessionId) {
49
- this._sessionId = Math.floor(100000 + Math.random() * 900000)
50
- }
51
-
52
- return this._sessionId
53
- }
54
-
55
- set sessionId(value: number) {
56
- this._sessionId = value
57
-
58
- this.configureLogger()
59
- }
60
-
61
- get autoDisableRemoteLoggingIntervalInMinutes(): number {
62
- return this._autoDisableRemoteLoggingIntervalInMinutes
63
- }
64
-
65
- get remoteLoggingEnabled(): boolean {
66
- return this._remoteLoggingEnabled
67
- }
68
-
69
- set remoteLoggingEnabled(value: boolean) {
70
- this._remoteLoggingEnabled = value
71
-
72
- if (value === false) {
73
- this._sessionId = undefined
74
- }
75
-
76
- this.configureLogger()
77
- }
78
-
79
- private configureLogger() {
80
- const transportOptions = {}
81
- const transport = [consoleTransport]
82
- const config = {
83
- ...this._config,
84
- transport,
85
- transportOptions,
86
- }
87
-
88
- if (this.remoteLoggingEnabled && this.lokiUrl) {
89
- transport.push(lokiTransport)
90
- config['transportOptions'] = {
91
- lokiUrl: this.lokiUrl,
92
- lokiLabels: {
93
- ...this.lokiLabels,
94
- session_id: `${this.sessionId}`,
95
- },
96
- }
97
-
98
- if (this.autoDisableRemoteLoggingIntervalInMinutes && this.autoDisableRemoteLoggingIntervalInMinutes > 0) {
99
- this.remoteLoggingAutoDisableTimer = setTimeout(() => {
100
- this.remoteLoggingEnabled = false
101
- }, this.autoDisableRemoteLoggingIntervalInMinutes * 60000)
102
- }
103
- }
104
-
105
- this._log = logger.createLogger<'test' | 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'>(config)
106
- }
107
-
108
- public startEventListeners() {
109
- this.eventListener = DeviceEventEmitter.addListener(RemoteLoggerEventTypes.ENABLE_REMOTE_LOGGING, (value) => {
110
- this.remoteLoggingEnabled = value
111
- })
112
- }
113
-
114
- public stopEventListeners() {
115
- this.eventListener = undefined
116
- }
117
-
118
- public overrideCurrentAutoDisableExpiration(expirationInMinutes: number) {
119
- if (expirationInMinutes <= 0) {
120
- return
121
- }
122
-
123
- if (this.remoteLoggingAutoDisableTimer) {
124
- clearTimeout(this.remoteLoggingAutoDisableTimer)
125
- }
126
-
127
- this.remoteLoggingAutoDisableTimer = setTimeout(() => {
128
- this.remoteLoggingEnabled = false
129
- }, expirationInMinutes * 60000)
130
- }
131
-
132
- public test(...msgs: unknown[]): void {
133
- this._log?.test(...this.messageFormatter(...msgs))
134
- }
135
-
136
- public trace(...msgs: unknown[]): void {
137
- this._log?.trace(...this.messageFormatter(...msgs))
138
- }
139
-
140
- public debug(...msgs: unknown[]): void {
141
- this._log?.debug(...this.messageFormatter(...msgs))
142
- }
143
-
144
- public info(...msgs: unknown[]): void {
145
- this._log?.info(...this.messageFormatter(...msgs))
146
- }
147
-
148
- public warn(...msgs: unknown[]): void {
149
- this._log?.warn(...this.messageFormatter(...msgs))
150
- }
151
-
152
- public error(...msgs: unknown[]): void {
153
- this._log?.error(...this.messageFormatter(...msgs))
154
- }
155
-
156
- public fatal(...msgs: unknown[]): void {
157
- this._log?.fatal(...this.messageFormatter(...msgs))
158
- }
159
-
160
- public report(bifoldError: BifoldError): void {
161
- this._log?.info({ message: 'Sending Loki report' })
162
- const { title, description, code, message } = bifoldError
163
- lokiTransport({
164
- msg: title,
165
- rawMsg: [{ message: title, data: { title, description, code, message } }],
166
- level: { severity: 3, text: 'error' },
167
- options: {
168
- lokiUrl: this.lokiUrl,
169
- lokiLabels: this.lokiLabels,
170
- job: 'incident-report',
171
- },
172
- })
173
- }
174
- }
package/src/transports.ts DELETED
@@ -1,78 +0,0 @@
1
- /* eslint-disable no-console */
2
- import axios from 'axios'
3
- import { Buffer } from 'buffer'
4
- import { transportFunctionType } from 'react-native-logs'
5
-
6
- export interface RemoteLoggerOptions {
7
- lokiUrl?: string
8
- lokiLabels?: Record<string, string>
9
- autoDisableRemoteLoggingIntervalInMinutes?: number
10
- job?: string
11
- }
12
-
13
- export type LokiTransportProps = {
14
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
- msg: any
16
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
- rawMsg: any
18
- level: {
19
- severity: number
20
- text: string
21
- }
22
- options?: RemoteLoggerOptions
23
- }
24
-
25
- export const lokiTransport: transportFunctionType = (props: LokiTransportProps) => {
26
- // Loki requires a timestamp with nanosecond precision
27
- // however Date.now() only returns milliseconds precision.
28
- const timestampEndPadding = '000000'
29
-
30
- if (!props.options) {
31
- throw Error('props.options is required')
32
- }
33
-
34
- if (!props.options.lokiUrl) {
35
- throw Error('props.options.lokiUrl is required')
36
- }
37
-
38
- if (!props.options.lokiLabels) {
39
- throw Error('props.options.labels is required')
40
- }
41
-
42
- const { lokiUrl, lokiLabels } = props.options
43
- const { message, data } = props.rawMsg.pop()
44
- const payload = {
45
- streams: [
46
- {
47
- stream: {
48
- job: props.options.job ?? 'react-native-logs',
49
- level: props.level.text,
50
- ...lokiLabels,
51
- },
52
- values: [[`${Date.now()}${timestampEndPadding}`, JSON.stringify({ message, data })]],
53
- },
54
- ],
55
- }
56
-
57
- const [credentials, href] = lokiUrl.split('@')
58
- const [username, password] = credentials.split('//')[1].split(':')
59
- const protocol = credentials.split('//')[0]
60
- const authHeader = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`
61
- const config = {
62
- headers: {
63
- 'Content-Type': 'application/json',
64
- Authorization: authHeader,
65
- },
66
- }
67
-
68
- axios
69
- .post(`${protocol}//${href}`, payload, config)
70
- .then((res) => {
71
- if (res.status !== 204) {
72
- console.warn(`Expected Loki to return 204, received ${res.status}`)
73
- }
74
- })
75
- .catch((error) => {
76
- console.error(`Error while sending to Loki, ${error}`)
77
- })
78
- }