@viplance/nestjs-logger 0.1.0 → 0.1.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/README.md +37 -2
- package/dist/db.service.d.ts +8 -0
- package/dist/db.service.js +51 -0
- package/dist/db.service.js.map +1 -0
- package/dist/defaults.d.ts +1 -0
- package/dist/defaults.js +5 -0
- package/dist/defaults.js.map +1 -0
- package/dist/entities/log.entity.d.ts +6 -0
- package/dist/entities/log.entity.js +19 -0
- package/dist/entities/log.entity.js.map +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/log.interceptor.d.ts +8 -0
- package/dist/log.interceptor.js +42 -0
- package/dist/log.interceptor.js.map +1 -0
- package/dist/log.module.d.ts +4 -0
- package/dist/log.module.js +40 -0
- package/dist/log.module.js.map +1 -0
- package/dist/log.service.d.ts +12 -0
- package/dist/log.service.js +66 -0
- package/dist/log.service.js.map +1 -0
- package/dist/services/db.service.d.ts +9 -0
- package/dist/services/db.service.js +65 -0
- package/dist/services/db.service.js.map +1 -0
- package/dist/services/log.service.d.ts +18 -0
- package/dist/services/log.service.js +99 -0
- package/dist/services/log.service.js.map +1 -0
- package/dist/services/memory-db.service.d.ts +9 -0
- package/dist/services/memory-db.service.js +63 -0
- package/dist/services/memory-db.service.js.map +1 -0
- package/dist/types/db.type.d.ts +1 -0
- package/dist/types/db.type.js +3 -0
- package/dist/types/db.type.js.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +19 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/log.type.d.ts +7 -0
- package/dist/types/log.type.js +12 -0
- package/dist/types/log.type.js.map +1 -0
- package/dist/types/options.type.d.ts +10 -0
- package/dist/types/options.type.js +3 -0
- package/dist/types/options.type.js.map +1 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/package.json +4 -2
- package/src/defaults.ts +1 -0
- package/src/entities/log.entity.ts +16 -0
- package/src/index.ts +3 -2
- package/src/log.interceptor.ts +27 -0
- package/src/log.module.ts +36 -0
- package/src/services/log.service.ts +101 -0
- package/src/services/memory-db.service.ts +58 -0
- package/src/types/index.ts +2 -0
- package/src/types/log.type.ts +7 -0
- package/src/types/options.type.ts +11 -0
- package/src/logger.module.ts +0 -43
- package/src/logger.service.ts +0 -27
package/README.md
CHANGED
|
@@ -1,2 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
NestJS internal logging system
|
|
1
|
+
## @viplance/nestjs-logger
|
|
2
|
+
# NestJS internal logging system
|
|
3
|
+
|
|
4
|
+
1. Install the package npm i @viplance/nestjs-logger
|
|
5
|
+
2. Import the module in app.module.ts
|
|
6
|
+
import { LogModule } from '@viplance/nestjs-logger';
|
|
7
|
+
|
|
8
|
+
@Module({
|
|
9
|
+
imports: [
|
|
10
|
+
...,
|
|
11
|
+
LogModule,
|
|
12
|
+
]
|
|
13
|
+
})
|
|
14
|
+
3. Use the LogService in case of custom logs.
|
|
15
|
+
3. If you want to catch all errors automatically, put the code in main.ts
|
|
16
|
+
import { LogModule } from '@viplance/nestjs-logger';
|
|
17
|
+
|
|
18
|
+
await LogModule.connect(app, {
|
|
19
|
+
path: '/logs',
|
|
20
|
+
database: {
|
|
21
|
+
type: 'mongodb',
|
|
22
|
+
host: 'localhost',
|
|
23
|
+
port: 27017,
|
|
24
|
+
collection: 'logs'
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
`path` and `database` options are optional
|
|
29
|
+
The logs could be available at your_application_url/<path>
|
|
30
|
+
By default the logs will be stored in memory and deleted when the application stops.
|
|
31
|
+
|
|
32
|
+
Available service methods:
|
|
33
|
+
- log()
|
|
34
|
+
- error()
|
|
35
|
+
- warn()
|
|
36
|
+
- debug()
|
|
37
|
+
- verbose()
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare class LogDbService {
|
|
2
|
+
private db;
|
|
3
|
+
constructor();
|
|
4
|
+
insert(table: string, data: any): string;
|
|
5
|
+
getMany(table: string): any[];
|
|
6
|
+
getOneById(table: string, id: string): any;
|
|
7
|
+
getManyByProperty(table: string, field: string, value: string): any[];
|
|
8
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// DB layer
|
|
3
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
4
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
6
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
7
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
8
|
+
};
|
|
9
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
10
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.LogDbService = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const crypto_1 = require("crypto");
|
|
16
|
+
const defaults_1 = require("./defaults");
|
|
17
|
+
const tables = [defaults_1.defaultTable];
|
|
18
|
+
let LogDbService = class LogDbService {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.db = {};
|
|
21
|
+
for (const table of tables) {
|
|
22
|
+
this.db[table] = [];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
insert(table, data) {
|
|
26
|
+
// generate new random ID
|
|
27
|
+
const randomData = (0, crypto_1.randomBytes)(32).toString("hex");
|
|
28
|
+
const id = (0, crypto_1.createHash)("sha256").update(randomData).digest("hex");
|
|
29
|
+
this.db[table].push({
|
|
30
|
+
...data,
|
|
31
|
+
id, // creader unique index
|
|
32
|
+
});
|
|
33
|
+
console.log(this.db);
|
|
34
|
+
return id;
|
|
35
|
+
}
|
|
36
|
+
getMany(table) {
|
|
37
|
+
return this.db[table];
|
|
38
|
+
}
|
|
39
|
+
getOneById(table, id) {
|
|
40
|
+
return this.db[table].find((item) => item.id === id);
|
|
41
|
+
}
|
|
42
|
+
getManyByProperty(table, field, value) {
|
|
43
|
+
return this.db[table].filter((item) => item[field] === value);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
exports.LogDbService = LogDbService;
|
|
47
|
+
exports.LogDbService = LogDbService = __decorate([
|
|
48
|
+
(0, common_1.Injectable)(),
|
|
49
|
+
__metadata("design:paramtypes", [])
|
|
50
|
+
], LogDbService);
|
|
51
|
+
//# sourceMappingURL=db.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db.service.js","sourceRoot":"","sources":["../src/db.service.ts"],"names":[],"mappings":";AAAA,WAAW;;;;;;;;;;;;AAEX,2CAA4C;AAC5C,mCAAiD;AACjD,yCAA0C;AAE1C,MAAM,MAAM,GAAG,CAAC,uBAAY,CAAC,CAAC;AAGvB,IAAM,YAAY,GAAlB,MAAM,YAAY;IAGvB;QAFQ,OAAE,GAA6B,EAAE,CAAC;QAGxC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;QACtB,CAAC;IACH,CAAC;IAEM,MAAM,CAAC,KAAa,EAAE,IAAS;QACpC,yBAAyB;QACzB,MAAM,UAAU,GAAG,IAAA,oBAAW,EAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACnD,MAAM,EAAE,GAAG,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAEjE,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;YAClB,GAAG,IAAI;YACP,EAAE,EAAE,uBAAuB;SAC5B,CAAC,CAAC;QAEH,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAErB,OAAO,EAAE,CAAC;IACZ,CAAC;IAEM,OAAO,CAAC,KAAa;QAC1B,OAAO,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC;IAEM,UAAU,CAAC,KAAa,EAAE,EAAU;QACzC,OAAO,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IACvD,CAAC;IAEM,iBAAiB,CAAC,KAAa,EAAE,KAAa,EAAE,KAAa;QAClE,OAAO,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC,CAAC;IAChE,CAAC;CACF,CAAA;AAnCY,oCAAY;uBAAZ,YAAY;IADxB,IAAA,mBAAU,GAAE;;GACA,YAAY,CAmCxB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const defaultTable = "logs";
|
package/dist/defaults.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaults.js","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG,MAAM,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createLogEntity = createLogEntity;
|
|
4
|
+
const typeorm_1 = require("typeorm");
|
|
5
|
+
function createLogEntity(name) {
|
|
6
|
+
return new typeorm_1.EntitySchema({
|
|
7
|
+
name,
|
|
8
|
+
columns: {
|
|
9
|
+
_id: {
|
|
10
|
+
type: String,
|
|
11
|
+
objectId: true,
|
|
12
|
+
primary: true,
|
|
13
|
+
},
|
|
14
|
+
type: { type: String },
|
|
15
|
+
message: { type: String },
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=log.entity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.entity.js","sourceRoot":"","sources":["../../src/entities/log.entity.ts"],"names":[],"mappings":";;AAEA,0CAaC;AAfD,qCAAuC;AAEvC,SAAgB,eAAe,CAAC,IAAY;IAC1C,OAAO,IAAI,sBAAY,CAAC;QACtB,IAAI;QACJ,OAAO,EAAE;YACP,GAAG,EAAE;gBACH,IAAI,EAAE,MAAM;gBACZ,QAAQ,EAAE,IAAI;gBACd,OAAO,EAAE,IAAI;aACd;YACD,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;YACtB,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;SAC1B;KACF,CAAC,CAAC;AACL,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
1
|
+
export * from "./log.interceptor";
|
|
2
|
+
export * from "./log.module";
|
|
3
|
+
export * from "./services/log.service";
|
package/dist/index.js
CHANGED
|
@@ -14,6 +14,7 @@ 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
|
-
__exportStar(require("./
|
|
18
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./log.interceptor"), exports);
|
|
18
|
+
__exportStar(require("./log.module"), exports);
|
|
19
|
+
__exportStar(require("./services/log.service"), exports);
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,+CAA6B;AAC7B,yDAAuC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CallHandler, ExecutionContext, NestInterceptor } from "@nestjs/common";
|
|
2
|
+
import { Observable } from "rxjs";
|
|
3
|
+
import { LogService } from "./services/log.service";
|
|
4
|
+
export declare class LogInterceptor implements NestInterceptor {
|
|
5
|
+
private readonly logService;
|
|
6
|
+
constructor(logService: LogService);
|
|
7
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<any>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.LogInterceptor = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const rxjs_1 = require("rxjs");
|
|
18
|
+
const log_service_1 = require("./services/log.service");
|
|
19
|
+
let LogInterceptor = class LogInterceptor {
|
|
20
|
+
constructor(logService) {
|
|
21
|
+
this.logService = logService;
|
|
22
|
+
}
|
|
23
|
+
intercept(context, next) {
|
|
24
|
+
return next.handle().pipe((0, rxjs_1.tap)({
|
|
25
|
+
error: (error) => {
|
|
26
|
+
var _a;
|
|
27
|
+
// error handler
|
|
28
|
+
(_a = this.logService) === null || _a === void 0 ? void 0 : _a.error(error.message, error.stack, context);
|
|
29
|
+
},
|
|
30
|
+
complete: () => {
|
|
31
|
+
// normal request
|
|
32
|
+
},
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
exports.LogInterceptor = LogInterceptor;
|
|
37
|
+
exports.LogInterceptor = LogInterceptor = __decorate([
|
|
38
|
+
(0, common_1.Injectable)(),
|
|
39
|
+
__param(0, (0, common_1.Inject)(log_service_1.LogService)),
|
|
40
|
+
__metadata("design:paramtypes", [log_service_1.LogService])
|
|
41
|
+
], LogInterceptor);
|
|
42
|
+
//# sourceMappingURL=log.interceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.interceptor.js","sourceRoot":"","sources":["../src/log.interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,2CAAoD;AACpD,+BAAuC;AACvC,wDAAoD;AAG7C,IAAM,cAAc,GAApB,MAAM,cAAc;IACzB,YAAiD,UAAsB;QAAtB,eAAU,GAAV,UAAU,CAAY;IAAG,CAAC;IAE3E,SAAS,CAAC,OAAyB,EAAE,IAAiB;QACpD,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CACvB,IAAA,UAAG,EAAC;YACF,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE;;gBACf,gBAAgB;gBAChB,MAAA,IAAI,CAAC,UAAU,0CAAE,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YAC9D,CAAC;YACD,QAAQ,EAAE,GAAG,EAAE;gBACb,iBAAiB;YACnB,CAAC;SACF,CAAC,CACH,CAAC;IACJ,CAAC;CACF,CAAA;AAhBY,wCAAc;yBAAd,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAEE,WAAA,IAAA,eAAM,EAAC,wBAAU,CAAC,CAAA;qCAA8B,wBAAU;GAD5D,cAAc,CAgB1B"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.LogModule = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const log_service_1 = require("./services/log.service");
|
|
12
|
+
const memory_db_service_1 = require("./services/memory-db.service");
|
|
13
|
+
const log_interceptor_1 = require("./log.interceptor");
|
|
14
|
+
const typeorm_1 = require("@nestjs/typeorm");
|
|
15
|
+
let LogModule = class LogModule {
|
|
16
|
+
static async connect(app, options) {
|
|
17
|
+
app.resolve(log_service_1.LogService);
|
|
18
|
+
const logService = await app.resolve(log_service_1.LogService);
|
|
19
|
+
app.useGlobalInterceptors(new log_interceptor_1.LogInterceptor(logService)); // intercept all errors
|
|
20
|
+
if (options === null || options === void 0 ? void 0 : options.path) {
|
|
21
|
+
const httpAdapter = app.getHttpAdapter();
|
|
22
|
+
httpAdapter.get(options.path, async (req, res) => {
|
|
23
|
+
res.json(logService.getAll());
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
if (options) {
|
|
27
|
+
await logService.connectDb(options);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
exports.LogModule = LogModule;
|
|
32
|
+
exports.LogModule = LogModule = __decorate([
|
|
33
|
+
(0, common_1.Global)(),
|
|
34
|
+
(0, common_1.Module)({
|
|
35
|
+
imports: [typeorm_1.TypeOrmModule],
|
|
36
|
+
providers: [log_service_1.LogService, memory_db_service_1.MemoryDbService],
|
|
37
|
+
exports: [typeorm_1.TypeOrmModule, log_service_1.LogService, memory_db_service_1.MemoryDbService],
|
|
38
|
+
})
|
|
39
|
+
], LogModule);
|
|
40
|
+
//# sourceMappingURL=log.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.module.js","sourceRoot":"","sources":["../src/log.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAgD;AAChD,wDAAoD;AACpD,oEAA+D;AAC/D,uDAAmD;AAEnD,6CAAgD;AAQzC,IAAM,SAAS,GAAf,MAAM,SAAS;IACb,MAAM,CAAC,KAAK,CAAC,OAAO,CACzB,GAAQ,EACR,OAA0B;QAE1B,GAAG,CAAC,OAAO,CAAC,wBAAU,CAAC,CAAC;QAExB,MAAM,UAAU,GAAe,MAAM,GAAG,CAAC,OAAO,CAAC,wBAAU,CAAC,CAAC;QAE7D,GAAG,CAAC,qBAAqB,CAAC,IAAI,gCAAc,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,uBAAuB;QAElF,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,EAAE,CAAC;YAClB,MAAM,WAAW,GAAG,GAAG,CAAC,cAAc,EAAE,CAAC;YACzC,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,GAAQ,EAAE,GAAQ,EAAE,EAAE;gBACzD,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC;YAChC,CAAC,CAAC,CAAC;QACL,CAAC;QAED,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;CACF,CAAA;AAtBY,8BAAS;oBAAT,SAAS;IANrB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,CAAC,uBAAa,CAAC;QACxB,SAAS,EAAE,CAAC,wBAAU,EAAE,mCAAe,CAAC;QACxC,OAAO,EAAE,CAAC,uBAAa,EAAE,wBAAU,EAAE,mCAAe,CAAC;KACtD,CAAC;GACW,SAAS,CAsBrB"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ExecutionContext, LoggerService } from "@nestjs/common";
|
|
2
|
+
import { LogDbService } from "./db.service";
|
|
3
|
+
export declare class LogService implements LoggerService {
|
|
4
|
+
private readonly dbService;
|
|
5
|
+
constructor(dbService: LogDbService);
|
|
6
|
+
log(message: string, context?: string): void;
|
|
7
|
+
error(message: string, trace?: string, context?: ExecutionContext): void;
|
|
8
|
+
warn(message: string, context?: string): void;
|
|
9
|
+
debug(message: string, context?: string): void;
|
|
10
|
+
verbose(message: string, context?: string): void;
|
|
11
|
+
private smartInsert;
|
|
12
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.LogService = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const db_service_1 = require("./db.service");
|
|
15
|
+
const defaults_1 = require("./defaults");
|
|
16
|
+
const types_1 = require("./types");
|
|
17
|
+
let LogService = class LogService {
|
|
18
|
+
constructor(dbService) {
|
|
19
|
+
this.dbService = dbService;
|
|
20
|
+
}
|
|
21
|
+
log(message, context) {
|
|
22
|
+
this.smartInsert(defaults_1.defaultTable, {
|
|
23
|
+
type: types_1.LogType.LOG,
|
|
24
|
+
message,
|
|
25
|
+
context,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
error(message, trace, context) {
|
|
29
|
+
this.smartInsert(defaults_1.defaultTable, {
|
|
30
|
+
type: types_1.LogType.ERROR,
|
|
31
|
+
message,
|
|
32
|
+
trace,
|
|
33
|
+
context,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
warn(message, context) {
|
|
37
|
+
this.smartInsert(defaults_1.defaultTable, {
|
|
38
|
+
type: types_1.LogType.WARN,
|
|
39
|
+
message,
|
|
40
|
+
context,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
debug(message, context) {
|
|
44
|
+
this.smartInsert(defaults_1.defaultTable, {
|
|
45
|
+
type: types_1.LogType.DEBUG,
|
|
46
|
+
message,
|
|
47
|
+
context,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
verbose(message, context) {
|
|
51
|
+
this.smartInsert(defaults_1.defaultTable, {
|
|
52
|
+
type: types_1.LogType.VERBOSE,
|
|
53
|
+
message,
|
|
54
|
+
context,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
smartInsert(table, data) {
|
|
58
|
+
return this.dbService.insert(table, data);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
exports.LogService = LogService;
|
|
62
|
+
exports.LogService = LogService = __decorate([
|
|
63
|
+
(0, common_1.Injectable)({ scope: common_1.Scope.TRANSIENT }),
|
|
64
|
+
__metadata("design:paramtypes", [db_service_1.LogDbService])
|
|
65
|
+
], LogService);
|
|
66
|
+
//# sourceMappingURL=log.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.service.js","sourceRoot":"","sources":["../src/log.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAKwB;AACxB,6CAA4C;AAC5C,yCAA0C;AAC1C,mCAAkC;AAG3B,IAAM,UAAU,GAAhB,MAAM,UAAU;IACrB,YAA6B,SAAuB;QAAvB,cAAS,GAAT,SAAS,CAAc;IAAG,CAAC;IAExD,GAAG,CAAC,OAAe,EAAE,OAAgB;QACnC,IAAI,CAAC,WAAW,CAAC,uBAAY,EAAE;YAC7B,IAAI,EAAE,eAAO,CAAC,GAAG;YACjB,OAAO;YACP,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,OAAe,EAAE,KAAc,EAAE,OAA0B;QAC/D,IAAI,CAAC,WAAW,CAAC,uBAAY,EAAE;YAC7B,IAAI,EAAE,eAAO,CAAC,KAAK;YACnB,OAAO;YACP,KAAK;YACL,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IAED,IAAI,CAAC,OAAe,EAAE,OAAgB;QACpC,IAAI,CAAC,WAAW,CAAC,uBAAY,EAAE;YAC7B,IAAI,EAAE,eAAO,CAAC,IAAI;YAClB,OAAO;YACP,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,OAAe,EAAE,OAAgB;QACrC,IAAI,CAAC,WAAW,CAAC,uBAAY,EAAE;YAC7B,IAAI,EAAE,eAAO,CAAC,KAAK;YACnB,OAAO;YACP,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IAED,OAAO,CAAC,OAAe,EAAE,OAAgB;QACvC,IAAI,CAAC,WAAW,CAAC,uBAAY,EAAE;YAC7B,IAAI,EAAE,eAAO,CAAC,OAAO;YACrB,OAAO;YACP,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IAEO,WAAW,CAAC,KAAa,EAAE,IAAS;QAC1C,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC5C,CAAC;CACF,CAAA;AA/CY,gCAAU;qBAAV,UAAU;IADtB,IAAA,mBAAU,EAAC,EAAE,KAAK,EAAE,cAAK,CAAC,SAAS,EAAE,CAAC;qCAEG,yBAAY;GADzC,UAAU,CA+CtB"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare class MemoryDbService {
|
|
2
|
+
private db;
|
|
3
|
+
constructor();
|
|
4
|
+
insert(table: string, data: any): string;
|
|
5
|
+
update(table: string, condition: any, data: any): string;
|
|
6
|
+
getMany(table: string): any[];
|
|
7
|
+
getOneById(table: string, id: string): any;
|
|
8
|
+
getManyByProperty(table: string, field: string, value: string): any[];
|
|
9
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MemoryDbService = void 0;
|
|
13
|
+
// Memory DB layer
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const crypto_1 = require("crypto");
|
|
16
|
+
const defaults_1 = require("../defaults");
|
|
17
|
+
const tables = [defaults_1.defaultTable];
|
|
18
|
+
let MemoryDbService = class MemoryDbService {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.db = {};
|
|
21
|
+
for (const table of tables) {
|
|
22
|
+
this.db[table] = [];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
insert(table, data) {
|
|
26
|
+
// generate new random ID
|
|
27
|
+
const randomData = (0, crypto_1.randomBytes)(32).toString("hex");
|
|
28
|
+
const id = (0, crypto_1.createHash)("sha256").update(randomData).digest("hex");
|
|
29
|
+
this.db[table].push({
|
|
30
|
+
...data,
|
|
31
|
+
id, // creader unique index
|
|
32
|
+
});
|
|
33
|
+
console.log(this.db);
|
|
34
|
+
return id;
|
|
35
|
+
}
|
|
36
|
+
update(table, condition, data) {
|
|
37
|
+
// generate new random ID
|
|
38
|
+
const randomData = (0, crypto_1.randomBytes)(32).toString("hex");
|
|
39
|
+
const id = (0, crypto_1.createHash)("sha256").update(randomData).digest("hex");
|
|
40
|
+
this.db[table].push({
|
|
41
|
+
...data,
|
|
42
|
+
id, // creader unique index
|
|
43
|
+
});
|
|
44
|
+
console.log(this.db);
|
|
45
|
+
return id;
|
|
46
|
+
}
|
|
47
|
+
getMany(table) {
|
|
48
|
+
return this.db[table].map((log) => ({
|
|
49
|
+
type: log.type,
|
|
50
|
+
message: log.message,
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
getOneById(table, id) {
|
|
54
|
+
return this.db[table].find((item) => item.id === id);
|
|
55
|
+
}
|
|
56
|
+
getManyByProperty(table, field, value) {
|
|
57
|
+
return this.db[table].filter((item) => item[field] === value);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
exports.MemoryDbService = MemoryDbService;
|
|
61
|
+
exports.MemoryDbService = MemoryDbService = __decorate([
|
|
62
|
+
(0, common_1.Injectable)(),
|
|
63
|
+
__metadata("design:paramtypes", [])
|
|
64
|
+
], MemoryDbService);
|
|
65
|
+
//# sourceMappingURL=db.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db.service.js","sourceRoot":"","sources":["../../src/services/db.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kBAAkB;AAClB,2CAA4C;AAC5C,mCAAiD;AACjD,0CAA2C;AAE3C,MAAM,MAAM,GAAG,CAAC,uBAAY,CAAC,CAAC;AAGvB,IAAM,eAAe,GAArB,MAAM,eAAe;IAG1B;QAFQ,OAAE,GAA6B,EAAE,CAAC;QAGxC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;QACtB,CAAC;IACH,CAAC;IAEM,MAAM,CAAC,KAAa,EAAE,IAAS;QACpC,yBAAyB;QACzB,MAAM,UAAU,GAAG,IAAA,oBAAW,EAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACnD,MAAM,EAAE,GAAG,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAEjE,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;YAClB,GAAG,IAAI;YACP,EAAE,EAAE,uBAAuB;SAC5B,CAAC,CAAC;QAEH,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAErB,OAAO,EAAE,CAAC;IACZ,CA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,18 @@
|
|
|
1
|
+
import { ExecutionContext, LoggerService } from "@nestjs/common";
|
|
2
|
+
import { MemoryDbService } from "./memory-db.service";
|
|
3
|
+
import { LogModuleOptions } from "../types";
|
|
4
|
+
import { DataSource, EntitySchema } from "typeorm";
|
|
5
|
+
export declare class LogService implements LoggerService {
|
|
6
|
+
private readonly MemoryDbService;
|
|
7
|
+
static connection: DataSource;
|
|
8
|
+
static Log: EntitySchema;
|
|
9
|
+
constructor(MemoryDbService: MemoryDbService);
|
|
10
|
+
connectDb(options: LogModuleOptions): Promise<DataSource>;
|
|
11
|
+
log(message: string, context?: string): void;
|
|
12
|
+
error(message: string, trace?: string, context?: ExecutionContext): void;
|
|
13
|
+
warn(message: string, context?: string): void;
|
|
14
|
+
debug(message: string, context?: string): void;
|
|
15
|
+
verbose(message: string, context?: string): void;
|
|
16
|
+
getAll(): any[];
|
|
17
|
+
private smartInsert;
|
|
18
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var LogService_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.LogService = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const memory_db_service_1 = require("./memory-db.service");
|
|
16
|
+
const defaults_1 = require("../defaults");
|
|
17
|
+
const types_1 = require("../types");
|
|
18
|
+
const typeorm_1 = require("typeorm");
|
|
19
|
+
const log_entity_1 = require("../entities/log.entity");
|
|
20
|
+
let LogService = LogService_1 = class LogService {
|
|
21
|
+
constructor(MemoryDbService // @InjectRepository(Log) // private readonly userRepository: Repository<Log>
|
|
22
|
+
) {
|
|
23
|
+
this.MemoryDbService = MemoryDbService;
|
|
24
|
+
}
|
|
25
|
+
async connectDb(options) {
|
|
26
|
+
var _a, _b, _c, _d, _e, _f;
|
|
27
|
+
const tableName = ((_a = options.database) === null || _a === void 0 ? void 0 : _a.collection) || ((_b = options.database) === null || _b === void 0 ? void 0 : _b.table) || "logs";
|
|
28
|
+
LogService_1.Log = (0, log_entity_1.createLogEntity)(tableName);
|
|
29
|
+
const dataSourceOptions = {
|
|
30
|
+
type: (_c = options.database) === null || _c === void 0 ? void 0 : _c.type,
|
|
31
|
+
database: (_d = options.database) === null || _d === void 0 ? void 0 : _d.database,
|
|
32
|
+
host: (_e = options.database) === null || _e === void 0 ? void 0 : _e.host,
|
|
33
|
+
port: (_f = options.database) === null || _f === void 0 ? void 0 : _f.port,
|
|
34
|
+
entities: [LogService_1.Log],
|
|
35
|
+
};
|
|
36
|
+
LogService_1.connection = new typeorm_1.DataSource(dataSourceOptions);
|
|
37
|
+
await LogService_1.connection.initialize();
|
|
38
|
+
return LogService_1.connection;
|
|
39
|
+
}
|
|
40
|
+
log(message, context) {
|
|
41
|
+
this.smartInsert(defaults_1.defaultTable, {
|
|
42
|
+
type: types_1.LogType.LOG,
|
|
43
|
+
message,
|
|
44
|
+
context,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
error(message, trace, context) {
|
|
48
|
+
this.smartInsert(defaults_1.defaultTable, {
|
|
49
|
+
type: types_1.LogType.ERROR,
|
|
50
|
+
message,
|
|
51
|
+
trace,
|
|
52
|
+
context,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
warn(message, context) {
|
|
56
|
+
this.smartInsert(defaults_1.defaultTable, {
|
|
57
|
+
type: types_1.LogType.WARN,
|
|
58
|
+
message,
|
|
59
|
+
context,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
debug(message, context) {
|
|
63
|
+
this.smartInsert(defaults_1.defaultTable, {
|
|
64
|
+
type: types_1.LogType.DEBUG,
|
|
65
|
+
message,
|
|
66
|
+
context,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
verbose(message, context) {
|
|
70
|
+
this.smartInsert(defaults_1.defaultTable, {
|
|
71
|
+
type: types_1.LogType.VERBOSE,
|
|
72
|
+
message,
|
|
73
|
+
context,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
getAll() {
|
|
77
|
+
return this.MemoryDbService.getMany(defaults_1.defaultTable);
|
|
78
|
+
}
|
|
79
|
+
async smartInsert(table, data) {
|
|
80
|
+
return await LogService_1.connection.manager.insert(LogService_1.Log, {
|
|
81
|
+
type: data.type,
|
|
82
|
+
message: data.message,
|
|
83
|
+
count: 1,
|
|
84
|
+
});
|
|
85
|
+
// return this.MemoryDbService.insert(table, {
|
|
86
|
+
// ...data,
|
|
87
|
+
// count: 1,
|
|
88
|
+
// createdAt: new Date(),
|
|
89
|
+
// updatedAt: new Date(),
|
|
90
|
+
// });
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
exports.LogService = LogService;
|
|
94
|
+
exports.LogService = LogService = LogService_1 = __decorate([
|
|
95
|
+
(0, common_1.Injectable)({ scope: common_1.Scope.TRANSIENT }),
|
|
96
|
+
__metadata("design:paramtypes", [memory_db_service_1.MemoryDbService // @InjectRepository(Log) // private readonly userRepository: Repository<Log>
|
|
97
|
+
])
|
|
98
|
+
], LogService);
|
|
99
|
+
//# sourceMappingURL=log.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.service.js","sourceRoot":"","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-0,0 +1,9 @@
|
|
|
1
|
+
export declare class MemoryDbService {
|
|
2
|
+
private db;
|
|
3
|
+
constructor();
|
|
4
|
+
insert(table: string, data: any): string;
|
|
5
|
+
update(table: string, condition: any, data: any): string;
|
|
6
|
+
getMany(table: string): any[];
|
|
7
|
+
getOneById(table: string, id: string): any;
|
|
8
|
+
getManyByProperty(table: string, field: string, value: string): any[];
|
|
9
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MemoryDbService = void 0;
|
|
13
|
+
// Memory DB layer
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const crypto_1 = require("crypto");
|
|
16
|
+
const defaults_1 = require("../defaults");
|
|
17
|
+
const tables = [defaults_1.defaultTable];
|
|
18
|
+
let MemoryDbService = class MemoryDbService {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.db = {};
|
|
21
|
+
for (const table of tables) {
|
|
22
|
+
this.db[table] = [];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
insert(table, data) {
|
|
26
|
+
// generate new random ID
|
|
27
|
+
const randomData = (0, crypto_1.randomBytes)(32).toString("hex");
|
|
28
|
+
const id = (0, crypto_1.createHash)("sha256").update(randomData).digest("hex");
|
|
29
|
+
this.db[table].push({
|
|
30
|
+
...data,
|
|
31
|
+
id, // creader unique index
|
|
32
|
+
});
|
|
33
|
+
return id;
|
|
34
|
+
}
|
|
35
|
+
update(table, condition, data) {
|
|
36
|
+
// generate new random ID
|
|
37
|
+
const randomData = (0, crypto_1.randomBytes)(32).toString("hex");
|
|
38
|
+
const id = (0, crypto_1.createHash)("sha256").update(randomData).digest("hex");
|
|
39
|
+
this.db[table].push({
|
|
40
|
+
...data,
|
|
41
|
+
id, // creader unique index
|
|
42
|
+
});
|
|
43
|
+
return id;
|
|
44
|
+
}
|
|
45
|
+
getMany(table) {
|
|
46
|
+
return this.db[table].map((log) => ({
|
|
47
|
+
type: log.type,
|
|
48
|
+
message: log.message,
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
getOneById(table, id) {
|
|
52
|
+
return this.db[table].find((item) => item.id === id);
|
|
53
|
+
}
|
|
54
|
+
getManyByProperty(table, field, value) {
|
|
55
|
+
return this.db[table].filter((item) => item[field] === value);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
exports.MemoryDbService = MemoryDbService;
|
|
59
|
+
exports.MemoryDbService = MemoryDbService = __decorate([
|
|
60
|
+
(0, common_1.Injectable)(),
|
|
61
|
+
__metadata("design:paramtypes", [])
|
|
62
|
+
], MemoryDbService);
|
|
63
|
+
//# sourceMappingURL=memory-db.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-db.service.js","sourceRoot":"","sources":["../../src/services/memory-db.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kBAAkB;AAClB,2CAA4C;AAC5C,mCAAiD;AACjD,0CAA2C;AAE3C,MAAM,MAAM,GAAG,CAAC,uBAAY,CAAC,CAAC;AAGvB,IAAM,eAAe,GAArB,MAAM,eAAe;IAG1B;QAFQ,OAAE,G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CAAC;IACN,CAAC;IAEM,UAAU,CAAC,KAAa,EAAE,EAAU;QACzC,OAAO,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IACvD,CAAC;IAEM,iBAAiB,CAAC,KAAa,EAAE,KAAa,EAAE,KAAa;QAClE,OAAO,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC,CAAC;IAChE,CAAC;CACF,CAAA;AAjDY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;;GACA,eAAe,CAiD3B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type DbType = "memory" | "mongo" | "mysql" | "postgresql" | "sqlite";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db.type.js","sourceRoot":"","sources":["../../src/types/db.type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,19 @@
|
|
|
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("./log.type"), exports);
|
|
18
|
+
__exportStar(require("./options.type"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,iDAA+B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LogType = void 0;
|
|
4
|
+
var LogType;
|
|
5
|
+
(function (LogType) {
|
|
6
|
+
LogType["LOG"] = "log";
|
|
7
|
+
LogType["ERROR"] = "error";
|
|
8
|
+
LogType["WARN"] = "warn";
|
|
9
|
+
LogType["DEBUG"] = "debug";
|
|
10
|
+
LogType["VERBOSE"] = "verbose";
|
|
11
|
+
})(LogType || (exports.LogType = LogType = {}));
|
|
12
|
+
//# sourceMappingURL=log.type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.type.js","sourceRoot":"","sources":["../../src/types/log.type.ts"],"names":[],"mappings":";;;AAAA,IAAY,OAMX;AAND,WAAY,OAAO;IACjB,sBAAW,CAAA;IACX,0BAAe,CAAA;IACf,wBAAa,CAAA;IACb,0BAAe,CAAA;IACf,8BAAmB,CAAA;AACrB,CAAC,EANW,OAAO,uBAAP,OAAO,QAMlB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.type.js","sourceRoot":"","sources":["../../src/types/options.type.ts"],"names":[],"mappings":""}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LogType = void 0;
|
|
4
|
+
var LogType;
|
|
5
|
+
(function (LogType) {
|
|
6
|
+
LogType["LOG"] = "log";
|
|
7
|
+
LogType["ERROR"] = "error";
|
|
8
|
+
LogType["WARN"] = "warn";
|
|
9
|
+
LogType["DEBUG"] = "debug";
|
|
10
|
+
LogType["VERBOSE"] = "verbose";
|
|
11
|
+
})(LogType || (exports.LogType = LogType = {}));
|
|
12
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAAA,IAAY,OAMX;AAND,WAAY,OAAO;IACjB,sBAAW,CAAA;IACX,0BAAe,CAAA;IACf,wBAAa,CAAA;IACb,0BAAe,CAAA;IACf,8BAAmB,CAAA;AACrB,CAAC,EANW,OAAO,uBAAP,OAAO,QAMlB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@viplance/nestjs-logger",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "NestJS internal logging system",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,8 +14,10 @@
|
|
|
14
14
|
"peerDependencies": {
|
|
15
15
|
"@nestjs/common": "^10.0.0",
|
|
16
16
|
"@nestjs/core": "^10.0.0",
|
|
17
|
+
"@nestjs/typeorm": "^11.0.0",
|
|
17
18
|
"reflect-metadata": "^0.2.2",
|
|
18
|
-
"rxjs": "^7.8.2"
|
|
19
|
+
"rxjs": "^7.8.2",
|
|
20
|
+
"typeorm": "^0.3.27"
|
|
19
21
|
},
|
|
20
22
|
"devDependencies": {
|
|
21
23
|
"@types/node": "^24.5.2",
|
package/src/defaults.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const defaultTable = "logs";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EntitySchema } from "typeorm";
|
|
2
|
+
|
|
3
|
+
export function createLogEntity(name: string) {
|
|
4
|
+
return new EntitySchema({
|
|
5
|
+
name,
|
|
6
|
+
columns: {
|
|
7
|
+
_id: {
|
|
8
|
+
type: String,
|
|
9
|
+
objectId: true,
|
|
10
|
+
primary: true,
|
|
11
|
+
},
|
|
12
|
+
type: { type: String },
|
|
13
|
+
message: { type: String },
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
1
|
+
export * from "./log.interceptor";
|
|
2
|
+
export * from "./log.module";
|
|
3
|
+
export * from "./services/log.service";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CallHandler,
|
|
3
|
+
ExecutionContext,
|
|
4
|
+
NestInterceptor,
|
|
5
|
+
} from "@nestjs/common";
|
|
6
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
7
|
+
import { Observable, tap } from "rxjs";
|
|
8
|
+
import { LogService } from "./services/log.service";
|
|
9
|
+
|
|
10
|
+
@Injectable()
|
|
11
|
+
export class LogInterceptor implements NestInterceptor {
|
|
12
|
+
constructor(@Inject(LogService) private readonly logService: LogService) {}
|
|
13
|
+
|
|
14
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
|
15
|
+
return next.handle().pipe(
|
|
16
|
+
tap({
|
|
17
|
+
error: (error) => {
|
|
18
|
+
// error handler
|
|
19
|
+
this.logService?.error(error.message, error.stack, context);
|
|
20
|
+
},
|
|
21
|
+
complete: () => {
|
|
22
|
+
// normal request
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Module, Global } from "@nestjs/common";
|
|
2
|
+
import { LogService } from "./services/log.service";
|
|
3
|
+
import { MemoryDbService } from "./services/memory-db.service";
|
|
4
|
+
import { LogInterceptor } from "./log.interceptor";
|
|
5
|
+
import { LogModuleOptions } from "./types";
|
|
6
|
+
import { TypeOrmModule } from "@nestjs/typeorm";
|
|
7
|
+
|
|
8
|
+
@Global()
|
|
9
|
+
@Module({
|
|
10
|
+
imports: [TypeOrmModule],
|
|
11
|
+
providers: [LogService, MemoryDbService],
|
|
12
|
+
exports: [TypeOrmModule, LogService, MemoryDbService],
|
|
13
|
+
})
|
|
14
|
+
export class LogModule {
|
|
15
|
+
public static async connect(
|
|
16
|
+
app: any,
|
|
17
|
+
options?: LogModuleOptions
|
|
18
|
+
): Promise<void> {
|
|
19
|
+
app.resolve(LogService);
|
|
20
|
+
|
|
21
|
+
const logService: LogService = await app.resolve(LogService);
|
|
22
|
+
|
|
23
|
+
app.useGlobalInterceptors(new LogInterceptor(logService)); // intercept all errors
|
|
24
|
+
|
|
25
|
+
if (options?.path) {
|
|
26
|
+
const httpAdapter = app.getHttpAdapter();
|
|
27
|
+
httpAdapter.get(options.path, async (req: any, res: any) => {
|
|
28
|
+
res.json(logService.getAll());
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (options) {
|
|
33
|
+
await logService.connectDb(options);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExecutionContext,
|
|
3
|
+
Injectable,
|
|
4
|
+
LoggerService,
|
|
5
|
+
Scope,
|
|
6
|
+
} from "@nestjs/common";
|
|
7
|
+
import { MemoryDbService } from "./memory-db.service";
|
|
8
|
+
import { defaultTable } from "../defaults";
|
|
9
|
+
import { LogModuleOptions, LogType } from "../types";
|
|
10
|
+
import { DataSource, DataSourceOptions, EntitySchema } from "typeorm";
|
|
11
|
+
import { createLogEntity } from "../entities/log.entity";
|
|
12
|
+
|
|
13
|
+
@Injectable({ scope: Scope.TRANSIENT })
|
|
14
|
+
export class LogService implements LoggerService {
|
|
15
|
+
static connection: DataSource;
|
|
16
|
+
static Log: EntitySchema;
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
private readonly MemoryDbService: MemoryDbService // @InjectRepository(Log) // private readonly userRepository: Repository<Log>
|
|
20
|
+
) {}
|
|
21
|
+
|
|
22
|
+
async connectDb(options: LogModuleOptions): Promise<DataSource> {
|
|
23
|
+
const tableName =
|
|
24
|
+
options.database?.collection || options.database?.table || "logs";
|
|
25
|
+
|
|
26
|
+
LogService.Log = createLogEntity(tableName);
|
|
27
|
+
|
|
28
|
+
const dataSourceOptions = {
|
|
29
|
+
type: options.database?.type,
|
|
30
|
+
database: options.database?.database,
|
|
31
|
+
host: options.database?.host,
|
|
32
|
+
port: options.database?.port,
|
|
33
|
+
entities: [LogService.Log],
|
|
34
|
+
} as DataSourceOptions;
|
|
35
|
+
|
|
36
|
+
LogService.connection = new DataSource(dataSourceOptions);
|
|
37
|
+
|
|
38
|
+
await LogService.connection.initialize();
|
|
39
|
+
|
|
40
|
+
return LogService.connection;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
log(message: string, context?: string) {
|
|
44
|
+
this.smartInsert(defaultTable, {
|
|
45
|
+
type: LogType.LOG,
|
|
46
|
+
message,
|
|
47
|
+
context,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
error(message: string, trace?: string, context?: ExecutionContext) {
|
|
52
|
+
this.smartInsert(defaultTable, {
|
|
53
|
+
type: LogType.ERROR,
|
|
54
|
+
message,
|
|
55
|
+
trace,
|
|
56
|
+
context,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
warn(message: string, context?: string) {
|
|
61
|
+
this.smartInsert(defaultTable, {
|
|
62
|
+
type: LogType.WARN,
|
|
63
|
+
message,
|
|
64
|
+
context,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
debug(message: string, context?: string) {
|
|
69
|
+
this.smartInsert(defaultTable, {
|
|
70
|
+
type: LogType.DEBUG,
|
|
71
|
+
message,
|
|
72
|
+
context,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
verbose(message: string, context?: string) {
|
|
77
|
+
this.smartInsert(defaultTable, {
|
|
78
|
+
type: LogType.VERBOSE,
|
|
79
|
+
message,
|
|
80
|
+
context,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
getAll(): any[] {
|
|
85
|
+
return this.MemoryDbService.getMany(defaultTable);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private async smartInsert(table: string, data: any): Promise<any> {
|
|
89
|
+
return await LogService.connection.manager.insert(LogService.Log, {
|
|
90
|
+
type: data.type,
|
|
91
|
+
message: data.message,
|
|
92
|
+
count: 1,
|
|
93
|
+
});
|
|
94
|
+
// return this.MemoryDbService.insert(table, {
|
|
95
|
+
// ...data,
|
|
96
|
+
// count: 1,
|
|
97
|
+
// createdAt: new Date(),
|
|
98
|
+
// updatedAt: new Date(),
|
|
99
|
+
// });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Memory DB layer
|
|
2
|
+
import { Injectable } from "@nestjs/common";
|
|
3
|
+
import { createHash, randomBytes } from "crypto";
|
|
4
|
+
import { defaultTable } from "../defaults";
|
|
5
|
+
|
|
6
|
+
const tables = [defaultTable];
|
|
7
|
+
|
|
8
|
+
@Injectable()
|
|
9
|
+
export class MemoryDbService {
|
|
10
|
+
private db: { [key: string]: any[] } = {};
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
for (const table of tables) {
|
|
14
|
+
this.db[table] = [];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public insert(table: string, data: any): string {
|
|
19
|
+
// generate new random ID
|
|
20
|
+
const randomData = randomBytes(24).toString("hex");
|
|
21
|
+
const id = createHash("sha256").update(randomData).digest("hex");
|
|
22
|
+
|
|
23
|
+
this.db[table].push({
|
|
24
|
+
...data,
|
|
25
|
+
id, // unique index
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return id;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public update(table: string, condition: any, data: any): string {
|
|
32
|
+
// generate new random ID
|
|
33
|
+
const randomData = randomBytes(24).toString("hex");
|
|
34
|
+
const id = createHash("sha256").update(randomData).digest("hex");
|
|
35
|
+
|
|
36
|
+
this.db[table].push({
|
|
37
|
+
...data,
|
|
38
|
+
id, // unique index
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return id;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public getMany(table: string): any[] {
|
|
45
|
+
return this.db[table].map((log) => ({
|
|
46
|
+
type: log.type,
|
|
47
|
+
message: log.message,
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public getOneById(table: string, id: string): any {
|
|
52
|
+
return this.db[table].find((item) => item.id === id);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public getManyByProperty(table: string, field: string, value: string): any[] {
|
|
56
|
+
return this.db[table].filter((item) => item[field] === value);
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/logger.module.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { Module, DynamicModule, Global } from "@nestjs/common";
|
|
2
|
-
import { CustomLoggerService } from "./logger.service";
|
|
3
|
-
|
|
4
|
-
export interface LoggerModuleOptions {
|
|
5
|
-
prefix?: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
@Global()
|
|
9
|
-
@Module({})
|
|
10
|
-
export class CustomLoggerModule {
|
|
11
|
-
static forRoot(options: LoggerModuleOptions = {}): DynamicModule {
|
|
12
|
-
return {
|
|
13
|
-
module: CustomLoggerModule,
|
|
14
|
-
providers: [
|
|
15
|
-
{
|
|
16
|
-
provide: CustomLoggerService,
|
|
17
|
-
useFactory: () => {
|
|
18
|
-
return new (class extends CustomLoggerService {
|
|
19
|
-
override log(message: string, context?: string) {
|
|
20
|
-
super.log(`${options.prefix ?? ""}${message}`, context);
|
|
21
|
-
}
|
|
22
|
-
override error(
|
|
23
|
-
message: string,
|
|
24
|
-
trace?: string,
|
|
25
|
-
context?: string
|
|
26
|
-
) {
|
|
27
|
-
super.error(
|
|
28
|
-
`${options.prefix ?? ""}${message}`,
|
|
29
|
-
trace,
|
|
30
|
-
context
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
override warn(message: string, context?: string) {
|
|
34
|
-
super.warn(`${options.prefix ?? ""}${message}`, context);
|
|
35
|
-
}
|
|
36
|
-
})();
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
],
|
|
40
|
-
exports: [CustomLoggerService],
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
}
|
package/src/logger.service.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { Injectable, LoggerService, Scope } from "@nestjs/common";
|
|
2
|
-
|
|
3
|
-
@Injectable({ scope: Scope.TRANSIENT })
|
|
4
|
-
export class CustomLoggerService implements LoggerService {
|
|
5
|
-
log(message: string, context?: string) {
|
|
6
|
-
console.log(`[LOG]${context ? " [" + context + "]" : ""}: ${message}`);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
error(message: string, trace?: string, context?: string) {
|
|
10
|
-
console.error(`[ERROR]${context ? " [" + context + "]" : ""}: ${message}`);
|
|
11
|
-
if (trace) {
|
|
12
|
-
console.error(trace);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
warn(message: string, context?: string) {
|
|
17
|
-
console.warn(`[WARN]${context ? " [" + context + "]" : ""}: ${message}`);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
debug(message: string, context?: string) {
|
|
21
|
-
console.debug(`[DEBUG]${context ? " [" + context + "]" : ""}: ${message}`);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
verbose(message: string, context?: string) {
|
|
25
|
-
console.info(`[VERBOSE]${context ? " [" + context + "]" : ""}: ${message}`);
|
|
26
|
-
}
|
|
27
|
-
}
|