@viplance/nestjs-logger 0.1.4 → 0.1.6
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 +44 -26
- package/dist/entities/log.entity.js.map +1 -1
- package/dist/log.module.js +11 -0
- package/dist/log.module.js.map +1 -1
- package/dist/services/log.service.d.ts +4 -0
- package/dist/services/log.service.js +33 -1
- package/dist/services/log.service.js.map +1 -1
- package/dist/types/options.type.d.ts +4 -0
- package/package.json +1 -1
- package/src/entities/log.entity.ts +1 -1
- package/src/log.module.ts +10 -1
- package/src/services/log.service.ts +40 -1
- package/src/types/options.type.ts +4 -0
package/README.md
CHANGED
|
@@ -1,36 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# @viplance/nestjs-logger
|
|
2
|
+
## NestJS internal logging system
|
|
3
3
|
|
|
4
|
+
[](https://www.npmjs.com/package/@viplance/nestjs-logger)
|
|
5
|
+
[](https://github.com/viplance/nestjs-logger)
|
|
6
|
+
|
|
7
|
+
### Installation
|
|
4
8
|
1. Install the package npm i @viplance/nestjs-logger<br />
|
|
5
9
|
2. Import the module in app.module.ts<br />
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
```
|
|
11
|
+
import { LogModule } from '@viplance/nestjs-logger';
|
|
12
|
+
|
|
13
|
+
@Module({
|
|
14
|
+
imports: [
|
|
15
|
+
...,
|
|
16
|
+
LogModule,
|
|
17
|
+
]
|
|
18
|
+
})
|
|
19
|
+
```
|
|
13
20
|
<br />
|
|
14
21
|
3. Connect the module in main.ts
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
```
|
|
23
|
+
import { LogModule } from '@viplance/nestjs-logger';
|
|
24
|
+
|
|
25
|
+
await LogModule.connect(app, {
|
|
26
|
+
path: '/logs', // define the public URL for the log list
|
|
27
|
+
key: 'kjhj#$kj3lqq1', // use the key to protect data from unauthorized access
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
Use the database to store logs.
|
|
31
|
+
```
|
|
32
|
+
await LogModule.connect(app, {
|
|
33
|
+
path: '/logs',
|
|
34
|
+
database: {
|
|
35
|
+
type: 'mongodb',
|
|
36
|
+
host: 'localhost',
|
|
37
|
+
port: 27017,
|
|
38
|
+
collection: 'logs',
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
<br />
|
|
29
43
|
|
|
30
|
-
|
|
31
|
-
|
|
44
|
+
### Additional information
|
|
45
|
+
|
|
46
|
+
`path`, `key` and `database` options are optional
|
|
47
|
+
<br />
|
|
48
|
+
Use the LogService in case of custom logs to debug the application.
|
|
49
|
+
<br />
|
|
50
|
+
The logs could be available at `your_application_url`/`path`?key=`key>`
|
|
32
51
|
<br />
|
|
33
|
-
The logs could be available at your_application_url/<path><br />
|
|
34
52
|
By default the logs will be stored in memory and deleted when the application stops.<br />
|
|
35
53
|
<br />
|
|
36
54
|
Available service methods:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.entity.js","sourceRoot":"","sources":["../../src/entities/log.entity.ts"],"names":[],"mappings":";;AAEA,0CAkBC;AApBD,
|
|
1
|
+
{"version":3,"file":"log.entity.js","sourceRoot":"","sources":["../../src/entities/log.entity.ts"],"names":[],"mappings":";;AAEA,0CAkBC;AApBD,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;YACzB,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE;YACnC,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;YACzC,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;YACvC,SAAS,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;YACzB,SAAS,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;SAC1B;KACF,CAAC,CAAC;AACL,CAAC"}
|
package/dist/log.module.js
CHANGED
|
@@ -5,6 +5,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
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
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
8
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
12
|
exports.LogModule = void 0;
|
|
10
13
|
const common_1 = require("@nestjs/common");
|
|
@@ -12,6 +15,7 @@ const log_service_1 = require("./services/log.service");
|
|
|
12
15
|
const memory_db_service_1 = require("./services/memory-db.service");
|
|
13
16
|
const log_interceptor_1 = require("./log.interceptor");
|
|
14
17
|
const typeorm_1 = require("@nestjs/typeorm");
|
|
18
|
+
const node_querystring_1 = __importDefault(require("node:querystring"));
|
|
15
19
|
let LogModule = class LogModule {
|
|
16
20
|
static async connect(app, options) {
|
|
17
21
|
app.resolve(log_service_1.LogService);
|
|
@@ -20,6 +24,13 @@ let LogModule = class LogModule {
|
|
|
20
24
|
if (options === null || options === void 0 ? void 0 : options.path) {
|
|
21
25
|
const httpAdapter = app.getHttpAdapter();
|
|
22
26
|
httpAdapter.get(options.path, async (req, res) => {
|
|
27
|
+
var _a;
|
|
28
|
+
if ((_a = log_service_1.LogService.options) === null || _a === void 0 ? void 0 : _a.key) {
|
|
29
|
+
const params = node_querystring_1.default.parse(req.url.split("?")[1]);
|
|
30
|
+
if (params.key && params.key !== log_service_1.LogService.options.key) {
|
|
31
|
+
throw new common_1.HttpException("Unauthorized", 401);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
23
34
|
res.json(await logService.getAll());
|
|
24
35
|
});
|
|
25
36
|
}
|
package/dist/log.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.module.js","sourceRoot":"","sources":["../src/log.module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"log.module.js","sourceRoot":"","sources":["../src/log.module.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA+D;AAC/D,wDAAoD;AACpD,oEAA+D;AAC/D,uDAAmD;AAEnD,6CAAgD;AAChD,wEAA2C;AAQpC,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,IAAI,MAAA,wBAAU,CAAC,OAAO,0CAAE,GAAG,EAAE,CAAC;oBAC5B,MAAM,MAAM,GAAG,0BAAW,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;oBAExD,IAAI,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC,GAAG,KAAK,wBAAU,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC;wBACxD,MAAM,IAAI,sBAAa,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC;oBAC/C,CAAC;gBACH,CAAC;gBAED,GAAG,CAAC,IAAI,CAAC,MAAM,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC;YACtC,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;AA9BY,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,CA8BrB"}
|
|
@@ -3,10 +3,13 @@ import { MemoryDbService } from "./memory-db.service";
|
|
|
3
3
|
import { LogModuleOptions } from "../types";
|
|
4
4
|
import { DataSource, EntitySchema } from "typeorm";
|
|
5
5
|
import { ExecutionContextHost } from "@nestjs/core/helpers/execution-context-host";
|
|
6
|
+
import { setInterval } from "timers";
|
|
6
7
|
export declare class LogService implements LoggerService {
|
|
7
8
|
private readonly memoryDbService;
|
|
8
9
|
static connection: DataSource;
|
|
10
|
+
static options: LogModuleOptions;
|
|
9
11
|
static Log: EntitySchema;
|
|
12
|
+
static timer: ReturnType<typeof setInterval>;
|
|
10
13
|
constructor(memoryDbService: MemoryDbService);
|
|
11
14
|
connectDb(options: LogModuleOptions): Promise<DataSource>;
|
|
12
15
|
log(message: string, context?: ExecutionContextHost): void;
|
|
@@ -18,4 +21,5 @@ export declare class LogService implements LoggerService {
|
|
|
18
21
|
private smartInsert;
|
|
19
22
|
private getConnection;
|
|
20
23
|
private parseContext;
|
|
24
|
+
private checkRecords;
|
|
21
25
|
}
|
|
@@ -17,6 +17,7 @@ const defaults_1 = require("../defaults");
|
|
|
17
17
|
const types_1 = require("../types");
|
|
18
18
|
const typeorm_1 = require("typeorm");
|
|
19
19
|
const log_entity_1 = require("../entities/log.entity");
|
|
20
|
+
const timers_1 = require("timers");
|
|
20
21
|
let LogService = LogService_1 = class LogService {
|
|
21
22
|
constructor(memoryDbService) {
|
|
22
23
|
this.memoryDbService = memoryDbService;
|
|
@@ -24,6 +25,7 @@ let LogService = LogService_1 = class LogService {
|
|
|
24
25
|
async connectDb(options) {
|
|
25
26
|
var _a, _b, _c, _d, _e, _f;
|
|
26
27
|
LogService_1.Log = (0, log_entity_1.createLogEntity)(((_a = options.database) === null || _a === void 0 ? void 0 : _a.collection) || ((_b = options.database) === null || _b === void 0 ? void 0 : _b.table) || defaults_1.defaultTable);
|
|
28
|
+
LogService_1.options = options;
|
|
27
29
|
const dataSourceOptions = {
|
|
28
30
|
type: (_c = options.database) === null || _c === void 0 ? void 0 : _c.type,
|
|
29
31
|
database: (_d = options.database) === null || _d === void 0 ? void 0 : _d.database,
|
|
@@ -33,6 +35,10 @@ let LogService = LogService_1 = class LogService {
|
|
|
33
35
|
};
|
|
34
36
|
LogService_1.connection = new typeorm_1.DataSource(dataSourceOptions);
|
|
35
37
|
await LogService_1.connection.initialize();
|
|
38
|
+
if (LogService_1.timer) {
|
|
39
|
+
clearInterval(LogService_1.timer);
|
|
40
|
+
}
|
|
41
|
+
LogService_1.timer = (0, timers_1.setInterval)(this.checkRecords, 1000 * 60 * 60); // check one time per hour
|
|
36
42
|
return LogService_1.connection;
|
|
37
43
|
}
|
|
38
44
|
log(message, context) {
|
|
@@ -73,7 +79,15 @@ let LogService = LogService_1 = class LogService {
|
|
|
73
79
|
}
|
|
74
80
|
async getAll() {
|
|
75
81
|
return this.getConnection().find(LogService_1.Log, {
|
|
76
|
-
select: [
|
|
82
|
+
select: [
|
|
83
|
+
"type",
|
|
84
|
+
"message",
|
|
85
|
+
"count",
|
|
86
|
+
"createdAt",
|
|
87
|
+
"updatedAt",
|
|
88
|
+
"context",
|
|
89
|
+
"trace",
|
|
90
|
+
],
|
|
77
91
|
});
|
|
78
92
|
}
|
|
79
93
|
async smartInsert(data) {
|
|
@@ -130,6 +144,24 @@ let LogService = LogService_1 = class LogService {
|
|
|
130
144
|
}
|
|
131
145
|
return res;
|
|
132
146
|
}
|
|
147
|
+
async checkRecords() {
|
|
148
|
+
var _a, _b;
|
|
149
|
+
if ((_a = LogService_1.options) === null || _a === void 0 ? void 0 : _a.maxSize) {
|
|
150
|
+
const latest = await this.getConnection().find(LogService_1.Log, {
|
|
151
|
+
order: { updatedAt: "DESC" },
|
|
152
|
+
take: (_b = LogService_1.options) === null || _b === void 0 ? void 0 : _b.maxSize,
|
|
153
|
+
select: ["_id"],
|
|
154
|
+
});
|
|
155
|
+
const latestIds = latest.map((item) => item.id);
|
|
156
|
+
await LogService_1.connection
|
|
157
|
+
.getRepository(LogService_1.Log)
|
|
158
|
+
.createQueryBuilder()
|
|
159
|
+
.delete()
|
|
160
|
+
.from(LogService_1.Log)
|
|
161
|
+
.where("_id NOT IN (:...ids)", { ids: latestIds })
|
|
162
|
+
.execute();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
133
165
|
};
|
|
134
166
|
exports.LogService = LogService;
|
|
135
167
|
LogService.Log = (0, log_entity_1.createLogEntity)(defaults_1.defaultTable);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.service.js","sourceRoot":"","sources":["../../src/services/log.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;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E,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC;iBACjD,OAAO,EAAE,CAAC;QACf,CAAC;IACH,CAAC;;AAtLU,gCAAU;AAGd,cAAG,GAAiB,IAAA,4BAAe,EAAC,uBAAY,CAAC,AAA9C,CAA+C;qBAH9C,UAAU;IADtB,IAAA,mBAAU,EAAC,EAAE,KAAK,EAAE,cAAK,CAAC,SAAS,EAAE,CAAC;qCAOS,mCAAe;GANlD,UAAU,CAuLtB"}
|
package/package.json
CHANGED
package/src/log.module.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { Module, Global } from "@nestjs/common";
|
|
1
|
+
import { Module, Global, HttpException } from "@nestjs/common";
|
|
2
2
|
import { LogService } from "./services/log.service";
|
|
3
3
|
import { MemoryDbService } from "./services/memory-db.service";
|
|
4
4
|
import { LogInterceptor } from "./log.interceptor";
|
|
5
5
|
import { LogModuleOptions } from "./types";
|
|
6
6
|
import { TypeOrmModule } from "@nestjs/typeorm";
|
|
7
|
+
import querystring from "node:querystring";
|
|
7
8
|
|
|
8
9
|
@Global()
|
|
9
10
|
@Module({
|
|
@@ -25,6 +26,14 @@ export class LogModule {
|
|
|
25
26
|
if (options?.path) {
|
|
26
27
|
const httpAdapter = app.getHttpAdapter();
|
|
27
28
|
httpAdapter.get(options.path, async (req: any, res: any) => {
|
|
29
|
+
if (LogService.options?.key) {
|
|
30
|
+
const params = querystring.parse(req.url.split("?")[1]);
|
|
31
|
+
|
|
32
|
+
if (params.key && params.key !== LogService.options.key) {
|
|
33
|
+
throw new HttpException("Unauthorized", 401);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
28
37
|
res.json(await logService.getAll());
|
|
29
38
|
});
|
|
30
39
|
}
|
|
@@ -10,11 +10,14 @@ import {
|
|
|
10
10
|
} from "typeorm";
|
|
11
11
|
import { createLogEntity } from "../entities/log.entity";
|
|
12
12
|
import { ExecutionContextHost } from "@nestjs/core/helpers/execution-context-host";
|
|
13
|
+
import { setInterval } from "timers";
|
|
13
14
|
|
|
14
15
|
@Injectable({ scope: Scope.TRANSIENT })
|
|
15
16
|
export class LogService implements LoggerService {
|
|
16
17
|
static connection: DataSource;
|
|
18
|
+
static options: LogModuleOptions;
|
|
17
19
|
static Log: EntitySchema = createLogEntity(defaultTable);
|
|
20
|
+
static timer: ReturnType<typeof setInterval>;
|
|
18
21
|
|
|
19
22
|
constructor(private readonly memoryDbService: MemoryDbService) {}
|
|
20
23
|
|
|
@@ -23,6 +26,8 @@ export class LogService implements LoggerService {
|
|
|
23
26
|
options.database?.collection || options.database?.table || defaultTable
|
|
24
27
|
);
|
|
25
28
|
|
|
29
|
+
LogService.options = options;
|
|
30
|
+
|
|
26
31
|
const dataSourceOptions = {
|
|
27
32
|
type: options.database?.type,
|
|
28
33
|
database: options.database?.database,
|
|
@@ -35,6 +40,12 @@ export class LogService implements LoggerService {
|
|
|
35
40
|
|
|
36
41
|
await LogService.connection.initialize();
|
|
37
42
|
|
|
43
|
+
if (LogService.timer) {
|
|
44
|
+
clearInterval(LogService.timer);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
LogService.timer = setInterval(this.checkRecords, 1000 * 60 * 60); // check one time per hour
|
|
48
|
+
|
|
38
49
|
return LogService.connection;
|
|
39
50
|
}
|
|
40
51
|
|
|
@@ -81,7 +92,15 @@ export class LogService implements LoggerService {
|
|
|
81
92
|
|
|
82
93
|
async getAll(): Promise<any[]> {
|
|
83
94
|
return this.getConnection().find(LogService.Log, {
|
|
84
|
-
select: [
|
|
95
|
+
select: [
|
|
96
|
+
"type",
|
|
97
|
+
"message",
|
|
98
|
+
"count",
|
|
99
|
+
"createdAt",
|
|
100
|
+
"updatedAt",
|
|
101
|
+
"context",
|
|
102
|
+
"trace",
|
|
103
|
+
],
|
|
85
104
|
});
|
|
86
105
|
}
|
|
87
106
|
|
|
@@ -157,4 +176,24 @@ export class LogService implements LoggerService {
|
|
|
157
176
|
|
|
158
177
|
return res;
|
|
159
178
|
}
|
|
179
|
+
|
|
180
|
+
private async checkRecords() {
|
|
181
|
+
if (LogService.options?.maxSize) {
|
|
182
|
+
const latest = await this.getConnection().find(LogService.Log, {
|
|
183
|
+
order: { updatedAt: "DESC" },
|
|
184
|
+
take: LogService.options?.maxSize,
|
|
185
|
+
select: ["_id"],
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
const latestIds = latest.map((item) => item.id);
|
|
189
|
+
|
|
190
|
+
await LogService.connection
|
|
191
|
+
.getRepository(LogService.Log)
|
|
192
|
+
.createQueryBuilder()
|
|
193
|
+
.delete()
|
|
194
|
+
.from(LogService.Log)
|
|
195
|
+
.where("_id NOT IN (:...ids)", { ids: latestIds })
|
|
196
|
+
.execute();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
160
199
|
}
|
|
@@ -2,6 +2,10 @@ import { DataSourceOptions } from "typeorm";
|
|
|
2
2
|
|
|
3
3
|
export type LogModuleOptions = {
|
|
4
4
|
path?: string;
|
|
5
|
+
key?: string; // access key
|
|
6
|
+
maxRecords?: number; // max log records
|
|
7
|
+
maxAge?: number; // in days
|
|
8
|
+
maxSize?: number; // in megabytes
|
|
5
9
|
database?: DataSourceOptions & {
|
|
6
10
|
host?: string;
|
|
7
11
|
port?: string;
|