nestjs-log-decorator 0.1.0 → 0.2.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 +15 -13
- package/dist/{index.mjs → index.cjs} +4 -2
- package/package.json +4 -6
- /package/dist/{index.d.mts → index.d.cts} +0 -0
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ TypeScript decorators that eliminate logging boilerplate from NestJS application
|
|
|
25
25
|
|
|
26
26
|
## Description
|
|
27
27
|
|
|
28
|
-
`@Log()` decorator replaces try-catch logging pattern
|
|
28
|
+
`@Log()` decorator replaces the try-catch logging pattern in NestJS service methods by automatically logging method invocation on return or error.
|
|
29
29
|
|
|
30
30
|
**Key Features**
|
|
31
31
|
|
|
@@ -47,7 +47,7 @@ import { Logger } from '@nestjs/common';
|
|
|
47
47
|
import { Log } from 'nestjs-log-decorator';
|
|
48
48
|
|
|
49
49
|
class UserService {
|
|
50
|
-
|
|
50
|
+
readonly logger = new Logger(UserService.name);
|
|
51
51
|
|
|
52
52
|
@Log()
|
|
53
53
|
createUser(name: string, email: string) {
|
|
@@ -57,7 +57,7 @@ class UserService {
|
|
|
57
57
|
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
-
Once a service method is called, it will log the method
|
|
60
|
+
Once a service method is called, it will log the method invocation with all arguments.
|
|
61
61
|
|
|
62
62
|
```typescript
|
|
63
63
|
const result = service.createUser('John', 'john@example.com');
|
|
@@ -106,7 +106,7 @@ const result = await resultPromise;
|
|
|
106
106
|
|
|
107
107
|
### Complete Example
|
|
108
108
|
|
|
109
|
-
After installation, no additional configuration is needed, just make sure that your class has a `logger` property
|
|
109
|
+
After installation, no additional configuration is needed, just make sure that your class has a public `logger` property that implements the default NestJS Logger interface.
|
|
110
110
|
|
|
111
111
|
```typescript
|
|
112
112
|
import { Logger } from '@nestjs/common';
|
|
@@ -115,7 +115,7 @@ import { Log } from 'nestjs-log-decorator';
|
|
|
115
115
|
|
|
116
116
|
class PaymentService {
|
|
117
117
|
// `logger` property will be used by decorator
|
|
118
|
-
|
|
118
|
+
readonly logger = new Logger(PaymentService.name);
|
|
119
119
|
|
|
120
120
|
@Log()
|
|
121
121
|
async processPayment(amount: number, currency: string) {
|
|
@@ -130,6 +130,8 @@ class PaymentService {
|
|
|
130
130
|
}
|
|
131
131
|
```
|
|
132
132
|
|
|
133
|
+
The `logger` property is public due to TypeScript validation limitations. TS cannot check if a private property is valid, but it should still work even if the property is private.
|
|
134
|
+
|
|
133
135
|
## How It Works
|
|
134
136
|
|
|
135
137
|
The `@Log()` decorator wraps your methods with automatic try-catch logging. It extracts parameter names, captures arguments, and logs structured output on success or error.
|
|
@@ -181,7 +183,7 @@ Apply `@Log()` to specific methods for granular control:
|
|
|
181
183
|
|
|
182
184
|
```typescript
|
|
183
185
|
class DataService {
|
|
184
|
-
|
|
186
|
+
readonly logger = new Logger(DataService.name);
|
|
185
187
|
|
|
186
188
|
@Log()
|
|
187
189
|
async fetchData(id: number) {
|
|
@@ -207,7 +209,7 @@ import { Log } from 'nestjs-log-decorator';
|
|
|
207
209
|
@Log()
|
|
208
210
|
@Injectable()
|
|
209
211
|
class PaymentService {
|
|
210
|
-
|
|
212
|
+
readonly logger = new Logger(PaymentService.name);
|
|
211
213
|
|
|
212
214
|
processPayment(amount: number, currency: string) {
|
|
213
215
|
// Automatically logged on success or error
|
|
@@ -230,7 +232,7 @@ import { Log, NoLog } from 'nestjs-log-decorator';
|
|
|
230
232
|
|
|
231
233
|
@Log()
|
|
232
234
|
class UserService {
|
|
233
|
-
|
|
235
|
+
readonly logger = new Logger(UserService.name);
|
|
234
236
|
|
|
235
237
|
createUser(name: string) {
|
|
236
238
|
// Logged
|
|
@@ -266,7 +268,7 @@ Class-level with `onInvoke`:
|
|
|
266
268
|
```typescript
|
|
267
269
|
@Log({ onInvoke: true })
|
|
268
270
|
class ApiService {
|
|
269
|
-
|
|
271
|
+
readonly logger = new Logger(ApiService.name);
|
|
270
272
|
|
|
271
273
|
// All methods will log invocation + completion
|
|
272
274
|
}
|
|
@@ -287,7 +289,7 @@ interface LargePayload {
|
|
|
287
289
|
}
|
|
288
290
|
|
|
289
291
|
class SyncService {
|
|
290
|
-
|
|
292
|
+
readonly logger = new Logger(SyncService.name);
|
|
291
293
|
|
|
292
294
|
// Only log the ID, exclude the large payload
|
|
293
295
|
@Log({ args: (id: number, _payload: LargePayload) => ({ id }) })
|
|
@@ -469,11 +471,11 @@ import { Log, NoLog } from 'nestjs-log-decorator';
|
|
|
469
471
|
@Log()
|
|
470
472
|
@Injectable()
|
|
471
473
|
export class OrderService {
|
|
472
|
-
|
|
474
|
+
readonly logger = new Logger(OrderService.name);
|
|
473
475
|
|
|
474
476
|
constructor(
|
|
475
|
-
|
|
476
|
-
|
|
477
|
+
readonly orderRepo: OrderRepository,
|
|
478
|
+
readonly paymentGateway: PaymentGateway,
|
|
477
479
|
) {}
|
|
478
480
|
|
|
479
481
|
// Logged with all args
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
require("@nestjs/common");
|
|
2
2
|
|
|
3
3
|
//#region src/axios/axios.stub.ts
|
|
4
4
|
/**
|
|
@@ -427,4 +427,6 @@ const NoLog = () => {
|
|
|
427
427
|
};
|
|
428
428
|
|
|
429
429
|
//#endregion
|
|
430
|
-
|
|
430
|
+
exports.Log = Log;
|
|
431
|
+
exports.NoLog = NoLog;
|
|
432
|
+
exports.isLoggable = isLoggable;
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nestjs-log-decorator",
|
|
3
|
-
"
|
|
4
|
-
"version": "0.1.0",
|
|
3
|
+
"version": "0.2.0",
|
|
5
4
|
"description": "Decorator that removes try catch boilerplate logging from NestJS service methods",
|
|
6
5
|
"author": "Vlad Goncharov <leovs010@gmail.com>",
|
|
7
6
|
"license": "AGPL-3.0",
|
|
@@ -14,12 +13,11 @@
|
|
|
14
13
|
"url": "https://github.com/neolabhq/nestjs-log-decorator/issues"
|
|
15
14
|
},
|
|
16
15
|
"exports": {
|
|
17
|
-
".": "./dist/index.
|
|
16
|
+
".": "./dist/index.cjs",
|
|
18
17
|
"./package.json": "./package.json"
|
|
19
18
|
},
|
|
20
|
-
"main": "./dist/index.
|
|
21
|
-
"
|
|
22
|
-
"types": "./dist/index.d.mts",
|
|
19
|
+
"main": "./dist/index.cjs",
|
|
20
|
+
"types": "./dist/index.d.cts",
|
|
23
21
|
"files": [
|
|
24
22
|
"dist"
|
|
25
23
|
],
|
|
File without changes
|