@bifold/remote-logs 1.0.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/LICENSE +202 -0
- package/README.md +137 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +17 -0
- package/build/logger.d.ts +48 -0
- package/build/logger.js +143 -0
- package/build/transports.d.ts +17 -0
- package/build/transports.js +55 -0
- package/package.json +59 -0
- package/src/index.ts +2 -0
- package/src/logger.ts +174 -0
- package/src/transports.ts +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2021 Indicio, PBC.
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
|
|
195
|
+
You may obtain a copy of the License at
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# TL;DR
|
|
2
|
+
|
|
3
|
+
The `@hyperledger/aries-remote-logs` package provides a simple way to send [credo-ts](https://github.com/openwallet-foundation/credo-ts) (Credo) logs to a remote [Loki](https://grafana.com/docs/loki/latest/) server.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
This package allows a copy of the Credo logs to be sent to a remote Loki server. This is useful for debugging issues in environments where the logs are not easily accessible, yet the logs are needed to provide [Tier 3](https://en.wikipedia.org/wiki/Technical_support) support.
|
|
8
|
+
|
|
9
|
+
Due to the sensitive nature of Verifiable Credentials and Digital Identity in general, the logs are not sent automatically. Instead, this feature must be enabled. It is **highly recommended** that this feature be disabled by default in production environments and only enabled, by the end user, when needed, and even then automatically disabled after a short period of time.
|
|
10
|
+
|
|
11
|
+
### Considerations
|
|
12
|
+
|
|
13
|
+
When remote logging is enabled the `RemoteLogger` will generate a session ID automatically. This ID is then set as a label on all logs sent to the remote server. This allows the logs to be filtered by session ID. This is useful when debugging issues that are specific to a single user.
|
|
14
|
+
|
|
15
|
+
When the end user enables remote logging present them with the a session ID. This ID should be recorded and provided to the support team. The support team can then use this ID to identify the correct logs. This session ID is also helpful to minimize corelation of the logs with the end user.
|
|
16
|
+
|
|
17
|
+

|
|
18
|
+
|
|
19
|
+
Fetch the session ID with the following code:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
// ...
|
|
23
|
+
const { agent } = useAgent()
|
|
24
|
+
const logger = agent?.config.logger as RemoteLogger
|
|
25
|
+
|
|
26
|
+
console.log(`Session ID: ${logger.sessionId}`)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Configuration
|
|
30
|
+
|
|
31
|
+
The `RemoteLogger` is configured with a set of options. These options are passed to the constructor of the `RemoteLogger` class. The options are as follows:
|
|
32
|
+
|
|
33
|
+
| Option | Type | Description |
|
|
34
|
+
| ------------------------------------------- | ------------------------ | ---------------------------------------------------------------------------- |
|
|
35
|
+
| `lokiUrl` | `string` | The full URL of the Loki server. |
|
|
36
|
+
| `lokiLabels` | `Record<string, string>` | A set of labels to be applied to all logs sent to the Loki server. |
|
|
37
|
+
| `autoDisableRemoteLoggingIntervalInMinutes` | `number` | The number of minutes to wait before automatically disabling remote logging. |
|
|
38
|
+
|
|
39
|
+
The image below shows what a typical Grafana log query looks like. The query is filtered by the `application` and `job` labels. The `sessionId` label is used to filter the logs for a specific debug session.
|
|
40
|
+
|
|
41
|
+

|
|
42
|
+
|
|
43
|
+
### Example Usage
|
|
44
|
+
|
|
45
|
+
Setup the `RemoteLogger` and pass it to the Credo `Agent` as shown below:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { RemoteLogger, RemoteLoggerOptions } from '@hyperledger/aries-remote-logs'
|
|
49
|
+
|
|
50
|
+
// ...
|
|
51
|
+
|
|
52
|
+
const autoDisableRemoteLoggingIntervalInMinutes = 90 // important
|
|
53
|
+
const lokiUrl = 'https://logs.example.com:3100/loki/api/v1/push'
|
|
54
|
+
const logOptions: RemoteLoggerOptions = {
|
|
55
|
+
lokiUrl,
|
|
56
|
+
lokiLabels: {
|
|
57
|
+
application: 'bifold-wallet',
|
|
58
|
+
job: 'react-native-logs',
|
|
59
|
+
},
|
|
60
|
+
autoDisableRemoteLoggingIntervalInMinutes,
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const logger = new RemoteLogger(logOptions)
|
|
64
|
+
logger.startEventListeners()
|
|
65
|
+
|
|
66
|
+
const credoConfigOpts = {
|
|
67
|
+
config: {
|
|
68
|
+
label: 'Bifold Wallet',
|
|
69
|
+
walletConfig: {
|
|
70
|
+
id: 'some-wallet-id',
|
|
71
|
+
key: 'some-secret-key',
|
|
72
|
+
},
|
|
73
|
+
logger,
|
|
74
|
+
mediatorPickupStrategy: MediatorPickupStrategy.Implicit,
|
|
75
|
+
autoUpdateStorageOnStartup: true,
|
|
76
|
+
autoAcceptConnections: true,
|
|
77
|
+
},
|
|
78
|
+
dependencies: agentDependencies,
|
|
79
|
+
modules: getAgentModules({
|
|
80
|
+
indyNetworks: ledgers,
|
|
81
|
+
mediatorInvitationUrl: Config.MEDIATOR_URL,
|
|
82
|
+
}),
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const agent = new Agent(credoConfigOpts)
|
|
86
|
+
|
|
87
|
+
// ...
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Enable and disable remote logging by sending events as shown below:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { DeviceEventEmitter } from 'react-native'
|
|
94
|
+
import { RemoteLoggerEventTypes } from '@hyperledger/aries-remote-logs'
|
|
95
|
+
|
|
96
|
+
// ...
|
|
97
|
+
|
|
98
|
+
// enable remote logging, this will send the logs to the remote server
|
|
99
|
+
// and will automatically disable remote logging after the specified
|
|
100
|
+
// interval.
|
|
101
|
+
DeviceEventEmitter.emit(RemoteLoggerEventTypes.ENABLE_REMOTE_LOGGING, true)
|
|
102
|
+
|
|
103
|
+
// ...
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Including the Package
|
|
107
|
+
|
|
108
|
+
**Metro**
|
|
109
|
+
|
|
110
|
+
Metro can't handle symlinks. Therefore, you need to specify the `@hyperledger/aries-remote-logs` location in the `metro.config.js` file of your application. Check out the example below, or view this [complete example](https://github.com/bcgov/bc-wallet-mobile/blob/main/app/metro.config.js) for more information.
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
// ...
|
|
114
|
+
fs.realpathSync(path.join(__dirname, 'node_modules', '@hyperledger/aries-remote-logs')),
|
|
115
|
+
// ...
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Yarn**
|
|
119
|
+
|
|
120
|
+
Yarn struggles to include `react-native` peer-dependencies from the local file system. To fix this, add any `react-native` dependencies from this package's `peerDependencies` in `package.json` to your own `package.json`. Use a command like `yarn add react-native-logs@^5.0.1` to do this.
|
|
121
|
+
|
|
122
|
+
## Development
|
|
123
|
+
|
|
124
|
+
This package ships with a docker-compose file that can be used to run a local Grafana Loki stack. To start the containers run the following command:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
docker compose up -d
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The Loki server will be available at `http://localhost:3100`. The Grafana server will be available at `http://localhost:3000`. The default username and password for Grafana is `admin` and the password is specified, can can be changed, in the `docker-compose.yaml` file:
|
|
131
|
+
|
|
132
|
+
```yaml
|
|
133
|
+
environment:
|
|
134
|
+
GF_SECURITY_ADMIN_PASSWORD: secret
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Once the stack is running, and you have logged into Grafana, you may need to add a Loki data source from the main landing page. The only value you need set is the URL where Loki is running. The value, as per the docker stack is: `http://loki:3100`
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./logger"), exports);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { BifoldError, BifoldLogger } from '@bifold/core';
|
|
2
|
+
import { RemoteLoggerOptions } from './transports';
|
|
3
|
+
export declare enum RemoteLoggerEventTypes {
|
|
4
|
+
ENABLE_REMOTE_LOGGING = "RemoteLogging.Enable"
|
|
5
|
+
}
|
|
6
|
+
export declare class RemoteLogger extends BifoldLogger {
|
|
7
|
+
private _remoteLoggingEnabled;
|
|
8
|
+
private _sessionId;
|
|
9
|
+
private _autoDisableRemoteLoggingIntervalInMinutes;
|
|
10
|
+
private lokiUrl;
|
|
11
|
+
private lokiLabels;
|
|
12
|
+
private remoteLoggingAutoDisableTimer;
|
|
13
|
+
private eventListener;
|
|
14
|
+
protected _log: any;
|
|
15
|
+
protected _config: {
|
|
16
|
+
levels: {
|
|
17
|
+
test: number;
|
|
18
|
+
trace: number;
|
|
19
|
+
debug: number;
|
|
20
|
+
info: number;
|
|
21
|
+
warn: number;
|
|
22
|
+
error: number;
|
|
23
|
+
fatal: number;
|
|
24
|
+
};
|
|
25
|
+
severity: string;
|
|
26
|
+
async: boolean;
|
|
27
|
+
dateFormat: string;
|
|
28
|
+
printDate: boolean;
|
|
29
|
+
};
|
|
30
|
+
constructor(options: RemoteLoggerOptions);
|
|
31
|
+
get sessionId(): number;
|
|
32
|
+
set sessionId(value: number);
|
|
33
|
+
get autoDisableRemoteLoggingIntervalInMinutes(): number;
|
|
34
|
+
get remoteLoggingEnabled(): boolean;
|
|
35
|
+
set remoteLoggingEnabled(value: boolean);
|
|
36
|
+
private configureLogger;
|
|
37
|
+
startEventListeners(): void;
|
|
38
|
+
stopEventListeners(): void;
|
|
39
|
+
overrideCurrentAutoDisableExpiration(expirationInMinutes: number): void;
|
|
40
|
+
test(message: string, data?: object | undefined): void;
|
|
41
|
+
trace(message: string, data?: object | undefined): void;
|
|
42
|
+
debug(message: string, data?: object | undefined): void;
|
|
43
|
+
info(message: string, data?: object | undefined): void;
|
|
44
|
+
warn(message: string, data?: object | undefined): void;
|
|
45
|
+
error(message: string, data?: object | undefined): void;
|
|
46
|
+
fatal(message: string, data?: object | undefined): void;
|
|
47
|
+
report(bifoldError: BifoldError): void;
|
|
48
|
+
}
|
package/build/logger.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RemoteLogger = exports.RemoteLoggerEventTypes = void 0;
|
|
4
|
+
const core_1 = require("@bifold/core");
|
|
5
|
+
const react_native_1 = require("react-native");
|
|
6
|
+
const react_native_logs_1 = require("react-native-logs");
|
|
7
|
+
const transports_1 = require("./transports");
|
|
8
|
+
var RemoteLoggerEventTypes;
|
|
9
|
+
(function (RemoteLoggerEventTypes) {
|
|
10
|
+
RemoteLoggerEventTypes["ENABLE_REMOTE_LOGGING"] = "RemoteLogging.Enable";
|
|
11
|
+
})(RemoteLoggerEventTypes || (exports.RemoteLoggerEventTypes = RemoteLoggerEventTypes = {}));
|
|
12
|
+
class RemoteLogger extends core_1.BifoldLogger {
|
|
13
|
+
constructor(options) {
|
|
14
|
+
var _a, _b, _c;
|
|
15
|
+
super();
|
|
16
|
+
this._remoteLoggingEnabled = false;
|
|
17
|
+
this._autoDisableRemoteLoggingIntervalInMinutes = 0;
|
|
18
|
+
this._config = {
|
|
19
|
+
levels: {
|
|
20
|
+
test: 0,
|
|
21
|
+
trace: 0,
|
|
22
|
+
debug: 0,
|
|
23
|
+
info: 1,
|
|
24
|
+
warn: 2,
|
|
25
|
+
error: 3,
|
|
26
|
+
fatal: 4,
|
|
27
|
+
},
|
|
28
|
+
severity: 'debug',
|
|
29
|
+
async: true,
|
|
30
|
+
dateFormat: 'time',
|
|
31
|
+
printDate: false,
|
|
32
|
+
};
|
|
33
|
+
this.lokiUrl = (_a = options.lokiUrl) !== null && _a !== void 0 ? _a : undefined;
|
|
34
|
+
this.lokiLabels = (_b = options.lokiLabels) !== null && _b !== void 0 ? _b : {};
|
|
35
|
+
this._autoDisableRemoteLoggingIntervalInMinutes = (_c = options.autoDisableRemoteLoggingIntervalInMinutes) !== null && _c !== void 0 ? _c : 0;
|
|
36
|
+
this.configureLogger();
|
|
37
|
+
}
|
|
38
|
+
get sessionId() {
|
|
39
|
+
if (!this._sessionId) {
|
|
40
|
+
this._sessionId = Math.floor(100000 + Math.random() * 900000);
|
|
41
|
+
}
|
|
42
|
+
return this._sessionId;
|
|
43
|
+
}
|
|
44
|
+
set sessionId(value) {
|
|
45
|
+
this._sessionId = value;
|
|
46
|
+
this.configureLogger();
|
|
47
|
+
}
|
|
48
|
+
get autoDisableRemoteLoggingIntervalInMinutes() {
|
|
49
|
+
return this._autoDisableRemoteLoggingIntervalInMinutes;
|
|
50
|
+
}
|
|
51
|
+
get remoteLoggingEnabled() {
|
|
52
|
+
return this._remoteLoggingEnabled;
|
|
53
|
+
}
|
|
54
|
+
set remoteLoggingEnabled(value) {
|
|
55
|
+
this._remoteLoggingEnabled = value;
|
|
56
|
+
if (value === false) {
|
|
57
|
+
this._sessionId = undefined;
|
|
58
|
+
}
|
|
59
|
+
this.configureLogger();
|
|
60
|
+
}
|
|
61
|
+
configureLogger() {
|
|
62
|
+
const transportOptions = {};
|
|
63
|
+
const transport = [react_native_logs_1.consoleTransport];
|
|
64
|
+
const config = Object.assign(Object.assign({}, this._config), { transport,
|
|
65
|
+
transportOptions });
|
|
66
|
+
if (this.remoteLoggingEnabled && this.lokiUrl) {
|
|
67
|
+
transport.push(transports_1.lokiTransport);
|
|
68
|
+
config['transportOptions'] = {
|
|
69
|
+
lokiUrl: this.lokiUrl,
|
|
70
|
+
lokiLabels: Object.assign(Object.assign({}, this.lokiLabels), { session_id: `${this.sessionId}` }),
|
|
71
|
+
};
|
|
72
|
+
if (this.autoDisableRemoteLoggingIntervalInMinutes && this.autoDisableRemoteLoggingIntervalInMinutes > 0) {
|
|
73
|
+
this.remoteLoggingAutoDisableTimer = setTimeout(() => {
|
|
74
|
+
this.remoteLoggingEnabled = false;
|
|
75
|
+
}, this.autoDisableRemoteLoggingIntervalInMinutes * 60000);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
this._log = react_native_logs_1.logger.createLogger(config);
|
|
79
|
+
}
|
|
80
|
+
startEventListeners() {
|
|
81
|
+
this.eventListener = react_native_1.DeviceEventEmitter.addListener(RemoteLoggerEventTypes.ENABLE_REMOTE_LOGGING, (value) => {
|
|
82
|
+
this.remoteLoggingEnabled = value;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
stopEventListeners() {
|
|
86
|
+
this.eventListener = undefined;
|
|
87
|
+
}
|
|
88
|
+
overrideCurrentAutoDisableExpiration(expirationInMinutes) {
|
|
89
|
+
if (expirationInMinutes <= 0) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (this.remoteLoggingAutoDisableTimer) {
|
|
93
|
+
clearTimeout(this.remoteLoggingAutoDisableTimer);
|
|
94
|
+
}
|
|
95
|
+
this.remoteLoggingAutoDisableTimer = setTimeout(() => {
|
|
96
|
+
this.remoteLoggingEnabled = false;
|
|
97
|
+
}, expirationInMinutes * 60000);
|
|
98
|
+
}
|
|
99
|
+
test(message, data) {
|
|
100
|
+
var _a;
|
|
101
|
+
(_a = this._log) === null || _a === void 0 ? void 0 : _a.test({ message, data });
|
|
102
|
+
}
|
|
103
|
+
trace(message, data) {
|
|
104
|
+
var _a;
|
|
105
|
+
(_a = this._log) === null || _a === void 0 ? void 0 : _a.trace({ message, data });
|
|
106
|
+
}
|
|
107
|
+
debug(message, data) {
|
|
108
|
+
var _a;
|
|
109
|
+
(_a = this._log) === null || _a === void 0 ? void 0 : _a.debug({ message, data });
|
|
110
|
+
}
|
|
111
|
+
info(message, data) {
|
|
112
|
+
var _a;
|
|
113
|
+
(_a = this._log) === null || _a === void 0 ? void 0 : _a.info({ message, data });
|
|
114
|
+
}
|
|
115
|
+
warn(message, data) {
|
|
116
|
+
var _a;
|
|
117
|
+
(_a = this._log) === null || _a === void 0 ? void 0 : _a.warn({ message, data });
|
|
118
|
+
}
|
|
119
|
+
error(message, data) {
|
|
120
|
+
var _a;
|
|
121
|
+
(_a = this._log) === null || _a === void 0 ? void 0 : _a.error({ message, data });
|
|
122
|
+
}
|
|
123
|
+
fatal(message, data) {
|
|
124
|
+
var _a;
|
|
125
|
+
(_a = this._log) === null || _a === void 0 ? void 0 : _a.fatal({ message, data });
|
|
126
|
+
}
|
|
127
|
+
report(bifoldError) {
|
|
128
|
+
var _a;
|
|
129
|
+
(_a = this._log) === null || _a === void 0 ? void 0 : _a.info({ message: 'Sending Loki report' });
|
|
130
|
+
const { title, description, code, message } = bifoldError;
|
|
131
|
+
(0, transports_1.lokiTransport)({
|
|
132
|
+
msg: title,
|
|
133
|
+
rawMsg: [{ message: title, data: { title, description, code, message } }],
|
|
134
|
+
level: { severity: 3, text: 'error' },
|
|
135
|
+
options: {
|
|
136
|
+
lokiUrl: this.lokiUrl,
|
|
137
|
+
lokiLabels: this.lokiLabels,
|
|
138
|
+
job: 'incident-report',
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.RemoteLogger = RemoteLogger;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { transportFunctionType } from 'react-native-logs';
|
|
2
|
+
export interface RemoteLoggerOptions {
|
|
3
|
+
lokiUrl?: string;
|
|
4
|
+
lokiLabels?: Record<string, string>;
|
|
5
|
+
autoDisableRemoteLoggingIntervalInMinutes?: number;
|
|
6
|
+
job?: string;
|
|
7
|
+
}
|
|
8
|
+
export type LokiTransportProps = {
|
|
9
|
+
msg: any;
|
|
10
|
+
rawMsg: any;
|
|
11
|
+
level: {
|
|
12
|
+
severity: number;
|
|
13
|
+
text: string;
|
|
14
|
+
};
|
|
15
|
+
options?: RemoteLoggerOptions;
|
|
16
|
+
};
|
|
17
|
+
export declare const lokiTransport: transportFunctionType;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.lokiTransport = void 0;
|
|
7
|
+
/* eslint-disable no-console */
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
|
+
const buffer_1 = require("buffer");
|
|
10
|
+
const lokiTransport = (props) => {
|
|
11
|
+
var _a;
|
|
12
|
+
// Loki requires a timestamp with nanosecond precision
|
|
13
|
+
// however Date.now() only returns milliseconds precision.
|
|
14
|
+
const timestampEndPadding = '000000';
|
|
15
|
+
if (!props.options) {
|
|
16
|
+
throw Error('props.options is required');
|
|
17
|
+
}
|
|
18
|
+
if (!props.options.lokiUrl) {
|
|
19
|
+
throw Error('props.options.lokiUrl is required');
|
|
20
|
+
}
|
|
21
|
+
if (!props.options.lokiLabels) {
|
|
22
|
+
throw Error('props.options.labels is required');
|
|
23
|
+
}
|
|
24
|
+
const { lokiUrl, lokiLabels } = props.options;
|
|
25
|
+
const { message, data } = props.rawMsg.pop();
|
|
26
|
+
const payload = {
|
|
27
|
+
streams: [
|
|
28
|
+
{
|
|
29
|
+
stream: Object.assign({ job: (_a = props.options.job) !== null && _a !== void 0 ? _a : 'react-native-logs', level: props.level.text }, lokiLabels),
|
|
30
|
+
values: [[`${Date.now()}${timestampEndPadding}`, JSON.stringify({ message, data })]],
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
};
|
|
34
|
+
const [credentials, href] = lokiUrl.split('@');
|
|
35
|
+
const [username, password] = credentials.split('//')[1].split(':');
|
|
36
|
+
const protocol = credentials.split('//')[0];
|
|
37
|
+
const authHeader = `Basic ${buffer_1.Buffer.from(`${username}:${password}`).toString('base64')}`;
|
|
38
|
+
const config = {
|
|
39
|
+
headers: {
|
|
40
|
+
'Content-Type': 'application/json',
|
|
41
|
+
Authorization: authHeader,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
axios_1.default
|
|
45
|
+
.post(`${protocol}//${href}`, payload, config)
|
|
46
|
+
.then((res) => {
|
|
47
|
+
if (res.status !== 204) {
|
|
48
|
+
console.warn(`Expected Loki to return 204, received ${res.status}`);
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
.catch((error) => {
|
|
52
|
+
console.error(`Error while sending to Loki, ${error}`);
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
exports.lokiTransport = lokiTransport;
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bifold/remote-logs",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Remote logging for credo-ts agents",
|
|
5
|
+
"main": "build/index.js",
|
|
6
|
+
"types": "build/index.d.ts",
|
|
7
|
+
"source": "src/index.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"build"
|
|
11
|
+
],
|
|
12
|
+
"license": "Apache-2.0",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "echo 'building remote-logs 🏗️' && yarn run clean && yarn run compile && echo 'remote-logs built ✅'",
|
|
15
|
+
"clean": "rimraf ./build",
|
|
16
|
+
"compile": "tsc"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/openwallet-foundation/bifold-wallet.git",
|
|
21
|
+
"directory": "packages/remote-logging"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"registry": "https://registry.npmjs.org/",
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/openwallet-foundation/bifold-wallet/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/openwallet-foundation/bifold-wallet",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@babel/runtime": "^7.20.0",
|
|
33
|
+
"@typescript-eslint/parser": "^6.6.0",
|
|
34
|
+
"eslint": "^8.48.0",
|
|
35
|
+
"eslint-import-resolver-typescript": "^2.5.0",
|
|
36
|
+
"rimraf": "^5.0.0",
|
|
37
|
+
"typescript": "^5.0.4"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@credo-ts/core": "0.5.13",
|
|
41
|
+
"axios": "^1.4.0",
|
|
42
|
+
"buffer": "^6.0.3",
|
|
43
|
+
"react": "^18.2.0",
|
|
44
|
+
"react-native": "^0.73.6",
|
|
45
|
+
"react-native-logs": "^5.1.0"
|
|
46
|
+
},
|
|
47
|
+
"installConfig": {
|
|
48
|
+
"hoistingLimits": "workspaces"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@bifold/core": "1.0.0",
|
|
52
|
+
"@credo-ts/core": "0.5.13",
|
|
53
|
+
"axios": "^1.4.0",
|
|
54
|
+
"buffer": "^6.0.3",
|
|
55
|
+
"react": "18.2.0",
|
|
56
|
+
"react-native": "^0.73.6",
|
|
57
|
+
"react-native-logs": "^5.1.0"
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/index.ts
ADDED
package/src/logger.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
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(message: string, data?: object | undefined): void {
|
|
133
|
+
this._log?.test({ message, data })
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public trace(message: string, data?: object | undefined): void {
|
|
137
|
+
this._log?.trace({ message, data })
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public debug(message: string, data?: object | undefined): void {
|
|
141
|
+
this._log?.debug({ message, data })
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public info(message: string, data?: object | undefined): void {
|
|
145
|
+
this._log?.info({ message, data })
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public warn(message: string, data?: object | undefined): void {
|
|
149
|
+
this._log?.warn({ message, data })
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public error(message: string, data?: object | undefined): void {
|
|
153
|
+
this._log?.error({ message, data })
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public fatal(message: string, data?: object | undefined): void {
|
|
157
|
+
this._log?.fatal({ message, data })
|
|
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
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
}
|