@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.
@@ -1,31 +1,35 @@
1
1
  # Logger Helper
2
2
 
3
- Powerful, flexible logging built on Winston - supports multiple transports, log levels, and hierarchical scopes.
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
- | **Scopes** | Hierarchical context tracking (e.g., `['MyService', 'MyMethod']`) |
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'); // Informational
18
- logger.error('message'); // Error
19
- logger.warn('message'); // Warning
20
- logger.debug('message'); // Debug
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
- The recommended way to get a logger instance is by using the `LoggerFactory`.
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
- ### Scopes
40
+ ### Using Logger.get() Directly
37
41
 
38
- You can provide an array of scopes to the `getLogger` method to create a hierarchical logger. This is useful for identifying the source of log messages.
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 logger = LoggerFactory.getLogger(['MyService', 'MyMethod']);
42
- logger.info('This message is from MyService-MyMethod');
54
+ const logger1 = Logger.get('MyService');
55
+ const logger2 = Logger.get('MyService');
56
+ // logger1 === logger2 (same instance)
43
57
  ```
44
58
 
45
- ## Configuration
59
+ ## Method-Scoped Logging with `.for()`
46
60
 
47
- The logger is configured through environment variables and the `defineCustomLogger` function, which sets up the `winston` instance.
61
+ The `.for()` method creates a sub-scoped logger for specific methods, making it easy to trace logs:
48
62
 
49
- ### Log Levels
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
- The logger supports the following log levels (from highest to lowest priority): `error`, `alert`, `emerg`, `warn`, `info`, `http`, `verbose`, `debug`, `silly`.
83
+ ## Log Formats
52
84
 
53
- ### Transports
85
+ The logger supports two output formats controlled by `APP_ENV_LOGGER_FORMAT`:
54
86
 
55
- `Ignis` comes with three main types of transports:
87
+ ### JSON Format
56
88
 
57
- 1. **Console:** Logs messages to the console. Enabled by default.
58
- 2. **DailyRotateFile:** Logs messages to files that are rotated on a daily or hourly basis.
59
- 3. **Dgram (UDP):** Sends log messages over UDP to a remote log aggregation service.
89
+ ```bash
90
+ APP_ENV_LOGGER_FORMAT=json
91
+ ```
60
92
 
61
- ### Environment Variables for Configuration
93
+ Output:
94
+ ```json
95
+ {"level":"info","message":"[UserService] User created","timestamp":"2024-01-11T10:30:00.000Z","label":"app"}
96
+ ```
62
97
 
63
- - `APP_ENV_LOGGER_FOLDER_PATH`: The directory to store log files.
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
- **Example `.env` file:**
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,debug
242
+ APP_ENV_LOGGER_DGRAM_LEVELS=error,warn,info
78
243
  ```
79
244
 
80
- ## Custom Logger
245
+ ## Best Practices
81
246
 
82
- While the default logger is powerful, you can create your own custom logger by extending the `Logger` class and providing a custom `winston.Logger` instance.
247
+ ### 1. Use Method-Scoped Logging
83
248
 
84
249
  ```typescript
85
- import { Logger, defineCustomLogger, applicationLogFormatter } from '@venizia/ignis';
86
- import winston from 'winston';
250
+ // Good - clear context
251
+ this.logger.for('createOrder').info('Processing order: %s', orderId);
87
252
 
88
- const myCustomWinstonLogger = defineCustomLogger({
89
- loggerFormatter: applicationLogFormatter,
90
- transports: {
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
- const myLogger = new Logger({ customLogger: myCustomWinstonLogger });
97
- myLogger.withScope('CustomScope').info('This is a custom log message.');
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