app-tracker 1.4.1 → 1.4.2

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/index.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { LogType } from './log';
2
+ export declare class AppLogger {
3
+ static sendLogsOnLocal: boolean;
4
+ private static endpointUrl;
5
+ private static apiKey;
6
+ private static logs;
7
+ private static sendTries;
8
+ private static maxSendTries;
9
+ private static isSending;
10
+ private static maxLogsCount;
11
+ private static userAgent;
12
+ private static logLevel;
13
+ private static sendLogsByTypesImmediately;
14
+ private static isNode;
15
+ static init(apiKey: string, config?: AppLoggerConfig): void;
16
+ static debug(message?: string, ...optionalParams: any[]): void;
17
+ static info(message?: string, ...optionalParams: any[]): void;
18
+ static log(message?: string, ...optionalParams: any[]): void;
19
+ static warn(message?: string, ...optionalParams: any[]): void;
20
+ static error(message?: string, ...optionalParams: any[]): void;
21
+ private static addLog;
22
+ private static logToConsole;
23
+ private static isRunningLocal;
24
+ private static sendLogsToServer;
25
+ private static checkLogType;
26
+ private static sendLogsToServerCore;
27
+ }
28
+ export declare class AppLoggerConfig {
29
+ sendLogsOnLocal?: boolean | undefined;
30
+ endpointUrl?: string | undefined;
31
+ maxSendTries?: number | undefined;
32
+ maxLogsCount?: number | undefined;
33
+ /** Defines which log types should be sent to the server */
34
+ logLevel?: LogType[] | undefined;
35
+ sendLogsByTypesImmediately?: LogType[] | undefined;
36
+ }
File without changes
package/log.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ export declare class Log {
2
+ logType: LogType;
3
+ message: any;
4
+ optionalParams: any[];
5
+ createdAt: Date;
6
+ }
7
+ export declare enum LogType {
8
+ debug = 0,
9
+ info = 1,
10
+ log = 2,
11
+ warn = 3,
12
+ error = 4,
13
+ critical = 5
14
+ }
package/log.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Log {
4
+ constructor() {
5
+ this.logType = LogType.log;
6
+ this.optionalParams = [];
7
+ this.createdAt = new Date();
8
+ }
9
+ }
10
+ exports.Log = Log;
11
+ var LogType;
12
+ (function (LogType) {
13
+ LogType[LogType["debug"] = 0] = "debug";
14
+ LogType[LogType["info"] = 1] = "info";
15
+ LogType[LogType["log"] = 2] = "log";
16
+ LogType[LogType["warn"] = 3] = "warn";
17
+ LogType[LogType["error"] = 4] = "error";
18
+ LogType[LogType["critical"] = 5] = "critical";
19
+ })(LogType = exports.LogType || (exports.LogType = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "app-tracker",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "App Tracker for Web-Applications",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/.prettierrc DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "printWidth": 120,
3
- "trailingComma": "all",
4
- "singleQuote": true
5
- }
package/lib/README.md DELETED
@@ -1 +0,0 @@
1
- # App Tracker for Web Applications
package/lib/package.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "name": "app-tracker",
3
- "version": "1.4.1",
4
- "description": "App Tracker for Web-Applications",
5
- "main": "index.js",
6
- "scripts": {
7
- "build": "tsc && npm run copyFile",
8
- "copyFile": "xcopy package.json lib && xcopy README.md lib && npm run terser",
9
- "terser": "terser lib/index.js --compress -o lib/index.js",
10
- "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
11
- "lint": "tslint -p tsconfig.json"
12
- },
13
- "keywords": [],
14
- "author": "Egor Kin",
15
- "license": "ISC",
16
- "devDependencies": {
17
- "@types/node": "^17.0.10",
18
- "prettier": "^1.19.1",
19
- "tslint": "^5.20.1",
20
- "tslint-config-prettier": "^1.18.0",
21
- "typescript": "^3.7.3"
22
- }
23
- }
package/src/index.ts DELETED
@@ -1,179 +0,0 @@
1
- import { Log, LogType } from './log';
2
- const os = require('os');
3
-
4
- export class AppLogger {
5
-
6
- public static sendLogsOnLocal: boolean = false;
7
- private static endpointUrl: string = 'https://api-app-tracker.azurewebsites.net/';
8
- private static apiKey: string = '';
9
- private static logs: Log[] = [];
10
- private static sendTries: number = 0;
11
- private static maxSendTries: number = 5;
12
- private static isSending: boolean = false;
13
- private static maxLogsCount: number = 10;
14
- private static userAgent: string;
15
- private static logLevel: LogType[] = [LogType.error];
16
- private static sendLogsByTypesImmediately: LogType[] | undefined = [LogType.error];
17
- private static isNode = false;
18
-
19
- public static init(apiKey: string, config?: AppLoggerConfig) {
20
- this.apiKey = apiKey;
21
- if (config) {
22
- if (config.endpointUrl) this.endpointUrl = config.endpointUrl;
23
- if (config.maxLogsCount) this.maxLogsCount = config.maxLogsCount;
24
- if (config.maxSendTries) this.maxSendTries = config.maxSendTries;
25
- if (config.sendLogsOnLocal) this.sendLogsOnLocal = config.sendLogsOnLocal;
26
- if (config.logLevel) this.logLevel = config.logLevel;
27
- if (config.sendLogsByTypesImmediately) this.sendLogsByTypesImmediately = config.sendLogsByTypesImmediately;
28
- }
29
- this.isNode = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
30
- if (!this.isNode)
31
- this.userAgent = navigator.userAgent;
32
- else {
33
- this.userAgent = JSON.stringify(os);
34
- }
35
- if (apiKey != '' && !this.isNode) {
36
- window.addEventListener('beforeunload', (e) => {
37
- e.preventDefault();
38
- e.returnValue = '';
39
- if (!this.isSending) {
40
- const logsToSend = this.logs;
41
- this.isSending = true;
42
- this.logs = [];
43
- AppLogger.sendLogsToServerCore(logsToSend);
44
- }
45
- });
46
- }
47
- }
48
-
49
- public static debug(message?: string, ...optionalParams: any[]) {
50
- AppLogger.addLog(LogType.debug, message, optionalParams);
51
- }
52
-
53
- public static info(message?: string, ...optionalParams: any[]) {
54
- AppLogger.addLog(LogType.info, message, optionalParams);
55
- }
56
-
57
- public static log(message?: string, ...optionalParams: any[]) {
58
- AppLogger.addLog(LogType.log, message, optionalParams);
59
- }
60
-
61
- public static warn(message?: string, ...optionalParams: any[]) {
62
- AppLogger.addLog(LogType.warn, message, optionalParams);
63
- }
64
-
65
- public static error(message?: string, ...optionalParams: any[]) {
66
- AppLogger.addLog(LogType.error, message, optionalParams);
67
- }
68
-
69
- private static addLog(type: LogType, message: string | undefined, optionalParams: any[]) {
70
- if (this.checkLogType(type)) {
71
- if (this.isRunningLocal() && !this.sendLogsOnLocal) {
72
- AppLogger.logToConsole(type, message, optionalParams);
73
- } else {
74
- AppLogger.sendLogsToServer(type, message, optionalParams);
75
- }
76
- } else {
77
- AppLogger.logToConsole(type, message, optionalParams);
78
- }
79
- }
80
-
81
- private static logToConsole(type: LogType, message: string | undefined, optionalParams: any[]) {
82
- switch (type) {
83
- case LogType.debug:
84
- console.debug(message, optionalParams);
85
- break;
86
- case LogType.error:
87
- console.error(message, optionalParams);
88
- break;
89
- case LogType.info:
90
- console.info(message, optionalParams);
91
- break;
92
- case LogType.log:
93
- console.log(message, optionalParams);
94
- break;
95
- case LogType.warn:
96
- console.warn(message, optionalParams);
97
- break;
98
- default:
99
- console.warn("Unkown LogType: ", type);
100
- break;
101
- }
102
- }
103
-
104
- private static isRunningLocal(): boolean {
105
- return this.isNode ? os.hostname().indexOf("local") > -1 : window.location.href.indexOf('localhost') > -1;
106
- }
107
-
108
- private static sendLogsToServer(type: LogType, message?: string, ...optionalParams: any[][]) {
109
- const log = { logType: type, message: message, optionalParams: [], createdAt: new Date() } as Log;
110
- optionalParams.forEach(op => op.forEach(o => log.optionalParams.push(o)));
111
- this.logs.push(log);
112
- if ((this.logs.length >= this.maxLogsCount && !this.isSending) ||
113
- ((this.sendLogsByTypesImmediately?.find(l => l == type) || type == LogType.critical) != null && !this.isSending)) {
114
- const logsToSend = this.logs;
115
- this.logs = [];
116
- this.isSending = true;
117
- AppLogger.sendLogsToServerCore(logsToSend);
118
- }
119
- }
120
-
121
- private static checkLogType(type: LogType): boolean {
122
- if (this.logLevel.find(l => l === type) != null || type === LogType.critical) {
123
- return true;
124
- }
125
- else {
126
- return false;
127
- }
128
- }
129
-
130
- private static async sendLogsToServerCore(logsToSend: Log[]) {
131
- if (this.sendTries < this.maxSendTries) {
132
- try {
133
- const last = this.endpointUrl.charAt(this.endpointUrl.length - 1);
134
- const response = await fetch(`${this.endpointUrl}${last === '/' ? 'logs/createLog' : '/logs/createLog'}`,
135
- {
136
- method: 'POST',
137
- headers: {
138
- 'Accept': 'application/json',
139
- 'Content-Type': 'application/json',
140
- 'X-Auth-Key': this.apiKey
141
- },
142
- body: JSON.stringify({ logs: logsToSend, userAgent: this.userAgent }, (key: string, value: any) => {
143
- if (value instanceof Error) {
144
- let error: any = <any>{};
145
-
146
- Object.getOwnPropertyNames(value).forEach(function (key) {
147
- error[key] = (<any>value)[key];
148
- });
149
- return error;
150
- }
151
- return value;
152
- })
153
- });
154
-
155
- logsToSend = [];
156
- this.isSending = false;
157
- } catch (error) {
158
- this.sendTries++;
159
- setTimeout(() => {
160
- AppLogger.sendLogsToServerCore(logsToSend);
161
- }, 10000);
162
- }
163
- } else {
164
- this.sendTries = 0;
165
- this.isSending = false;
166
- console.error('AppLogger: Could not send Logs to Server');
167
- }
168
- }
169
- }
170
-
171
- export class AppLoggerConfig {
172
- public sendLogsOnLocal?: boolean | undefined;
173
- public endpointUrl?: string | undefined;
174
- public maxSendTries?: number | undefined;
175
- public maxLogsCount?: number | undefined = 5;
176
- /** Defines which log types should be sent to the server */
177
- public logLevel?: LogType[] | undefined;
178
- public sendLogsByTypesImmediately?: LogType[] | undefined = [LogType.error];
179
- }
package/src/log.ts DELETED
@@ -1,8 +0,0 @@
1
- export class Log {
2
- public logType: LogType = LogType.log;
3
- public message: any;
4
- public optionalParams: any[] = [];
5
- public createdAt: Date = new Date();
6
- }
7
-
8
- export enum LogType { debug, info, log, warn, error, critical }
package/tsconfig.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es6",
4
- "module": "commonjs",
5
- "declaration": true,
6
- "outDir": "./lib",
7
- "strict": true
8
- },
9
- "include": [
10
- "src"
11
- ],
12
- "exclude": [
13
- "node_modules",
14
- "**/__tests__/*"
15
- ]
16
- }
package/tslint.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": [
3
- "tslint:recommended",
4
- "tslint-config-prettier"
5
- ],
6
- "rules": {
7
- "no-console": false
8
- }
9
- }