@crimson-education/browser-logger 2.0.2-cognito.2 → 3.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/README.md +289 -18
- package/lib/index.d.ts +10 -24
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +45 -116
- package/lib/index.js.map +1 -1
- package/lib/logger/consoleTransport.d.ts +37 -0
- package/lib/logger/consoleTransport.d.ts.map +1 -0
- package/lib/logger/consoleTransport.js +81 -0
- package/lib/logger/consoleTransport.js.map +1 -0
- package/lib/logger/datadogTransport.d.ts +8 -0
- package/lib/logger/datadogTransport.d.ts.map +1 -0
- package/lib/logger/datadogTransport.js +21 -0
- package/lib/logger/datadogTransport.js.map +1 -0
- package/lib/logger/index.d.ts +16 -0
- package/lib/logger/index.d.ts.map +1 -0
- package/lib/logger/index.js +148 -0
- package/lib/logger/index.js.map +1 -0
- package/lib/logger/index.test.d.ts +2 -0
- package/lib/logger/index.test.d.ts.map +1 -0
- package/lib/logger/index.test.js +60 -0
- package/lib/logger/index.test.js.map +1 -0
- package/lib/logger/utils.d.ts +15 -0
- package/lib/logger/utils.d.ts.map +1 -0
- package/lib/logger/utils.js +32 -0
- package/lib/logger/utils.js.map +1 -0
- package/lib/reporters/amplifyReporter.d.ts +40 -14
- package/lib/reporters/amplifyReporter.d.ts.map +1 -1
- package/lib/reporters/amplifyReporter.js +15 -23
- package/lib/reporters/amplifyReporter.js.map +1 -1
- package/lib/reporters/amplifyReporter.test.js +0 -11
- package/lib/reporters/amplifyReporter.test.js.map +1 -1
- package/lib/reporters/datadogReporter.d.ts +64 -14
- package/lib/reporters/datadogReporter.d.ts.map +1 -1
- package/lib/reporters/datadogReporter.js +46 -101
- package/lib/reporters/datadogReporter.js.map +1 -1
- package/lib/reporters/gtmReporter.d.ts +3 -2
- package/lib/reporters/gtmReporter.d.ts.map +1 -1
- package/lib/reporters/gtmReporter.js +20 -6
- package/lib/reporters/gtmReporter.js.map +1 -1
- package/lib/reporters/index.d.ts +66 -28
- package/lib/reporters/index.d.ts.map +1 -1
- package/lib/reporters/index.js +210 -0
- package/lib/reporters/index.js.map +1 -1
- package/lib/reporters/logReporter.d.ts +35 -0
- package/lib/reporters/logReporter.d.ts.map +1 -0
- package/lib/reporters/logReporter.js +62 -0
- package/lib/reporters/logReporter.js.map +1 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +19 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/logger.d.ts +78 -0
- package/lib/types/logger.d.ts.map +1 -0
- package/lib/{types.js → types/logger.js} +1 -1
- package/lib/types/logger.js.map +1 -0
- package/lib/types/reporter.d.ts +155 -0
- package/lib/types/reporter.d.ts.map +1 -0
- package/lib/types/reporter.js +3 -0
- package/lib/types/reporter.js.map +1 -0
- package/lib/utils.d.ts +9 -4
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +28 -43
- package/lib/utils.js.map +1 -1
- package/lib/utils.test.d.ts +2 -0
- package/lib/utils.test.d.ts.map +1 -0
- package/lib/utils.test.js +32 -0
- package/lib/utils.test.js.map +1 -0
- package/package.json +5 -4
- package/src/index.ts +41 -120
- package/src/logger/consoleTransport.ts +101 -0
- package/src/logger/datadogTransport.ts +20 -0
- package/src/logger/index.test.ts +68 -0
- package/src/logger/index.ts +139 -0
- package/src/logger/utils.ts +28 -0
- package/src/reporters/amplifyReporter.test.ts +1 -14
- package/src/reporters/amplifyReporter.ts +65 -36
- package/src/reporters/datadogReporter.ts +123 -115
- package/src/reporters/gtmReporter.ts +35 -8
- package/src/reporters/index.ts +208 -24
- package/src/reporters/logReporter.ts +86 -0
- package/src/types/index.ts +2 -0
- package/src/types/logger.ts +85 -0
- package/src/types/reporter.ts +167 -0
- package/src/utils.test.ts +32 -0
- package/src/utils.ts +39 -49
- package/lib/types.d.ts +0 -48
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js.map +0 -1
- package/src/types.ts +0 -50
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Crimson Education Browser Logger
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A structured logger and analytics reporting utility for browser environments.
|
|
4
4
|
|
|
5
5
|
This library contains a number of reporters that is will send analytics to.
|
|
6
6
|
|
|
@@ -24,6 +24,7 @@ yarn add @crimson-education/browser-logger
|
|
|
24
24
|
|
|
25
25
|
```ts
|
|
26
26
|
import * as Logger from '@crimson-education/browser-logger';
|
|
27
|
+
import { config } from '../configuration';
|
|
27
28
|
|
|
28
29
|
Logger.init({
|
|
29
30
|
service: 'test-project',
|
|
@@ -33,22 +34,39 @@ Logger.init({
|
|
|
33
34
|
application: 'test',
|
|
34
35
|
},
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
37
|
+
reporters: {
|
|
38
|
+
log: true,
|
|
39
|
+
|
|
40
|
+
gtm: true,
|
|
41
|
+
|
|
42
|
+
datadog: {
|
|
43
|
+
applicationId: config.datadogApplicationId,
|
|
44
|
+
clientToken: config.datadogClientToken,
|
|
45
|
+
site: config.datadogSite,
|
|
46
|
+
proxyUrl: config.datadogTunnelProxyUrl,
|
|
47
|
+
version: config.datadogVersion,
|
|
48
|
+
|
|
49
|
+
sampleRate: config.environment === 'production' ? 50 : 0,
|
|
50
|
+
replaySampleRate: config.environment === 'production' ? 50 : 0,
|
|
45
51
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
forwardConsoleLogs: true,
|
|
53
|
+
logTransport: {
|
|
54
|
+
level: Logger.LogLevel.Info,
|
|
55
|
+
},
|
|
56
|
+
trackInteractions: true,
|
|
57
|
+
trackFrustrations: true,
|
|
58
|
+
allowedTracingOrigins: ['https://my.api.domain']
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
amplify: {
|
|
62
|
+
region: config.awsRegion,
|
|
63
|
+
identityPoolId: config.pinpointIdentityPoolId,
|
|
64
|
+
analyticsAppId: config.pinpointAnalyticsAppId,
|
|
65
|
+
|
|
66
|
+
autoTrackEvents: true,
|
|
67
|
+
autoTrackPageViews: true,
|
|
68
|
+
autoTrackSessions: true,
|
|
69
|
+
}
|
|
52
70
|
}
|
|
53
71
|
});
|
|
54
72
|
|
|
@@ -68,8 +86,7 @@ Logger.trackEvent({
|
|
|
68
86
|
},
|
|
69
87
|
});
|
|
70
88
|
|
|
71
|
-
|
|
72
|
-
const logger = Logger.createLogger('app');
|
|
89
|
+
export const logger = Logger.createLogger();
|
|
73
90
|
|
|
74
91
|
// Attributed logging.
|
|
75
92
|
logger.info('WOW!', {
|
|
@@ -77,6 +94,260 @@ logger.info('WOW!', {
|
|
|
77
94
|
});
|
|
78
95
|
```
|
|
79
96
|
|
|
97
|
+
### Structured Logging
|
|
98
|
+
|
|
99
|
+
Structured logging in this library is loosely based on Winston, and follows roughly the same interface as `@crimson-education/node-logger`.
|
|
100
|
+
|
|
101
|
+
Call `Logger.createLogger()` to create a logger instance, it is recommended to re-use this as a global logger everywhere, and then call `.child()` to reuse metadata.
|
|
102
|
+
|
|
103
|
+
Global metadata about the service will be added for transport usage after calling `Logger.init()`.
|
|
104
|
+
|
|
105
|
+
You can add more Global Metadata at any time by calling `addMetadata()`.
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
export const logger = Logger.createLogger();
|
|
110
|
+
|
|
111
|
+
const log = logger.child({
|
|
112
|
+
from: 'childLog',
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
log.info('My Log', { jobCount: jobs.length });
|
|
116
|
+
|
|
117
|
+
const timer = log.startTimer();
|
|
118
|
+
|
|
119
|
+
const result = await job(jobs);
|
|
120
|
+
|
|
121
|
+
timer.done({ message: 'Job Completed', metadata: { id: result.id } });
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Reporting
|
|
125
|
+
|
|
126
|
+
#### Opting out of events
|
|
127
|
+
|
|
128
|
+
You can opt out of sending events to reporters on a global, or per call basis using the endpoints config.
|
|
129
|
+
|
|
130
|
+
E.g. On calls
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
Logger.trackEvent({
|
|
134
|
+
message: 'my-event',
|
|
135
|
+
toReporters: ['log', 'datadog'],
|
|
136
|
+
excludeReporters: ['datadog']
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
E.g. Globally
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
Logger.init({
|
|
144
|
+
reporters: {
|
|
145
|
+
log: true,
|
|
146
|
+
datadog: {
|
|
147
|
+
...
|
|
148
|
+
endpoints: {
|
|
149
|
+
trackEvent: false,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
These will only send to the log reporter, it is recommended to use `toReporters` or `excludeReporters` separately as they overlap.
|
|
157
|
+
|
|
158
|
+
#### Filtering out metadata
|
|
159
|
+
|
|
160
|
+
You can filter what metadata gets sent to each reporter in the reporter config.
|
|
161
|
+
This accepts a string for exact match metadata keys, or a RegExp.
|
|
162
|
+
You can use `.` to traverse metadata.
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
Logger.init({
|
|
166
|
+
reporters: {
|
|
167
|
+
log: {
|
|
168
|
+
ignoreMetadataPatterns: [
|
|
169
|
+
'internalError',
|
|
170
|
+
'user.email',
|
|
171
|
+
/error\.stack.*/g
|
|
172
|
+
]
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Filtering out Breadcrumb categories
|
|
179
|
+
|
|
180
|
+
You can filter what breadcrumbs get sent to reports in the reporter config.
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
Logger.init({
|
|
184
|
+
reporters: {
|
|
185
|
+
log: {
|
|
186
|
+
ignoreBreadcrumbCategories: [
|
|
187
|
+
'fetch'
|
|
188
|
+
]
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// Not sent to the log reporter.
|
|
194
|
+
Logger.addBreadcrumb({
|
|
195
|
+
...
|
|
196
|
+
category: 'fetch',
|
|
197
|
+
});
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Configure Reporters
|
|
201
|
+
|
|
202
|
+
#### Log
|
|
203
|
+
|
|
204
|
+
By default, the log reporter is enabled. This adds all reporter functions to logs. Set `reporters.log` to `false` to disable reporter logging.
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import { config } from '../configuration';
|
|
208
|
+
import * as Logger from '@crimson-education/browser-logger';
|
|
209
|
+
|
|
210
|
+
Logger.init({
|
|
211
|
+
reporters: {
|
|
212
|
+
log: {
|
|
213
|
+
trackEventLevel: Logger.LogInfo.Debug,
|
|
214
|
+
endpoints: {
|
|
215
|
+
recordSession: false,
|
|
216
|
+
recordSessionStop: false,
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Check out the [`DatadogReporterConfig`](https://github.com/crimson-education/browser-logger/blob/main/src/reporters/datadogReporter.ts) for all of the configuration you can apply to Datadog reporting.
|
|
224
|
+
|
|
225
|
+
#### Datadog
|
|
226
|
+
|
|
227
|
+
To configure Datadog, this requires at least `applicationId`, `clientToken` and `site` (As we use an EU instance).
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
import { config } from '../configuration';
|
|
231
|
+
import * as Logger from '@crimson-education/browser-logger';
|
|
232
|
+
|
|
233
|
+
Logger.init({
|
|
234
|
+
reporters: {
|
|
235
|
+
datadog: {
|
|
236
|
+
applicationId: config.datadogApplicationId,
|
|
237
|
+
clientToken: config.datadogClientToken,
|
|
238
|
+
site: config.datadogSite,
|
|
239
|
+
proxyUrl: config.datadogTunnelProxyUrl,
|
|
240
|
+
version: config.datadogVersion,
|
|
241
|
+
|
|
242
|
+
sampleRate: config.environment === 'production' ? 50 : 0,
|
|
243
|
+
replaySampleRate: config.environment === 'production' ? 50 : 0,
|
|
244
|
+
|
|
245
|
+
forwardConsoleLogs: true,
|
|
246
|
+
logTransport: {
|
|
247
|
+
level: Logger.LogLevel.Info,
|
|
248
|
+
},
|
|
249
|
+
trackInteractions: true,
|
|
250
|
+
allowedTracingOrigins: ['https://my.api.domain']
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
Check out the [`DatadogReporterConfig`](https://github.com/crimson-education/browser-logger/blob/main/src/reporters/datadogReporter.ts) for all of the configuration you can apply to Datadog reporting.
|
|
257
|
+
|
|
258
|
+
This automatically adds a Datadog Log Transport that transmits Log data to Datadog, this can be customized in `DatadogReporterConfig` with `logTransport`, or disabled by setting `logTransport` to `false`.
|
|
259
|
+
|
|
260
|
+
#### Amplify/Pinpoint
|
|
261
|
+
|
|
262
|
+
To configure Amplify, this requires at least `region`, `identityPoolId` and `analyticsAppId`.
|
|
263
|
+
|
|
264
|
+
> **Note**: If you are setting up Browser Logger in a project that uses AWS Cognito or other AWS Amplify Features, you will want to set `identityPoolId` to false. This disables the reporter's call to Auth.configure, as you will have used that to configure your App already.
|
|
265
|
+
>
|
|
266
|
+
> You will need to set identityPoolId in your Auth.configure function yourself, otherwise Pinpoint won't work.
|
|
267
|
+
|
|
268
|
+
Auto `Auth.configure`:
|
|
269
|
+
```ts
|
|
270
|
+
import { config } from '../configuration';
|
|
271
|
+
import * as Logger from '@crimson-education/browser-logger';
|
|
272
|
+
|
|
273
|
+
Logger.init({
|
|
274
|
+
reporters: {
|
|
275
|
+
amplify: {
|
|
276
|
+
region: config.amplifyAwsRegion,
|
|
277
|
+
analyticsAppId: config.pinpointAnalyticsAppId,
|
|
278
|
+
identityPoolId: config.amplifyIdentityPoolId,
|
|
279
|
+
|
|
280
|
+
autoTrackEvents: true,
|
|
281
|
+
autoTrackPageViews: true,
|
|
282
|
+
autoTrackSessions: true,
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
Manual `Auth.configure`:
|
|
289
|
+
```ts
|
|
290
|
+
import { Auth } from '@aws-amplify/auth';
|
|
291
|
+
import { config } from '../configuration';
|
|
292
|
+
import * as Logger from '@crimson-education/browser-logger';
|
|
293
|
+
|
|
294
|
+
Auth.configure({
|
|
295
|
+
region: config.cognitoRegion,
|
|
296
|
+
userPoolId: config.cognitoUserPoolId,
|
|
297
|
+
userPoolWebClientId: config.cognitoClientId,
|
|
298
|
+
identityPoolId: config.amplifyIdentityPoolId,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
Logger.init({
|
|
302
|
+
reporters: {
|
|
303
|
+
amplify: {
|
|
304
|
+
region: config.amplifyAwsRegion,
|
|
305
|
+
analyticsAppId: config.pinpointAnalyticsAppId,
|
|
306
|
+
identityPoolId: false,
|
|
307
|
+
|
|
308
|
+
autoTrackEvents: true,
|
|
309
|
+
autoTrackPageViews: true,
|
|
310
|
+
autoTrackSessions: true,
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
Check out the [`AmplifyReporterConfig`](https://github.com/crimson-education/browser-logger/blob/main/src/reporters/amplifyReporter.ts) for all of the configuration you can apply to Amplify reporting.
|
|
317
|
+
|
|
318
|
+
#### Google Tag Manager
|
|
319
|
+
|
|
320
|
+
To configure GTM, you will need to have loaded the GTM script into your App.
|
|
321
|
+
See: https://support.google.com/tagmanager/answer/6103696?hl=en
|
|
322
|
+
|
|
323
|
+
```ts
|
|
324
|
+
import { config } from '../configuration';
|
|
325
|
+
import * as Logger from '@crimson-education/browser-logger';
|
|
326
|
+
|
|
327
|
+
// No configuration
|
|
328
|
+
Logger.init({
|
|
329
|
+
reporters: {
|
|
330
|
+
gtm: true,
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
// With configuration
|
|
335
|
+
Logger.init({
|
|
336
|
+
reporters: {
|
|
337
|
+
gtm: {
|
|
338
|
+
ignoreBreadcrumbCategories: ['fetch']
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
Check out the [`GTMReporterConfig`](https://github.com/crimson-education/browser-logger/blob/main/src/reporters/gtmReporter.ts) for all of the configuration you can apply to Google Tag Manager reporting.
|
|
345
|
+
|
|
346
|
+
### Functions
|
|
347
|
+
|
|
348
|
+
See the [src/logger/index.ts](./src/logger/index.ts) file for all exported functions of the Logger.
|
|
349
|
+
See the [src/reporter/index.ts](./src/reporter/index.ts) file for all exported functions of the Reporter.
|
|
350
|
+
|
|
80
351
|
### Improve Session Tracking with Component names
|
|
81
352
|
|
|
82
353
|
You can improve the name of components in analytics tools like Datadog and Amplify use, instead of the content of a component.
|
package/lib/index.d.ts
CHANGED
|
@@ -1,27 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ReporterBreadcrumb, ReporterEvent, TrackedReporterEvent } from './reporters';
|
|
3
|
-
import { DatadogReporterConfig } from './reporters/datadogReporter';
|
|
4
|
-
import { AmplifyReporterConfig } from './reporters/amplifyReporter';
|
|
1
|
+
import { ReporterConfigurations, ServiceInfo } from './types';
|
|
5
2
|
export * from './types';
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
gtm?: boolean;
|
|
3
|
+
export * from './reporters';
|
|
4
|
+
export * from './logger';
|
|
5
|
+
export declare type LoggerConfig = ServiceInfo & {
|
|
6
|
+
reporters?: ReporterConfigurations;
|
|
11
7
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export declare function
|
|
17
|
-
export declare function setRouteName(routeName: string): void;
|
|
18
|
-
export declare function setPageName(pageName: string): void;
|
|
19
|
-
export declare function reportError(error: ReportError, metadata?: Metadata): void;
|
|
20
|
-
export declare function recordSession(): void;
|
|
21
|
-
export declare function recordSessionStop(): void;
|
|
22
|
-
export declare function trackEventSinceLastAction(event: ReporterEvent): void;
|
|
23
|
-
export declare function getLastTrackedEvent(): TrackedReporterEvent | null;
|
|
24
|
-
export declare function createLogger(name?: string, options?: {
|
|
25
|
-
metadata?: Metadata;
|
|
26
|
-
}): ILogger;
|
|
8
|
+
/**
|
|
9
|
+
* Initializes the logger and reporters.
|
|
10
|
+
* @param config Reporter config options.
|
|
11
|
+
*/
|
|
12
|
+
export declare function init(config: LoggerConfig): void;
|
|
27
13
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAQ9D,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AAEzB,oBAAY,YAAY,GAAG,WAAW,GAAG;IAAE,SAAS,CAAC,EAAE,sBAAsB,CAAA;CAAE,CAAC;AAIhF;;;GAGG;AACH,wBAAgB,IAAI,CAAC,MAAM,EAAE,YAAY,QA2CxC"}
|
package/lib/index.js
CHANGED
|
@@ -14,128 +14,57 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
18
|
-
const
|
|
19
|
-
const
|
|
17
|
+
exports.init = void 0;
|
|
18
|
+
const logger_1 = require("./logger");
|
|
19
|
+
const reporters_1 = require("./reporters");
|
|
20
20
|
const gtmReporter_1 = require("./reporters/gtmReporter");
|
|
21
21
|
const amplifyReporter_1 = require("./reporters/amplifyReporter");
|
|
22
|
+
const datadogReporter_1 = require("./reporters/datadogReporter");
|
|
23
|
+
const logReporter_1 = require("./reporters/logReporter");
|
|
22
24
|
__exportStar(require("./types"), exports);
|
|
23
|
-
exports
|
|
24
|
-
|
|
25
|
+
__exportStar(require("./reporters"), exports);
|
|
26
|
+
__exportStar(require("./logger"), exports);
|
|
25
27
|
let initialized = false;
|
|
26
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Initializes the logger and reporters.
|
|
30
|
+
* @param config Reporter config options.
|
|
31
|
+
*/
|
|
27
32
|
function init(config) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
if (config.amplify) {
|
|
34
|
-
reporters.push((0, amplifyReporter_1.amplifyReporter)(config, config.amplify));
|
|
35
|
-
}
|
|
36
|
-
if (config.gtm) {
|
|
37
|
-
reporters.push((0, gtmReporter_1.gtmReporter)());
|
|
33
|
+
var _a, _b, _c, _d, _e, _f;
|
|
34
|
+
// Some reporters don't like being initialized multiple times.
|
|
35
|
+
if (initialized) {
|
|
36
|
+
return;
|
|
38
37
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
38
|
+
initialized = true;
|
|
39
|
+
// Log Reporter enabled by default.
|
|
40
|
+
const logReporterConfig = (_b = (_a = config.reporters) === null || _a === void 0 ? void 0 : _a.log) !== null && _b !== void 0 ? _b : true;
|
|
41
|
+
if (logReporterConfig !== false) {
|
|
42
|
+
reporters_1.reporters['log'] = (0, logReporter_1.logReporter)(config, typeof logReporterConfig === 'boolean' ? {} : logReporterConfig);
|
|
43
|
+
}
|
|
44
|
+
if ((_c = config.reporters) === null || _c === void 0 ? void 0 : _c.datadog) {
|
|
45
|
+
reporters_1.reporters['datadog'] = (0, datadogReporter_1.datadogReporter)(config, config.reporters.datadog);
|
|
46
|
+
}
|
|
47
|
+
if ((_d = config.reporters) === null || _d === void 0 ? void 0 : _d.amplify) {
|
|
48
|
+
reporters_1.reporters['amplify'] = (0, amplifyReporter_1.amplifyReporter)(config, config.reporters.amplify);
|
|
49
|
+
}
|
|
50
|
+
if ((_e = config.reporters) === null || _e === void 0 ? void 0 : _e.gtm) {
|
|
51
|
+
reporters_1.reporters['gtm'] = (0, gtmReporter_1.gtmReporter)(config, typeof config.reporters.gtm === 'boolean' ? {} : config.reporters.gtm);
|
|
52
|
+
}
|
|
53
|
+
const defaultMetadata = {
|
|
54
|
+
...((_f = config.defaultMetadata) !== null && _f !== void 0 ? _f : {}),
|
|
55
|
+
service: config.service,
|
|
56
|
+
environment: config.environment,
|
|
57
|
+
version: config.version,
|
|
58
|
+
};
|
|
59
|
+
// Sets the global log level, if specified.
|
|
60
|
+
if (config.logLevel) {
|
|
61
|
+
(0, logger_1.setLogLevel)(config.logLevel);
|
|
62
|
+
}
|
|
63
|
+
// Sets the global event level, if specified.
|
|
64
|
+
if (config.eventLevel) {
|
|
65
|
+
(0, reporters_1.setEventLevel)(config.eventLevel);
|
|
66
|
+
}
|
|
67
|
+
(0, reporters_1.addMetadata)(defaultMetadata);
|
|
45
68
|
}
|
|
46
69
|
exports.init = init;
|
|
47
|
-
function trackEvent(event) {
|
|
48
|
-
for (const reporter of reporters) {
|
|
49
|
-
reporter.trackEvent(event);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
exports.trackEvent = trackEvent;
|
|
53
|
-
function addBreadcrumb(breadcrumb) {
|
|
54
|
-
for (const reporter of reporters) {
|
|
55
|
-
reporter.addBreadcrumb(breadcrumb);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
exports.addBreadcrumb = addBreadcrumb;
|
|
59
|
-
function addMetadata(metadata) {
|
|
60
|
-
for (const reporter of reporters) {
|
|
61
|
-
reporter.addMetadata(metadata);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
exports.addMetadata = addMetadata;
|
|
65
|
-
function setUser(user) {
|
|
66
|
-
for (const reporter of reporters) {
|
|
67
|
-
reporter.setUser(user);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
exports.setUser = setUser;
|
|
71
|
-
function setRouteName(routeName) {
|
|
72
|
-
for (const reporter of reporters) {
|
|
73
|
-
reporter.setRouteName(routeName);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
exports.setRouteName = setRouteName;
|
|
77
|
-
function setPageName(pageName) {
|
|
78
|
-
for (const reporter of reporters) {
|
|
79
|
-
reporter.setPageName(pageName);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
exports.setPageName = setPageName;
|
|
83
|
-
function reportError(error, metadata) {
|
|
84
|
-
for (const reporter of reporters) {
|
|
85
|
-
reporter.reportError(error, metadata);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
exports.reportError = reportError;
|
|
89
|
-
function recordSession() {
|
|
90
|
-
for (const reporter of reporters) {
|
|
91
|
-
reporter.recordSession();
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
exports.recordSession = recordSession;
|
|
95
|
-
function recordSessionStop() {
|
|
96
|
-
for (const reporter of reporters) {
|
|
97
|
-
reporter.recordSessionStop();
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
exports.recordSessionStop = recordSessionStop;
|
|
101
|
-
function trackEventSinceLastAction(event) {
|
|
102
|
-
const lastEvent = getLastTrackedEvent();
|
|
103
|
-
if (lastEvent) {
|
|
104
|
-
const duration = new Date().getTime() - lastEvent.occurred.getTime();
|
|
105
|
-
trackEvent({
|
|
106
|
-
...event,
|
|
107
|
-
metadata: {
|
|
108
|
-
...event.metadata,
|
|
109
|
-
lastEventName: lastEvent.message,
|
|
110
|
-
timeSinceLastEvent: duration,
|
|
111
|
-
},
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
trackEvent(event);
|
|
116
|
-
}
|
|
117
|
-
sessionStorage.setItem('loggerLastEvent', JSON.stringify({ ...event, occurred: new Date() }));
|
|
118
|
-
}
|
|
119
|
-
exports.trackEventSinceLastAction = trackEventSinceLastAction;
|
|
120
|
-
function getLastTrackedEvent() {
|
|
121
|
-
const eventStr = sessionStorage.getItem('loggerLastEvent');
|
|
122
|
-
if (!eventStr)
|
|
123
|
-
return null;
|
|
124
|
-
const event = JSON.parse(eventStr);
|
|
125
|
-
event.occurred = new Date(event.occurred);
|
|
126
|
-
return event;
|
|
127
|
-
}
|
|
128
|
-
exports.getLastTrackedEvent = getLastTrackedEvent;
|
|
129
|
-
function createLogger(name, options) {
|
|
130
|
-
if (!initialized) {
|
|
131
|
-
throw new Error('You must call init on BrowserLogger before creating a logger');
|
|
132
|
-
}
|
|
133
|
-
if (ddInitialized) {
|
|
134
|
-
return (0, datadogReporter_1.datadogLogger)(name, options);
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
return (0, utils_1.consoleLogger)(name, options);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
exports.createLogger = createLogger;
|
|
141
70
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,qCAAuC;AACvC,2CAAoE;AACpE,yDAAsD;AACtD,iEAA8D;AAC9D,iEAA8D;AAC9D,yDAAsD;AAEtD,0CAAwB;AACxB,8CAA4B;AAC5B,2CAAyB;AAIzB,IAAI,WAAW,GAAG,KAAK,CAAC;AAExB;;;GAGG;AACH,SAAgB,IAAI,CAAC,MAAoB;;IACvC,8DAA8D;IAC9D,IAAI,WAAW,EAAE;QACf,OAAO;KACR;IACD,WAAW,GAAG,IAAI,CAAC;IAEnB,mCAAmC;IACnC,MAAM,iBAAiB,GAAG,MAAA,MAAA,MAAM,CAAC,SAAS,0CAAE,GAAG,mCAAI,IAAI,CAAC;IACxD,IAAI,iBAAiB,KAAK,KAAK,EAAE;QAC/B,qBAAS,CAAC,KAAK,CAAC,GAAG,IAAA,yBAAW,EAAC,MAAM,EAAE,OAAO,iBAAiB,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC;KACzG;IAED,IAAI,MAAA,MAAM,CAAC,SAAS,0CAAE,OAAO,EAAE;QAC7B,qBAAS,CAAC,SAAS,CAAC,GAAG,IAAA,iCAAe,EAAC,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;KAC1E;IAED,IAAI,MAAA,MAAM,CAAC,SAAS,0CAAE,OAAO,EAAE;QAC7B,qBAAS,CAAC,SAAS,CAAC,GAAG,IAAA,iCAAe,EAAC,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;KAC1E;IAED,IAAI,MAAA,MAAM,CAAC,SAAS,0CAAE,GAAG,EAAE;QACzB,qBAAS,CAAC,KAAK,CAAC,GAAG,IAAA,yBAAW,EAAC,MAAM,EAAE,OAAO,MAAM,CAAC,SAAS,CAAC,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;KAC/G;IAED,MAAM,eAAe,GAAG;QACtB,GAAG,CAAC,MAAA,MAAM,CAAC,eAAe,mCAAI,EAAE,CAAC;QACjC,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,WAAW,EAAE,MAAM,CAAC,WAAW;QAC/B,OAAO,EAAE,MAAM,CAAC,OAAO;KACxB,CAAC;IAEF,2CAA2C;IAC3C,IAAI,MAAM,CAAC,QAAQ,EAAE;QACnB,IAAA,oBAAW,EAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;KAC9B;IAED,6CAA6C;IAC7C,IAAI,MAAM,CAAC,UAAU,EAAE;QACrB,IAAA,yBAAa,EAAC,MAAM,CAAC,UAAU,CAAC,CAAC;KAClC;IAED,IAAA,uBAAW,EAAC,eAAe,CAAC,CAAC;AAC/B,CAAC;AA3CD,oBA2CC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ILogTransport, LogLevel, LogTransportConfigBase } from '../types';
|
|
2
|
+
declare const colors: {
|
|
3
|
+
bold: number[];
|
|
4
|
+
italic: number[];
|
|
5
|
+
underline: number[];
|
|
6
|
+
inverse: number[];
|
|
7
|
+
white: number[];
|
|
8
|
+
grey: number[];
|
|
9
|
+
black: number[];
|
|
10
|
+
blue: number[];
|
|
11
|
+
cyan: number[];
|
|
12
|
+
green: number[];
|
|
13
|
+
magenta: number[];
|
|
14
|
+
red: number[];
|
|
15
|
+
yellow: number[];
|
|
16
|
+
};
|
|
17
|
+
export declare type ColorizeColor = keyof typeof colors;
|
|
18
|
+
/**
|
|
19
|
+
* Colorize a string with a specified color.
|
|
20
|
+
* @param str String to colorize
|
|
21
|
+
* @param color Color to colorize the string with
|
|
22
|
+
* @returns Colorized string
|
|
23
|
+
*/
|
|
24
|
+
export declare function colorize(str: string, color: ColorizeColor): string;
|
|
25
|
+
export interface ConsoleLogTransportConfig extends LogTransportConfigBase {
|
|
26
|
+
/**
|
|
27
|
+
* Add colors to log messages.
|
|
28
|
+
*/
|
|
29
|
+
colorize?: false | typeof colorize;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the color for the log level.
|
|
32
|
+
*/
|
|
33
|
+
getLogLevelColor?: (level: LogLevel) => ColorizeColor;
|
|
34
|
+
}
|
|
35
|
+
export declare function consoleTransport(config?: ConsoleLogTransportConfig): ILogTransport & ConsoleLogTransportConfig;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=consoleTransport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consoleTransport.d.ts","sourceRoot":"","sources":["../../src/logger/consoleTransport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAG3E,QAAA,MAAM,MAAM;;;;;;;;;;;;;;CAcX,CAAC;AACF,oBAAY,aAAa,GAAG,MAAM,OAAO,MAAM,CAAC;AAEhD;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,UAGzD;AAWD,MAAM,WAAW,yBAA0B,SAAQ,sBAAsB;IACvE;;OAEG;IACH,QAAQ,CAAC,EAAE,KAAK,GAAG,OAAO,QAAQ,CAAC;IAEnC;;OAEG;IACH,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,KAAK,aAAa,CAAC;CACvD;AAED,wBAAgB,gBAAgB,CAAC,MAAM,GAAE,yBAA8B,6CAgDtE"}
|