@venizia/ignis-docs 0.0.4 → 0.0.5
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/package.json +2 -2
- package/wiki/best-practices/code-style-standards/function-patterns.md +31 -5
- package/wiki/best-practices/performance-optimization.md +39 -0
- package/wiki/best-practices/troubleshooting-tips.md +24 -3
- package/wiki/guides/tutorials/realtime-chat.md +724 -460
- package/wiki/references/base/filter-system/json-filtering.md +4 -2
- package/wiki/references/base/middlewares.md +24 -72
- package/wiki/references/base/repositories/advanced.md +83 -0
- package/wiki/references/base/repositories/index.md +1 -1
- package/wiki/references/components/socket-io.md +495 -78
- package/wiki/references/helpers/logger.md +222 -43
- package/wiki/references/helpers/network.md +273 -31
- package/wiki/references/helpers/socket-io.md +881 -71
- package/wiki/references/quick-reference.md +3 -5
- package/wiki/references/src-details/core.md +1 -2
- package/wiki/references/utilities/statuses.md +4 -2
|
@@ -1,31 +1,35 @@
|
|
|
1
1
|
# Logger Helper
|
|
2
2
|
|
|
3
|
-
Powerful, flexible logging built on Winston - supports multiple transports, log levels, and
|
|
3
|
+
Powerful, flexible logging built on Winston - supports multiple transports, log levels, hierarchical scopes, and high-frequency logging for performance-critical applications.
|
|
4
4
|
|
|
5
5
|
## Quick Reference
|
|
6
6
|
|
|
7
7
|
| Feature | Description |
|
|
8
8
|
|---------|-------------|
|
|
9
9
|
| **Factory Method** | `LoggerFactory.getLogger(['scope1', 'scope2'])` |
|
|
10
|
+
| **Direct Access** | `Logger.get('scope')` |
|
|
11
|
+
| **Method Scoping** | `logger.for('methodName').info('message')` |
|
|
10
12
|
| **Log Levels** | `error`, `alert`, `emerg`, `warn`, `info`, `http`, `verbose`, `debug`, `silly` |
|
|
11
13
|
| **Transports** | Console (default), DailyRotateFile, UDP/Dgram |
|
|
12
|
-
| **
|
|
14
|
+
| **Formats** | JSON (`json`), Pretty Text (`text`) |
|
|
15
|
+
| **HF Logger** | Zero-allocation logging for HFT use cases |
|
|
13
16
|
|
|
14
17
|
### Common Methods
|
|
15
18
|
|
|
16
19
|
```typescript
|
|
17
|
-
logger.info('message');
|
|
18
|
-
logger.error('message');
|
|
19
|
-
logger.warn('message');
|
|
20
|
-
logger.debug('message');
|
|
20
|
+
logger.info('message'); // Informational
|
|
21
|
+
logger.error('message'); // Error
|
|
22
|
+
logger.warn('message'); // Warning
|
|
23
|
+
logger.debug('message'); // Debug (requires DEBUG=true)
|
|
24
|
+
logger.for('methodName').info('message'); // Method-scoped logging
|
|
21
25
|
```
|
|
22
26
|
|
|
23
27
|
## Getting a Logger Instance
|
|
24
28
|
|
|
25
|
-
|
|
29
|
+
### Using LoggerFactory (Recommended)
|
|
26
30
|
|
|
27
31
|
```typescript
|
|
28
|
-
import { LoggerFactory } from '@venizia/ignis';
|
|
32
|
+
import { LoggerFactory } from '@venizia/ignis-helpers';
|
|
29
33
|
|
|
30
34
|
const logger = LoggerFactory.getLogger(['MyService']);
|
|
31
35
|
|
|
@@ -33,70 +37,243 @@ logger.info('This is an informational message.');
|
|
|
33
37
|
logger.error('This is an error message.');
|
|
34
38
|
```
|
|
35
39
|
|
|
36
|
-
###
|
|
40
|
+
### Using Logger.get() Directly
|
|
37
41
|
|
|
38
|
-
|
|
42
|
+
```typescript
|
|
43
|
+
import { Logger } from '@venizia/ignis-helpers';
|
|
44
|
+
|
|
45
|
+
const logger = Logger.get('MyService');
|
|
46
|
+
logger.info('Direct logger access');
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Logger Caching
|
|
50
|
+
|
|
51
|
+
Both methods use internal caching - the same scope always returns the same logger instance:
|
|
39
52
|
|
|
40
53
|
```typescript
|
|
41
|
-
const
|
|
42
|
-
|
|
54
|
+
const logger1 = Logger.get('MyService');
|
|
55
|
+
const logger2 = Logger.get('MyService');
|
|
56
|
+
// logger1 === logger2 (same instance)
|
|
43
57
|
```
|
|
44
58
|
|
|
45
|
-
##
|
|
59
|
+
## Method-Scoped Logging with `.for()`
|
|
46
60
|
|
|
47
|
-
The
|
|
61
|
+
The `.for()` method creates a sub-scoped logger for specific methods, making it easy to trace logs:
|
|
48
62
|
|
|
49
|
-
|
|
63
|
+
```typescript
|
|
64
|
+
class UserService {
|
|
65
|
+
private logger = LoggerFactory.getLogger(['UserService']);
|
|
66
|
+
|
|
67
|
+
async createUser(data: CreateUserDto) {
|
|
68
|
+
this.logger.for('createUser').info('Creating user: %j', data);
|
|
69
|
+
// Output: [UserService-createUser] Creating user: {...}
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const user = await this.userRepo.create({ data });
|
|
73
|
+
this.logger.for('createUser').info('User created: %s', user.id);
|
|
74
|
+
return user;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
this.logger.for('createUser').error('Failed to create user: %s', error);
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
50
82
|
|
|
51
|
-
|
|
83
|
+
## Log Formats
|
|
52
84
|
|
|
53
|
-
|
|
85
|
+
The logger supports two output formats controlled by `APP_ENV_LOGGER_FORMAT`:
|
|
54
86
|
|
|
55
|
-
|
|
87
|
+
### JSON Format
|
|
56
88
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
89
|
+
```bash
|
|
90
|
+
APP_ENV_LOGGER_FORMAT=json
|
|
91
|
+
```
|
|
60
92
|
|
|
61
|
-
|
|
93
|
+
Output:
|
|
94
|
+
```json
|
|
95
|
+
{"level":"info","message":"[UserService] User created","timestamp":"2024-01-11T10:30:00.000Z","label":"app"}
|
|
96
|
+
```
|
|
62
97
|
|
|
63
|
-
|
|
64
|
-
- `APP_ENV_APPLICATION_NAME`: Used as a prefix for log files and as a label in log messages.
|
|
65
|
-
- `APP_ENV_LOGGER_DGRAM_HOST`: The host for the UDP log transport.
|
|
66
|
-
- `APP_ENV_LOGGER_DGRAM_PORT`: The port for the UDP log transport.
|
|
67
|
-
- `APP_ENV_LOGGER_DGRAM_LABEL`: A label to identify the source of UDP logs.
|
|
68
|
-
- `APP_ENV_LOGGER_DGRAM_LEVELS`: A comma-separated list of log levels to be sent via UDP (e.g., `error,warn,info`).
|
|
98
|
+
### Pretty Text Format (Default)
|
|
69
99
|
|
|
70
|
-
|
|
100
|
+
```bash
|
|
101
|
+
APP_ENV_LOGGER_FORMAT=text
|
|
102
|
+
```
|
|
71
103
|
|
|
104
|
+
Output:
|
|
72
105
|
```
|
|
106
|
+
2024-01-11T10:30:00.000Z [app] info: [UserService] User created
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## File Rotation Configuration
|
|
110
|
+
|
|
111
|
+
Configure file rotation through environment variables or programmatically:
|
|
112
|
+
|
|
113
|
+
### Environment Variables
|
|
114
|
+
|
|
115
|
+
| Variable | Default | Description |
|
|
116
|
+
|----------|---------|-------------|
|
|
117
|
+
| `APP_ENV_LOGGER_FILE_FREQUENCY` | `1h` | Rotation frequency |
|
|
118
|
+
| `APP_ENV_LOGGER_FILE_MAX_SIZE` | `100m` | Max file size before rotation |
|
|
119
|
+
| `APP_ENV_LOGGER_FILE_MAX_FILES` | `5d` | Retention period (days) |
|
|
120
|
+
| `APP_ENV_LOGGER_FILE_DATE_PATTERN` | `YYYYMMDD_HH` | Date pattern in filename |
|
|
121
|
+
| `APP_ENV_LOGGER_FOLDER_PATH` | `./` | Log files directory |
|
|
122
|
+
|
|
123
|
+
### Programmatic Configuration
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { defineCustomLogger, applicationLogFormatter } from '@venizia/ignis-helpers';
|
|
127
|
+
|
|
128
|
+
const customLogger = defineCustomLogger({
|
|
129
|
+
loggerFormatter: applicationLogFormatter,
|
|
130
|
+
transports: {
|
|
131
|
+
info: {
|
|
132
|
+
file: {
|
|
133
|
+
prefix: 'my-app',
|
|
134
|
+
folder: './logs',
|
|
135
|
+
frequency: '24h', // Rotate daily
|
|
136
|
+
maxSize: '500m', // 500MB max
|
|
137
|
+
maxFiles: '30d', // Keep 30 days
|
|
138
|
+
datePattern: 'YYYYMMDD' // Daily pattern
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
error: {
|
|
142
|
+
file: {
|
|
143
|
+
prefix: 'my-app-error',
|
|
144
|
+
folder: './logs',
|
|
145
|
+
maxFiles: '90d' // Keep error logs longer
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## High-Frequency Logger (HfLogger)
|
|
153
|
+
|
|
154
|
+
For performance-critical applications like HFT systems, use `HfLogger` which provides:
|
|
155
|
+
|
|
156
|
+
- Zero allocation in hot path
|
|
157
|
+
- Lock-free ring buffer (64K entries)
|
|
158
|
+
- Sub-microsecond latency (~100-300ns)
|
|
159
|
+
- Pre-encoded messages
|
|
160
|
+
- Async background flushing
|
|
161
|
+
|
|
162
|
+
### Basic Usage
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import { HfLogger, HfLogFlusher } from '@venizia/ignis-helpers';
|
|
166
|
+
|
|
167
|
+
// At initialization time (once):
|
|
168
|
+
const logger = HfLogger.get('OrderEngine');
|
|
169
|
+
const MSG_ORDER_SENT = HfLogger.encodeMessage('Order sent');
|
|
170
|
+
const MSG_ORDER_FILLED = HfLogger.encodeMessage('Order filled');
|
|
171
|
+
|
|
172
|
+
// Start background flusher
|
|
173
|
+
const flusher = new HfLogFlusher();
|
|
174
|
+
flusher.start(100); // Flush every 100ms
|
|
175
|
+
|
|
176
|
+
// In hot path (~100-300ns, zero allocation):
|
|
177
|
+
logger.log('info', MSG_ORDER_SENT);
|
|
178
|
+
logger.log('info', MSG_ORDER_FILLED);
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### HfLogger API
|
|
182
|
+
|
|
183
|
+
| Method | Description |
|
|
184
|
+
|--------|-------------|
|
|
185
|
+
| `HfLogger.get(scope)` | Get/create a cached logger instance |
|
|
186
|
+
| `HfLogger.encodeMessage(msg)` | Pre-encode a message (cached) |
|
|
187
|
+
| `logger.log(level, msgBytes)` | Log to ring buffer (zero allocation) |
|
|
188
|
+
|
|
189
|
+
### HfLogFlusher API
|
|
190
|
+
|
|
191
|
+
| Method | Description |
|
|
192
|
+
|--------|-------------|
|
|
193
|
+
| `flusher.flush()` | Flush buffered entries to output |
|
|
194
|
+
| `flusher.start(intervalMs)` | Start background flush loop |
|
|
195
|
+
|
|
196
|
+
### Performance Characteristics
|
|
197
|
+
|
|
198
|
+
| Metric | Value |
|
|
199
|
+
|--------|-------|
|
|
200
|
+
| Log latency | ~100-300 nanoseconds |
|
|
201
|
+
| Buffer size | 64K entries (16MB) |
|
|
202
|
+
| Entry size | 256 bytes fixed |
|
|
203
|
+
| Allocation | Zero in hot path |
|
|
204
|
+
|
|
205
|
+
## Environment Variables
|
|
206
|
+
|
|
207
|
+
### Core Configuration
|
|
208
|
+
|
|
209
|
+
| Variable | Default | Description |
|
|
210
|
+
|----------|---------|-------------|
|
|
211
|
+
| `DEBUG` | `false` | Enable debug logging |
|
|
212
|
+
| `APP_ENV_EXTRA_LOG_ENVS` | `` | Additional environments to enable debug |
|
|
213
|
+
| `APP_ENV_LOGGER_FORMAT` | `text` | Output format (`json` or `text`) |
|
|
214
|
+
| `APP_ENV_LOGGER_FOLDER_PATH` | `./` | Log files directory |
|
|
215
|
+
|
|
216
|
+
### UDP Transport
|
|
217
|
+
|
|
218
|
+
| Variable | Description |
|
|
219
|
+
|----------|-------------|
|
|
220
|
+
| `APP_ENV_LOGGER_DGRAM_HOST` | UDP log aggregator host |
|
|
221
|
+
| `APP_ENV_LOGGER_DGRAM_PORT` | UDP log aggregator port |
|
|
222
|
+
| `APP_ENV_LOGGER_DGRAM_LABEL` | Label to identify log source |
|
|
223
|
+
| `APP_ENV_LOGGER_DGRAM_LEVELS` | Comma-separated levels to send via UDP |
|
|
224
|
+
|
|
225
|
+
### Example `.env`
|
|
226
|
+
|
|
227
|
+
```bash
|
|
228
|
+
# Core
|
|
229
|
+
DEBUG=true
|
|
230
|
+
APP_ENV_LOGGER_FORMAT=json
|
|
73
231
|
APP_ENV_LOGGER_FOLDER_PATH=./app_data/logs
|
|
232
|
+
|
|
233
|
+
# File rotation
|
|
234
|
+
APP_ENV_LOGGER_FILE_FREQUENCY=24h
|
|
235
|
+
APP_ENV_LOGGER_FILE_MAX_SIZE=500m
|
|
236
|
+
APP_ENV_LOGGER_FILE_MAX_FILES=30d
|
|
237
|
+
|
|
238
|
+
# UDP transport
|
|
74
239
|
APP_ENV_LOGGER_DGRAM_HOST=127.0.0.1
|
|
75
240
|
APP_ENV_LOGGER_DGRAM_PORT=5000
|
|
76
241
|
APP_ENV_LOGGER_DGRAM_LABEL=my-app
|
|
77
|
-
APP_ENV_LOGGER_DGRAM_LEVELS=error,warn,info
|
|
242
|
+
APP_ENV_LOGGER_DGRAM_LEVELS=error,warn,info
|
|
78
243
|
```
|
|
79
244
|
|
|
80
|
-
##
|
|
245
|
+
## Best Practices
|
|
81
246
|
|
|
82
|
-
|
|
247
|
+
### 1. Use Method-Scoped Logging
|
|
83
248
|
|
|
84
249
|
```typescript
|
|
85
|
-
|
|
86
|
-
|
|
250
|
+
// Good - clear context
|
|
251
|
+
this.logger.for('createOrder').info('Processing order: %s', orderId);
|
|
87
252
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
info: { file: { prefix: 'my-app', folder: './logs' } },
|
|
92
|
-
error: { file: { prefix: 'my-app-error', folder: './logs' } },
|
|
93
|
-
},
|
|
94
|
-
});
|
|
253
|
+
// Less clear
|
|
254
|
+
this.logger.info('[createOrder] Processing order: %s', orderId);
|
|
255
|
+
```
|
|
95
256
|
|
|
96
|
-
|
|
97
|
-
|
|
257
|
+
### 2. Pre-encode HfLogger Messages
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
// Good - pre-encoded at init
|
|
261
|
+
const MSG_TICK = HfLogger.encodeMessage('Tick received');
|
|
262
|
+
logger.log('debug', MSG_TICK);
|
|
263
|
+
|
|
264
|
+
// Bad - encodes on every call
|
|
265
|
+
logger.log('debug', HfLogger.encodeMessage('Tick received'));
|
|
98
266
|
```
|
|
99
267
|
|
|
268
|
+
### 3. Use Appropriate Logger for Use Case
|
|
269
|
+
|
|
270
|
+
| Use Case | Logger |
|
|
271
|
+
|----------|--------|
|
|
272
|
+
| General application | `Logger` / `LoggerFactory` |
|
|
273
|
+
| High-frequency trading | `HfLogger` |
|
|
274
|
+
| Performance-critical paths | `HfLogger` |
|
|
275
|
+
| Debug/development | `Logger` with `DEBUG=true` |
|
|
276
|
+
|
|
100
277
|
## See Also
|
|
101
278
|
|
|
102
279
|
- **Related Concepts:**
|
|
@@ -109,9 +286,11 @@ myLogger.withScope('CustomScope').info('This is a custom log message.');
|
|
|
109
286
|
|
|
110
287
|
- **References:**
|
|
111
288
|
- [Request Tracker Component](/references/components/request-tracker) - Request logging
|
|
289
|
+
- [Environment Variables](/references/configuration/environment-variables) - All configuration options
|
|
112
290
|
|
|
113
291
|
- **External Resources:**
|
|
114
292
|
- [Winston Documentation](https://github.com/winstonjs/winston) - Winston logging library
|
|
115
293
|
|
|
116
294
|
- **Best Practices:**
|
|
117
295
|
- [Troubleshooting Tips](/best-practices/troubleshooting-tips) - Using logs for debugging
|
|
296
|
+
- [Performance Optimization](/best-practices/performance-optimization) - High-performance logging
|