@tc-libs/logger 3.5.0 → 3.6.1
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 +119 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,124 @@
|
|
|
1
|
-
# logger
|
|
1
|
+
# @tc-libs/logger
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Wrapper NestJS su `winston` con supporto opzionale a notifiche Telegram.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Il package espone un `LoggerModule` configurabile e una `LogFactory` che crea logger contestualizzati. Ogni logger scrive sempre su console; se abilitato, inoltra anche messaggi a Telegram.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Registrazione del modulo
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
### Configurazione sincrona
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
```ts
|
|
12
|
+
import { LoggerModule } from '@tc-libs/logger';
|
|
13
|
+
|
|
14
|
+
LoggerModule.register(
|
|
15
|
+
{
|
|
16
|
+
telegram: {
|
|
17
|
+
enabled: true,
|
|
18
|
+
chatId: process.env.LOG_TELEGRAM_CHAT_ID!,
|
|
19
|
+
apiToken: process.env.LOG_TELEGRAM_BOT_TOKEN!,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
true,
|
|
23
|
+
);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Configurazione asincrona
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
LoggerModule.registerAsync(
|
|
30
|
+
{
|
|
31
|
+
imports: [ConfigModule],
|
|
32
|
+
inject: [ConfigService],
|
|
33
|
+
useFactory: async (configService: ConfigService) => ({
|
|
34
|
+
telegram: {
|
|
35
|
+
enabled: configService.get('LOG_TELEGRAM_ENABLED') === 'true',
|
|
36
|
+
chatId: configService.getOrThrow<string>('LOG_TELEGRAM_CHAT_ID'),
|
|
37
|
+
apiToken: configService.getOrThrow<string>('LOG_TELEGRAM_BOT_TOKEN'),
|
|
38
|
+
},
|
|
39
|
+
}),
|
|
40
|
+
},
|
|
41
|
+
true,
|
|
42
|
+
);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Come si usa
|
|
46
|
+
|
|
47
|
+
Inietta `LogFactory` e crea un logger con un contesto leggibile:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { Injectable } from '@nestjs/common';
|
|
51
|
+
import { LogFactory } from '@tc-libs/logger';
|
|
52
|
+
|
|
53
|
+
@Injectable()
|
|
54
|
+
export class UserService {
|
|
55
|
+
private readonly logger = this.logFactory.createLogger('UserService');
|
|
56
|
+
|
|
57
|
+
constructor(private readonly logFactory: LogFactory) {}
|
|
58
|
+
|
|
59
|
+
run() {
|
|
60
|
+
this.logger.log('Start processing');
|
|
61
|
+
this.logger.warn('Unexpected condition');
|
|
62
|
+
this.logger.error(new Error('Failure'));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## API del logger
|
|
68
|
+
|
|
69
|
+
Il logger creato implementa `ITcLogService`, che estende `LoggerService` Nest con metodi aggiuntivi:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
logger.log(message);
|
|
73
|
+
logger.error(message);
|
|
74
|
+
logger.warn(message);
|
|
75
|
+
logger.debug(message);
|
|
76
|
+
logger.verbose(message);
|
|
77
|
+
|
|
78
|
+
logger.notifyLog(message);
|
|
79
|
+
logger.notifyInfo(message);
|
|
80
|
+
logger.notifyWarn(message);
|
|
81
|
+
logger.notifyError(message);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Differenza pratica:
|
|
85
|
+
|
|
86
|
+
- `log`, `warn`, `error`, `debug`, `verbose`: scrivono su console
|
|
87
|
+
- `notify*`: scrivono su console e, se Telegram e abilitato, inviano anche la notifica al bot
|
|
88
|
+
|
|
89
|
+
## Cosa fa internamente
|
|
90
|
+
|
|
91
|
+
- usa `winston` per il logging console con timestamp, contesto e metadata
|
|
92
|
+
- serializza oggetti/errori in modo leggibile
|
|
93
|
+
- usa un transport custom `TelegramTransport` per inviare messaggi HTML tramite `telegraf`
|
|
94
|
+
|
|
95
|
+
## Quando usarlo
|
|
96
|
+
|
|
97
|
+
Usa questo package quando vuoi:
|
|
98
|
+
|
|
99
|
+
- avere un logger consistente tra piu package Nest
|
|
100
|
+
- creare logger per contesto senza configurare `winston` in ogni modulo
|
|
101
|
+
- ricevere alert operativi su Telegram per errori o eventi importanti
|
|
102
|
+
|
|
103
|
+
## Note operative
|
|
104
|
+
|
|
105
|
+
- Se `telegram.enabled` e `false`, il logger continua a funzionare normalmente su console.
|
|
106
|
+
- Anche con `enabled: true`, l'invio Telegram parte solo se `chatId` e valorizzato.
|
|
107
|
+
- `notifyError` e utile per errori operativi da segnalare fuori dai log locali.
|
|
108
|
+
|
|
109
|
+
## Export pubblici
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import {
|
|
113
|
+
ITcLogService,
|
|
114
|
+
LogFactory,
|
|
115
|
+
LoggerModule,
|
|
116
|
+
} from '@tc-libs/logger';
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Sviluppo
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
nx build logger
|
|
123
|
+
nx test logger
|
|
124
|
+
```
|