app-tracker 2.2.1 → 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 +83 -41
- package/app-tracker.service.d.ts +48 -39
- package/app-tracker.service.js +332 -1
- package/index.d.ts +4 -3
- package/index.js +9 -8
- package/log.d.ts +36 -32
- package/log.js +31 -1
- package/main.d.ts +8 -8
- package/main.js +19 -1
- package/package.json +54 -46
- package/version.d.ts +2 -0
- package/version.js +4 -0
package/README.md
CHANGED
|
@@ -1,41 +1,83 @@
|
|
|
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
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
# App Tracker for Web & Node.js
|
|
4
|
+
|
|
5
|
+
App Tracker is a TypeScript/JavaScript library for logging and tracking events in web and Node.js applications. It supports batching, flexible configuration, and sending logs to a central endpoint.
|
|
6
|
+
|
|
7
|
+
Runtime support: Node.js 16 or newer and modern browsers with `fetch`/XHR, `Promise`, and `WeakSet`. Missing browser storage or `sendBeacon` support is handled with fallbacks.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i app-tracker
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Registration & API Key
|
|
16
|
+
|
|
17
|
+
- Register at [App Tracker](https://www.app-tracker.cloud/) and create an application to get your API key.
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { AppTracker, AppTrackerService } from "app-tracker";
|
|
23
|
+
|
|
24
|
+
const config = {
|
|
25
|
+
userAgentOrOs: typeof window !== 'undefined' ? navigator.userAgent : require('os'),
|
|
26
|
+
logInConsole: true,
|
|
27
|
+
sendLogsInDevelopment: true,
|
|
28
|
+
// more options below
|
|
29
|
+
};
|
|
30
|
+
const tracker: AppTrackerService = AppTracker.init("API_KEY", config);
|
|
31
|
+
|
|
32
|
+
tracker.debug("Debug message", { foo: "bar" });
|
|
33
|
+
tracker.error("An error occurred", errorObj);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Configuration
|
|
37
|
+
|
|
38
|
+
| Property | Type | Default | Description |
|
|
39
|
+
|-----------------------------|--------------|--------------------------------|-------------|
|
|
40
|
+
| sendLogsInDevelopment | boolean | false | Send logs during development |
|
|
41
|
+
| logInConsole | boolean | false | Also log to the console |
|
|
42
|
+
| endpointUrl | string | https://app-tracker-api.azurewebsites.net/ | Target endpoint for logs |
|
|
43
|
+
| maxSendTries | number | 5 | Max. send attempts on error |
|
|
44
|
+
| retryDelayMs | number | 10000 | Delay between send attempts in milliseconds |
|
|
45
|
+
| requestTimeoutMs | number | 10000 | Timeout for each HTTP attempt in milliseconds |
|
|
46
|
+
| maxLogsCount | number | 10 | Batch size for sending logs |
|
|
47
|
+
| logLevel | LogType[] | [LogType.error] | Which log types are sent |
|
|
48
|
+
| sendLogsByTypesImmediately | LogType[] | [LogType.error, LogType.critical] | Types sent immediately |
|
|
49
|
+
| userAgentOrOs | any | | Browser: navigator.userAgent, Node: require('os') |
|
|
50
|
+
|
|
51
|
+
**LogType:** `debug`, `info`, `log`, `warn`, `error`, `critical`
|
|
52
|
+
|
|
53
|
+
## Architecture & Features
|
|
54
|
+
|
|
55
|
+
- **Singleton Service:** Always access via `AppTracker.init()` or `AppTracker.getService()`
|
|
56
|
+
- **Batching:** Logs are collected and sent after `maxLogsCount` or immediately for critical errors
|
|
57
|
+
- **Explicit flush:** Call and await `tracker.flush()` before a controlled Node.js shutdown
|
|
58
|
+
- **Environment Awareness:** Automatically adapts for browser/Node.js
|
|
59
|
+
- **Secure Sending:** API key required, retry mechanism on errors
|
|
60
|
+
- **External Dependency:** Uses `cross-fetch` for HTTP requests
|
|
61
|
+
|
|
62
|
+
## Build & Development
|
|
63
|
+
|
|
64
|
+
- **Build:** `npm run build` (TypeScript compile & copy to `lib/`)
|
|
65
|
+
- **Minify:** `npm run terser` (Terser minification of JS files)
|
|
66
|
+
- **Lint:** `npm run lint` (TSLint)
|
|
67
|
+
- **Format:** `npm run format` (Prettier)
|
|
68
|
+
|
|
69
|
+
## FAQ
|
|
70
|
+
|
|
71
|
+
**Where do I get my API key?**
|
|
72
|
+
> Register at https://www.app-tracker.cloud/ and create an application.
|
|
73
|
+
|
|
74
|
+
**How can I suppress logs in development?**
|
|
75
|
+
> Set `sendLogsInDevelopment: false` in the configuration.
|
|
76
|
+
|
|
77
|
+
**How do I send custom log types?**
|
|
78
|
+
> Use the `debug`, `info`, `warn`, `error`, `critical` methods of the service object.
|
|
79
|
+
|
|
80
|
+
## Further Information
|
|
81
|
+
- See `src/app-tracker.service.ts` for service logic
|
|
82
|
+
- See `src/log.ts` for configuration and log types
|
|
83
|
+
- See `.github/copilot-instructions.md` for developer guidelines
|
package/app-tracker.service.d.ts
CHANGED
|
@@ -1,39 +1,48 @@
|
|
|
1
|
-
import { AppTrackerConfig } from './log';
|
|
2
|
-
export declare class AppTrackerService {
|
|
3
|
-
private sendLogsInDevelopment;
|
|
4
|
-
private logInConsole;
|
|
5
|
-
private endpointUrl;
|
|
6
|
-
private apiKey;
|
|
7
|
-
private logs;
|
|
8
|
-
private
|
|
9
|
-
private
|
|
10
|
-
private
|
|
11
|
-
private
|
|
12
|
-
private
|
|
13
|
-
private
|
|
14
|
-
private
|
|
15
|
-
private
|
|
16
|
-
private
|
|
17
|
-
private
|
|
18
|
-
private
|
|
19
|
-
private
|
|
20
|
-
|
|
21
|
-
private
|
|
22
|
-
private
|
|
23
|
-
private
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
private
|
|
37
|
-
private
|
|
38
|
-
private
|
|
39
|
-
|
|
1
|
+
import { AppTrackerConfig } from './log';
|
|
2
|
+
export declare class AppTrackerService {
|
|
3
|
+
private sendLogsInDevelopment;
|
|
4
|
+
private logInConsole;
|
|
5
|
+
private endpointUrl;
|
|
6
|
+
private apiKey;
|
|
7
|
+
private logs;
|
|
8
|
+
private maxSendTries;
|
|
9
|
+
private retryDelayMs;
|
|
10
|
+
private requestTimeoutMs;
|
|
11
|
+
private isSending;
|
|
12
|
+
private activeSend?;
|
|
13
|
+
private maxLogsCount;
|
|
14
|
+
private userAgent;
|
|
15
|
+
private logLevel;
|
|
16
|
+
private sendLogsByTypesImmediately;
|
|
17
|
+
private isNode;
|
|
18
|
+
private nodeProcess;
|
|
19
|
+
private os;
|
|
20
|
+
private isRunningLocal;
|
|
21
|
+
private ident;
|
|
22
|
+
private eventListenerAdded;
|
|
23
|
+
private _boundBeforeUnloadHandler?;
|
|
24
|
+
init(apiKey: string, config?: AppTrackerConfig): void;
|
|
25
|
+
private addEventListener;
|
|
26
|
+
private removeEventListener;
|
|
27
|
+
private beforeunloadHandler;
|
|
28
|
+
private setRunningSystem;
|
|
29
|
+
getIdent(): string;
|
|
30
|
+
debug(message?: string, ...optionalParams: any[]): void;
|
|
31
|
+
info(message?: string, ...optionalParams: any[]): void;
|
|
32
|
+
log(message?: string, ...optionalParams: any[]): void;
|
|
33
|
+
warn(message?: string, ...optionalParams: any[]): void;
|
|
34
|
+
error(message?: string, ...optionalParams: any[]): void;
|
|
35
|
+
critical(message?: string, ...optionalParams: any[]): void;
|
|
36
|
+
private addLog;
|
|
37
|
+
private logToConsole;
|
|
38
|
+
private sendLogsToServer;
|
|
39
|
+
private checkLogType;
|
|
40
|
+
/** Sends all currently queued logs and any logs added while sending. */
|
|
41
|
+
flush(): Promise<void>;
|
|
42
|
+
private runSendLoop;
|
|
43
|
+
private sendLogsToServerCore;
|
|
44
|
+
private getLogsEndpoint;
|
|
45
|
+
private serializePayload;
|
|
46
|
+
private setUserAgentFromOs;
|
|
47
|
+
private createUUID;
|
|
48
|
+
}
|
package/app-tracker.service.js
CHANGED
|
@@ -1 +1,332 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const log_1 = require("./log");
|
|
13
|
+
const cross_fetch_1 = require("cross-fetch");
|
|
14
|
+
const version_1 = require("./version");
|
|
15
|
+
const safeFetch = typeof window !== 'undefined' ? cross_fetch_1.default.bind(window) : cross_fetch_1.default;
|
|
16
|
+
class AppTrackerService {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.sendLogsInDevelopment = false;
|
|
19
|
+
this.logInConsole = false;
|
|
20
|
+
this.endpointUrl = 'https://app-tracker-api.azurewebsites.net/';
|
|
21
|
+
this.apiKey = '';
|
|
22
|
+
this.logs = [];
|
|
23
|
+
this.maxSendTries = 5;
|
|
24
|
+
this.retryDelayMs = 10000;
|
|
25
|
+
this.requestTimeoutMs = 10000;
|
|
26
|
+
this.isSending = false;
|
|
27
|
+
this.maxLogsCount = 10;
|
|
28
|
+
this.userAgent = '';
|
|
29
|
+
this.logLevel = [log_1.LogType.error];
|
|
30
|
+
this.sendLogsByTypesImmediately = [log_1.LogType.error, log_1.LogType.critical];
|
|
31
|
+
this.isNode = false;
|
|
32
|
+
this.isRunningLocal = true;
|
|
33
|
+
this.ident = '';
|
|
34
|
+
this.eventListenerAdded = false;
|
|
35
|
+
}
|
|
36
|
+
init(apiKey, config) {
|
|
37
|
+
this.apiKey = apiKey;
|
|
38
|
+
if (config) {
|
|
39
|
+
if (typeof config.endpointUrl !== 'undefined')
|
|
40
|
+
this.endpointUrl = config.endpointUrl;
|
|
41
|
+
if (typeof config.maxLogsCount !== 'undefined')
|
|
42
|
+
this.maxLogsCount = config.maxLogsCount;
|
|
43
|
+
if (typeof config.maxSendTries !== 'undefined')
|
|
44
|
+
this.maxSendTries = Math.max(1, config.maxSendTries);
|
|
45
|
+
if (typeof config.retryDelayMs !== 'undefined')
|
|
46
|
+
this.retryDelayMs = Math.max(0, config.retryDelayMs);
|
|
47
|
+
if (typeof config.requestTimeoutMs !== 'undefined')
|
|
48
|
+
this.requestTimeoutMs = Math.max(1, config.requestTimeoutMs);
|
|
49
|
+
if (typeof config.sendLogsInDevelopment !== 'undefined')
|
|
50
|
+
this.sendLogsInDevelopment = config.sendLogsInDevelopment;
|
|
51
|
+
if (typeof config.logInConsole !== 'undefined')
|
|
52
|
+
this.logInConsole = config.logInConsole;
|
|
53
|
+
if (typeof config.logLevel !== 'undefined')
|
|
54
|
+
this.logLevel = config.logLevel;
|
|
55
|
+
if (typeof config.sendLogsByTypesImmediately !== 'undefined')
|
|
56
|
+
this.sendLogsByTypesImmediately = config.sendLogsByTypesImmediately;
|
|
57
|
+
}
|
|
58
|
+
this.setRunningSystem(config);
|
|
59
|
+
}
|
|
60
|
+
addEventListener(apiKey) {
|
|
61
|
+
if (apiKey != '' && !this.isNode && !this.isRunningLocal && !this.eventListenerAdded) {
|
|
62
|
+
this._boundBeforeUnloadHandler = this._boundBeforeUnloadHandler || ((e) => this.beforeunloadHandler(e));
|
|
63
|
+
window.addEventListener('beforeunload', this._boundBeforeUnloadHandler, true);
|
|
64
|
+
this.eventListenerAdded = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
removeEventListener(apiKey) {
|
|
68
|
+
if (apiKey != '' && !this.isNode && !this.isRunningLocal && this.eventListenerAdded) {
|
|
69
|
+
if (this._boundBeforeUnloadHandler) {
|
|
70
|
+
window.removeEventListener('beforeunload', this._boundBeforeUnloadHandler, true);
|
|
71
|
+
}
|
|
72
|
+
this.eventListenerAdded = false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
beforeunloadHandler(_event) {
|
|
76
|
+
if (this.logs.length === 0)
|
|
77
|
+
return;
|
|
78
|
+
const logsToSend = this.logs.splice(0);
|
|
79
|
+
const body = this.serializePayload(logsToSend, true);
|
|
80
|
+
const url = this.getLogsEndpoint();
|
|
81
|
+
// sendBeacon is specifically designed to survive page termination. The API
|
|
82
|
+
// key is also accepted in the request body by the server's ApiKeyGuard.
|
|
83
|
+
// text/plain keeps a cross-origin beacon CORS-safelisted; the API explicitly
|
|
84
|
+
// enables JSON parsing for this content type.
|
|
85
|
+
if (typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function') {
|
|
86
|
+
const accepted = navigator.sendBeacon(url, new Blob([body], { type: 'text/plain;charset=UTF-8' }));
|
|
87
|
+
if (accepted)
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
// Keep the logs queued if the browser could not schedule the beacon. A
|
|
91
|
+
// keepalive fetch is the best available fallback during page termination.
|
|
92
|
+
this.logs.unshift(...logsToSend);
|
|
93
|
+
void this.flush();
|
|
94
|
+
}
|
|
95
|
+
setRunningSystem(config) {
|
|
96
|
+
var _a, _b, _c, _d, _e;
|
|
97
|
+
this.nodeProcess = typeof globalThis !== 'undefined' ? globalThis.process : undefined;
|
|
98
|
+
this.isNode = Object.prototype.toString.call(this.nodeProcess || 0) === '[object process]';
|
|
99
|
+
if (!this.isNode) {
|
|
100
|
+
this.userAgent = (_a = config) === null || _a === void 0 ? void 0 : _a.userAgentOrOs;
|
|
101
|
+
this.ident = this.getIdent();
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
this.os = (_b = config) === null || _b === void 0 ? void 0 : _b.userAgentOrOs;
|
|
105
|
+
this.userAgent = this.setUserAgentFromOs();
|
|
106
|
+
}
|
|
107
|
+
this.isRunningLocal = this.isNode
|
|
108
|
+
? (_e = (_d = (_c = this.nodeProcess) === null || _c === void 0 ? void 0 : _c.env) === null || _d === void 0 ? void 0 : _d.NODE_ENV, (_e !== null && _e !== void 0 ? _e : '')).indexOf("development") > -1
|
|
109
|
+
: window.location.href.indexOf('localhost') > -1;
|
|
110
|
+
}
|
|
111
|
+
getIdent() {
|
|
112
|
+
try {
|
|
113
|
+
let ident = localStorage.getItem('App-Tracker.device-ident');
|
|
114
|
+
if (!ident) {
|
|
115
|
+
ident = this.createUUID();
|
|
116
|
+
localStorage.setItem('App-Tracker.device-ident', ident);
|
|
117
|
+
}
|
|
118
|
+
return ident;
|
|
119
|
+
}
|
|
120
|
+
catch (_) {
|
|
121
|
+
// Storage can be unavailable in sandboxed iframes or privacy modes.
|
|
122
|
+
return this.createUUID();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
debug(message, ...optionalParams) {
|
|
126
|
+
this.addLog(log_1.LogType.debug, message, ...optionalParams);
|
|
127
|
+
}
|
|
128
|
+
info(message, ...optionalParams) {
|
|
129
|
+
this.addLog(log_1.LogType.info, message, ...optionalParams);
|
|
130
|
+
}
|
|
131
|
+
log(message, ...optionalParams) {
|
|
132
|
+
this.addLog(log_1.LogType.log, message, ...optionalParams);
|
|
133
|
+
}
|
|
134
|
+
warn(message, ...optionalParams) {
|
|
135
|
+
this.addLog(log_1.LogType.warn, message, ...optionalParams);
|
|
136
|
+
}
|
|
137
|
+
error(message, ...optionalParams) {
|
|
138
|
+
this.addLog(log_1.LogType.error, message, ...optionalParams);
|
|
139
|
+
}
|
|
140
|
+
critical(message, ...optionalParams) {
|
|
141
|
+
this.addLog(log_1.LogType.critical, message, ...optionalParams);
|
|
142
|
+
}
|
|
143
|
+
addLog(type, message, ...optionalParams) {
|
|
144
|
+
if (this.checkLogType(type)) {
|
|
145
|
+
if (this.isRunningLocal && !this.sendLogsInDevelopment) {
|
|
146
|
+
this.logToConsole(type, message, ...optionalParams);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
this.sendLogsToServer(type, message, ...optionalParams);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
this.logToConsole(type, message, ...optionalParams);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
logToConsole(type, message, ...optionalParams) {
|
|
157
|
+
if (this.logInConsole) {
|
|
158
|
+
switch (type) {
|
|
159
|
+
case log_1.LogType.debug:
|
|
160
|
+
console.debug(message, ...optionalParams);
|
|
161
|
+
break;
|
|
162
|
+
case log_1.LogType.critical:
|
|
163
|
+
case log_1.LogType.error:
|
|
164
|
+
console.error(message, ...optionalParams);
|
|
165
|
+
break;
|
|
166
|
+
case log_1.LogType.info:
|
|
167
|
+
console.info(message, ...optionalParams);
|
|
168
|
+
break;
|
|
169
|
+
case log_1.LogType.log:
|
|
170
|
+
console.log(message, ...optionalParams);
|
|
171
|
+
break;
|
|
172
|
+
case log_1.LogType.warn:
|
|
173
|
+
console.warn(message, ...optionalParams);
|
|
174
|
+
break;
|
|
175
|
+
default:
|
|
176
|
+
console.warn("Unkown LogType: ", type);
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
sendLogsToServer(type, message, ...optionalParams) {
|
|
182
|
+
var _a, _b;
|
|
183
|
+
const log = { logType: type, message: message, optionalParams: [], createdAt: new Date(), ident: this.ident };
|
|
184
|
+
optionalParams.forEach((op) => {
|
|
185
|
+
if (Array.isArray(op))
|
|
186
|
+
op.forEach(o => log.optionalParams.push(o));
|
|
187
|
+
else
|
|
188
|
+
log.optionalParams.push(op);
|
|
189
|
+
});
|
|
190
|
+
this.logs.push(log);
|
|
191
|
+
const sendImmediately = type === log_1.LogType.critical || ((_a = this.sendLogsByTypesImmediately) === null || _a === void 0 ? void 0 : _a.includes(type)) === true;
|
|
192
|
+
if (this.logs.length >= this.maxLogsCount || sendImmediately)
|
|
193
|
+
void this.flush();
|
|
194
|
+
if (((_b = this.logs) === null || _b === void 0 ? void 0 : _b.length) > 0 && !this.eventListenerAdded) {
|
|
195
|
+
this.addEventListener(this.apiKey);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
checkLogType(type) {
|
|
199
|
+
if (this.logLevel.find(l => l === type) != null || type === log_1.LogType.critical) {
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/** Sends all currently queued logs and any logs added while sending. */
|
|
207
|
+
flush() {
|
|
208
|
+
if (this.activeSend)
|
|
209
|
+
return this.activeSend;
|
|
210
|
+
if (this.logs.length === 0)
|
|
211
|
+
return Promise.resolve();
|
|
212
|
+
this.isSending = true;
|
|
213
|
+
this.removeEventListener(this.apiKey);
|
|
214
|
+
this.activeSend = this.runSendLoop();
|
|
215
|
+
return this.activeSend;
|
|
216
|
+
}
|
|
217
|
+
runSendLoop() {
|
|
218
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
219
|
+
try {
|
|
220
|
+
while (this.logs.length > 0) {
|
|
221
|
+
const logsToSend = this.logs.splice(0);
|
|
222
|
+
const sent = yield this.sendLogsToServerCore(logsToSend);
|
|
223
|
+
if (!sent) {
|
|
224
|
+
// Preserve the failed batch for a later explicit flush or log event.
|
|
225
|
+
this.logs.unshift(...logsToSend);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
this.isSending = false;
|
|
232
|
+
this.activeSend = undefined;
|
|
233
|
+
if (this.logs.length > 0)
|
|
234
|
+
this.addEventListener(this.apiKey);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
sendLogsToServerCore(logsToSend) {
|
|
239
|
+
var _a;
|
|
240
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
241
|
+
for (let sendTry = 1; sendTry <= this.maxSendTries; sendTry++) {
|
|
242
|
+
const abortController = typeof AbortController !== 'undefined' ? new AbortController() : undefined;
|
|
243
|
+
const timeout = abortController ? setTimeout(() => abortController.abort(), this.requestTimeoutMs) : undefined;
|
|
244
|
+
try {
|
|
245
|
+
const body = this.serializePayload(logsToSend);
|
|
246
|
+
const response = yield safeFetch(this.getLogsEndpoint(), {
|
|
247
|
+
method: 'POST',
|
|
248
|
+
headers: {
|
|
249
|
+
'Accept': 'application/json',
|
|
250
|
+
'Content-Type': 'application/json',
|
|
251
|
+
'X-Auth-Key': this.apiKey
|
|
252
|
+
},
|
|
253
|
+
body,
|
|
254
|
+
signal: (_a = abortController) === null || _a === void 0 ? void 0 : _a.signal,
|
|
255
|
+
// Browsers impose a small quota on keepalive requests.
|
|
256
|
+
keepalive: body.length <= 60000
|
|
257
|
+
});
|
|
258
|
+
if (!response.ok)
|
|
259
|
+
throw new Error(`HTTP ${response.status} ${response.statusText}`);
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
console.error(`AppLogger: Could not send logs to server (attempt ${sendTry}/${this.maxSendTries})`, error);
|
|
264
|
+
if (sendTry < this.maxSendTries)
|
|
265
|
+
yield new Promise(resolve => setTimeout(resolve, this.retryDelayMs));
|
|
266
|
+
}
|
|
267
|
+
finally {
|
|
268
|
+
if (timeout !== undefined)
|
|
269
|
+
clearTimeout(timeout);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
console.error('AppLogger: Could not send logs to server finally; batch remains queued');
|
|
273
|
+
return false;
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
getLogsEndpoint() {
|
|
277
|
+
const last = this.endpointUrl.charAt(this.endpointUrl.length - 1);
|
|
278
|
+
return `${this.endpointUrl}${last === '/' ? 'logs/createLog' : '/logs/createLog'}`;
|
|
279
|
+
}
|
|
280
|
+
serializePayload(logsToSend, includeApiKey = false) {
|
|
281
|
+
const seen = new WeakSet();
|
|
282
|
+
const payload = {
|
|
283
|
+
ident: this.ident,
|
|
284
|
+
logs: logsToSend,
|
|
285
|
+
osType: this.isNode ? 'node' : 'web',
|
|
286
|
+
packageVersion: version_1.APP_TRACKER_VERSION,
|
|
287
|
+
userAgent: this.userAgent
|
|
288
|
+
};
|
|
289
|
+
if (includeApiKey)
|
|
290
|
+
payload.apiKey = this.apiKey;
|
|
291
|
+
return JSON.stringify(payload, (_key, value) => {
|
|
292
|
+
if (value instanceof Error) {
|
|
293
|
+
if (seen.has(value))
|
|
294
|
+
return '[Circular]';
|
|
295
|
+
seen.add(value);
|
|
296
|
+
const serializedError = {};
|
|
297
|
+
Object.getOwnPropertyNames(value).forEach(key => serializedError[key] = value[key]);
|
|
298
|
+
return serializedError;
|
|
299
|
+
}
|
|
300
|
+
if (typeof value === 'bigint')
|
|
301
|
+
return value.toString();
|
|
302
|
+
if (value && typeof value === 'object') {
|
|
303
|
+
if (seen.has(value))
|
|
304
|
+
return '[Circular]';
|
|
305
|
+
seen.add(value);
|
|
306
|
+
}
|
|
307
|
+
return value;
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
setUserAgentFromOs() {
|
|
311
|
+
var _a, _b;
|
|
312
|
+
if (!this.os) {
|
|
313
|
+
return JSON.stringify({ platform: (_a = this.nodeProcess) === null || _a === void 0 ? void 0 : _a.platform, version: (_b = this.nodeProcess) === null || _b === void 0 ? void 0 : _b.version });
|
|
314
|
+
}
|
|
315
|
+
const osInfo = {
|
|
316
|
+
arch: this.os.arch(),
|
|
317
|
+
platform: this.os.platform(),
|
|
318
|
+
release: this.os.release(),
|
|
319
|
+
type: this.os.type(),
|
|
320
|
+
version: this.os.version(),
|
|
321
|
+
hostname: this.os.hostname()
|
|
322
|
+
};
|
|
323
|
+
return JSON.stringify(osInfo);
|
|
324
|
+
}
|
|
325
|
+
createUUID() {
|
|
326
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
327
|
+
const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
328
|
+
return v.toString(16);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
exports.AppTrackerService = AppTrackerService;
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export * from './main';
|
|
2
|
-
export * from './app-tracker.service';
|
|
3
|
-
export * from './log';
|
|
1
|
+
export * from './main';
|
|
2
|
+
export * from './app-tracker.service';
|
|
3
|
+
export * from './log';
|
|
4
|
+
export * from './version';
|
package/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
function __export(m) {
|
|
3
|
-
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
4
|
-
}
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
__export(require("./main"));
|
|
7
|
-
__export(require("./app-tracker.service"));
|
|
8
|
-
__export(require("./log"));
|
|
1
|
+
"use strict";
|
|
2
|
+
function __export(m) {
|
|
3
|
+
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
4
|
+
}
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
__export(require("./main"));
|
|
7
|
+
__export(require("./app-tracker.service"));
|
|
8
|
+
__export(require("./log"));
|
|
9
|
+
__export(require("./version"));
|
package/log.d.ts
CHANGED
|
@@ -1,32 +1,36 @@
|
|
|
1
|
-
export declare class AppTrackerConfig {
|
|
2
|
-
/** Should logs in the development environment be sent to the server. For Node enviroment process.env.NODE_ENV must be set to 'development' */
|
|
3
|
-
sendLogsInDevelopment?: boolean;
|
|
4
|
-
logInConsole?: boolean;
|
|
5
|
-
endpointUrl?: string;
|
|
6
|
-
maxSendTries?: number;
|
|
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
|
-
|
|
1
|
+
export declare class AppTrackerConfig {
|
|
2
|
+
/** Should logs in the development environment be sent to the server. For Node enviroment process.env.NODE_ENV must be set to 'development' */
|
|
3
|
+
sendLogsInDevelopment?: boolean;
|
|
4
|
+
logInConsole?: boolean;
|
|
5
|
+
endpointUrl?: string;
|
|
6
|
+
maxSendTries?: number;
|
|
7
|
+
/** Delay between send attempts. */
|
|
8
|
+
retryDelayMs?: number;
|
|
9
|
+
/** Maximum duration of a single request before it is aborted. */
|
|
10
|
+
requestTimeoutMs?: number;
|
|
11
|
+
maxLogsCount?: number;
|
|
12
|
+
/** Defines which log types should be sent to the server */
|
|
13
|
+
logLevel?: LogType[];
|
|
14
|
+
sendLogsByTypesImmediately?: LogType[];
|
|
15
|
+
/**
|
|
16
|
+
* Please pass:
|
|
17
|
+
* Web -> navigator.userAgent,
|
|
18
|
+
* Node -> os (from require('os'))
|
|
19
|
+
*/
|
|
20
|
+
userAgentOrOs: any;
|
|
21
|
+
}
|
|
22
|
+
export declare class Log {
|
|
23
|
+
logType: LogType;
|
|
24
|
+
message: any;
|
|
25
|
+
optionalParams: any[];
|
|
26
|
+
createdAt: Date;
|
|
27
|
+
ident: string;
|
|
28
|
+
}
|
|
29
|
+
export declare enum LogType {
|
|
30
|
+
debug = 0,
|
|
31
|
+
info = 1,
|
|
32
|
+
log = 2,
|
|
33
|
+
warn = 3,
|
|
34
|
+
error = 4,
|
|
35
|
+
critical = 5
|
|
36
|
+
}
|
package/log.js
CHANGED
|
@@ -1 +1,31 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class AppTrackerConfig {
|
|
4
|
+
constructor() {
|
|
5
|
+
/** Delay between send attempts. */
|
|
6
|
+
this.retryDelayMs = 10000;
|
|
7
|
+
/** Maximum duration of a single request before it is aborted. */
|
|
8
|
+
this.requestTimeoutMs = 10000;
|
|
9
|
+
this.maxLogsCount = 5;
|
|
10
|
+
this.sendLogsByTypesImmediately = [LogType.error];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.AppTrackerConfig = AppTrackerConfig;
|
|
14
|
+
class Log {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.logType = LogType.log;
|
|
17
|
+
this.optionalParams = [];
|
|
18
|
+
this.createdAt = new Date();
|
|
19
|
+
this.ident = '';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Log = Log;
|
|
23
|
+
var LogType;
|
|
24
|
+
(function (LogType) {
|
|
25
|
+
LogType[LogType["debug"] = 0] = "debug";
|
|
26
|
+
LogType[LogType["info"] = 1] = "info";
|
|
27
|
+
LogType[LogType["log"] = 2] = "log";
|
|
28
|
+
LogType[LogType["warn"] = 3] = "warn";
|
|
29
|
+
LogType[LogType["error"] = 4] = "error";
|
|
30
|
+
LogType[LogType["critical"] = 5] = "critical";
|
|
31
|
+
})(LogType = exports.LogType || (exports.LogType = {}));
|
package/main.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { AppTrackerService } from './app-tracker.service';
|
|
2
|
-
import { AppTrackerConfig } from './log';
|
|
3
|
-
export declare class AppTracker {
|
|
4
|
-
private static _service;
|
|
5
|
-
private static get service();
|
|
6
|
-
static init(apiKey: string, config?: AppTrackerConfig): AppTrackerService;
|
|
7
|
-
static getService(): AppTrackerService;
|
|
8
|
-
}
|
|
1
|
+
import { AppTrackerService } from './app-tracker.service';
|
|
2
|
+
import { AppTrackerConfig } from './log';
|
|
3
|
+
export declare class AppTracker {
|
|
4
|
+
private static _service;
|
|
5
|
+
private static get service();
|
|
6
|
+
static init(apiKey: string, config?: AppTrackerConfig): AppTrackerService;
|
|
7
|
+
static getService(): AppTrackerService;
|
|
8
|
+
}
|
package/main.js
CHANGED
|
@@ -1 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const app_tracker_service_1 = require("./app-tracker.service");
|
|
4
|
+
class AppTracker {
|
|
5
|
+
static get service() {
|
|
6
|
+
if (!this._service)
|
|
7
|
+
this._service = new app_tracker_service_1.AppTrackerService();
|
|
8
|
+
return this._service;
|
|
9
|
+
}
|
|
10
|
+
static init(apiKey, config) {
|
|
11
|
+
AppTracker.service.init(apiKey, config);
|
|
12
|
+
return AppTracker.service;
|
|
13
|
+
}
|
|
14
|
+
static getService() {
|
|
15
|
+
return AppTracker.service;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.AppTracker = AppTracker;
|
|
19
|
+
AppTracker._service = null;
|
package/package.json
CHANGED
|
@@ -1,46 +1,54 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "app-tracker",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "App Tracker for Web-Applications",
|
|
5
|
-
"main": "index.js",
|
|
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
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "app-tracker",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "App Tracker for Web-Applications",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/Wigtertainment-Ltd/App-Tracker-PKG.git"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc && npm run copyFile",
|
|
12
|
+
"copyFile": "node scripts/copy-package-files.js",
|
|
13
|
+
"terser": "terser lib/main.js -c -m -o lib/main.js && terser lib/log.js -c -m -o lib/log.js && terser lib/app-tracker.service.js -c -m -o lib/app-tracker.service.js",
|
|
14
|
+
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
|
|
15
|
+
"lint": "tslint -p tsconfig.json",
|
|
16
|
+
"test": "npm run build && node test/logging.integration.js && node test/browser-build.js"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"logging",
|
|
20
|
+
"protocol",
|
|
21
|
+
"event logging",
|
|
22
|
+
"monitoring",
|
|
23
|
+
"tracking",
|
|
24
|
+
"recording",
|
|
25
|
+
"debugging",
|
|
26
|
+
"iroubleshooting",
|
|
27
|
+
"user activity",
|
|
28
|
+
"data analysis",
|
|
29
|
+
"system events",
|
|
30
|
+
"performance monitoring",
|
|
31
|
+
"processing",
|
|
32
|
+
"archiving",
|
|
33
|
+
"alerting",
|
|
34
|
+
"notification",
|
|
35
|
+
"integration",
|
|
36
|
+
"interfaces"
|
|
37
|
+
],
|
|
38
|
+
"author": "Wigtertainment Ltd",
|
|
39
|
+
"license": "ISC",
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=16"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^17.0.10",
|
|
45
|
+
"parcel": "^2.7.0",
|
|
46
|
+
"prettier": "^1.19.1",
|
|
47
|
+
"tslint": "^5.20.1",
|
|
48
|
+
"tslint-config-prettier": "^1.18.0",
|
|
49
|
+
"typescript": "^3.7.3"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"cross-fetch": "^3.1.5"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/version.d.ts
ADDED
package/version.js
ADDED