@octaviaflow/logger 1.0.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 +306 -0
- package/dist/index.js +982 -0
- package/dist/index.js.map +18 -0
- package/dist/presets/index.js +1163 -0
- package/dist/presets/index.js.map +21 -0
- package/package.json +73 -0
- package/types/logger.d.ts +494 -0
package/README.md
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
# @octaviaflow/logger
|
|
2
|
+
|
|
3
|
+
Production-ready, high-performance logger for Octaviaflow microservices. Built for Bun runtime with zero dependencies and optimized for structured logging, file rotation, and multi-service support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ๐ **High Performance**: <1ms per log, handles 10,000+ logs/second
|
|
8
|
+
- ๐ฆ **Zero Dependencies**: Uses Bun's native APIs
|
|
9
|
+
- ๐ฏ **Multi-Service Support**: Presets for backend, engine, and UI services
|
|
10
|
+
- ๐ **Structured Logging**: JSON format with rich metadata
|
|
11
|
+
- ๐ **Log Rotation**: Size-based and time-based with compression
|
|
12
|
+
- ๐จ **Multiple Formats**: JSON, Pretty, Compact, and ECS (Elastic Common Schema)
|
|
13
|
+
- ๐ **Security**: Built-in sanitization for sensitive data
|
|
14
|
+
- ๐ **Context Management**: AsyncLocalStorage for request/workflow context
|
|
15
|
+
- ๐ญ **Specialized Logging**: Audit, security, performance, and metrics
|
|
16
|
+
- ๐งช **Fully Typed**: Complete TypeScript support
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
bun add @octaviaflow/logger
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
### Backend Service
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { createBackendLogger } from '@octaviaflow/logger/presets';
|
|
30
|
+
|
|
31
|
+
const logger = createBackendLogger({
|
|
32
|
+
level: 'info',
|
|
33
|
+
version: '1.0.0',
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
logger.info('Server started', { port: 3000 });
|
|
37
|
+
|
|
38
|
+
// Request logging
|
|
39
|
+
logger.startRequest({
|
|
40
|
+
requestId: 'req_123',
|
|
41
|
+
method: 'GET',
|
|
42
|
+
path: '/api/workflows',
|
|
43
|
+
ip: '127.0.0.1',
|
|
44
|
+
protocol: 'http',
|
|
45
|
+
hostname: 'localhost',
|
|
46
|
+
url: 'http://localhost:3000/api/workflows',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// ... handle request ...
|
|
50
|
+
|
|
51
|
+
logger.endRequest(200, 145);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Engine Service
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { createEngineLogger } from '@octaviaflow/logger/presets';
|
|
58
|
+
|
|
59
|
+
const logger = createEngineLogger({
|
|
60
|
+
level: 'info',
|
|
61
|
+
version: '1.0.0',
|
|
62
|
+
workerId: 'worker_1',
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Workflow logging
|
|
66
|
+
logger.startWorkflow({
|
|
67
|
+
workflowId: 'wf_123',
|
|
68
|
+
workflowName: 'Data Sync',
|
|
69
|
+
executionId: 'exec_456',
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
logger.info('Processing workflow step');
|
|
73
|
+
|
|
74
|
+
logger.endWorkflow(true);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### UI Service
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { createUILogger } from '@octaviaflow/logger/presets';
|
|
81
|
+
|
|
82
|
+
const logger = createUILogger({
|
|
83
|
+
level: 'warn',
|
|
84
|
+
version: '1.0.0',
|
|
85
|
+
enableRemote: true,
|
|
86
|
+
remoteUrl: 'https://api.example.com/logs',
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
logger.info('User action', {
|
|
90
|
+
action: 'button_click',
|
|
91
|
+
component: 'WorkflowBuilder',
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## API Reference
|
|
96
|
+
|
|
97
|
+
### Basic Logging
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
logger.fatal('Fatal error');
|
|
101
|
+
logger.error('Error occurred');
|
|
102
|
+
logger.warn('Warning message');
|
|
103
|
+
logger.info('Info message');
|
|
104
|
+
logger.debug('Debug information');
|
|
105
|
+
logger.trace('Trace details');
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Specialized Logging
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
// Audit logging
|
|
112
|
+
logger.audit('user.login', {
|
|
113
|
+
actor: { id: 'user_123', type: 'user' },
|
|
114
|
+
resource: { type: 'session', id: 'session_456' },
|
|
115
|
+
result: 'success',
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Security logging
|
|
119
|
+
logger.security('auth_failure', {
|
|
120
|
+
event: 'auth_failure',
|
|
121
|
+
severity: 'high',
|
|
122
|
+
action: 'blocked',
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// Performance logging
|
|
126
|
+
logger.performance('api.request', 145, {
|
|
127
|
+
metrics: { dbQueries: 3, cacheHits: 5 },
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Metrics
|
|
131
|
+
logger.metric('api.requests.total', 100);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Context Management
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
// Set context for current async scope
|
|
138
|
+
logger.setContext({
|
|
139
|
+
user: { userId: 'user_123', organizationId: 'org_456' },
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Create child logger with additional context
|
|
143
|
+
const apiLogger = logger.child({
|
|
144
|
+
component: 'api',
|
|
145
|
+
endpoint: '/api/workflows',
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Clear context
|
|
149
|
+
logger.clearContext();
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Utilities
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
// Profile operations
|
|
156
|
+
logger.profile('operation_1');
|
|
157
|
+
// ... do work ...
|
|
158
|
+
logger.profile('operation_1'); // Logs duration
|
|
159
|
+
|
|
160
|
+
// Start timer
|
|
161
|
+
const getElapsed = logger.startTimer();
|
|
162
|
+
// ... do work ...
|
|
163
|
+
const duration = getElapsed();
|
|
164
|
+
logger.info('Operation completed', { duration });
|
|
165
|
+
|
|
166
|
+
// Flush and close
|
|
167
|
+
await logger.flush();
|
|
168
|
+
await logger.close();
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Configuration
|
|
172
|
+
|
|
173
|
+
### Custom Logger
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
import { Logger } from '@octaviaflow/logger';
|
|
177
|
+
|
|
178
|
+
const logger = new Logger({
|
|
179
|
+
service: 'backend',
|
|
180
|
+
environment: 'production',
|
|
181
|
+
version: '1.0.0',
|
|
182
|
+
level: 'info',
|
|
183
|
+
format: 'json',
|
|
184
|
+
transports: [
|
|
185
|
+
{
|
|
186
|
+
type: 'console',
|
|
187
|
+
enabled: true,
|
|
188
|
+
level: 'info',
|
|
189
|
+
format: 'pretty',
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
type: 'file',
|
|
193
|
+
enabled: true,
|
|
194
|
+
level: 'info',
|
|
195
|
+
file: {
|
|
196
|
+
path: './logs/app.log',
|
|
197
|
+
maxSize: '10m',
|
|
198
|
+
maxFiles: 10,
|
|
199
|
+
compress: true,
|
|
200
|
+
datePattern: 'YYYY-MM-DD',
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
type: 'http',
|
|
205
|
+
enabled: true,
|
|
206
|
+
level: 'error',
|
|
207
|
+
http: {
|
|
208
|
+
url: 'https://api.example.com/logs',
|
|
209
|
+
batchSize: 10,
|
|
210
|
+
flushInterval: 5000,
|
|
211
|
+
retry: { attempts: 3, delay: 1000 },
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
],
|
|
215
|
+
sanitize: {
|
|
216
|
+
enabled: true,
|
|
217
|
+
fields: ['password', 'token', 'apiKey'],
|
|
218
|
+
},
|
|
219
|
+
handleExceptions: true,
|
|
220
|
+
handleRejections: true,
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Log Formats
|
|
225
|
+
|
|
226
|
+
### JSON (Default for Production)
|
|
227
|
+
```json
|
|
228
|
+
{
|
|
229
|
+
"timestamp": "2024-11-21T10:30:45.123Z",
|
|
230
|
+
"level": "info",
|
|
231
|
+
"service": "backend",
|
|
232
|
+
"message": "API request completed",
|
|
233
|
+
"requestId": "req_abc123",
|
|
234
|
+
"statusCode": 200,
|
|
235
|
+
"duration": 145
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Pretty (Default for Development)
|
|
240
|
+
```
|
|
241
|
+
2024-11-21T10:30:45.123Z INFO [backend] API request completed {"requestId":"req_abc123","statusCode":200}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Compact
|
|
245
|
+
```
|
|
246
|
+
10:30:45 I [backend] API request completed
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### ECS (Elastic Common Schema)
|
|
250
|
+
```json
|
|
251
|
+
{
|
|
252
|
+
"@timestamp": "2024-11-21T10:30:45.123Z",
|
|
253
|
+
"log.level": "info",
|
|
254
|
+
"message": "API request completed",
|
|
255
|
+
"service": { "name": "backend", "version": "1.0.0" },
|
|
256
|
+
"host": { "hostname": "api-server-1" }
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## File Rotation
|
|
261
|
+
|
|
262
|
+
Logs are automatically rotated based on:
|
|
263
|
+
- **Size**: When file exceeds `maxSize` (default: 10MB)
|
|
264
|
+
- **Date**: Daily rotation with `datePattern` (default: YYYY-MM-DD)
|
|
265
|
+
- **Compression**: Old files are gzipped (optional)
|
|
266
|
+
- **Retention**: Keep last `maxFiles` (default: 10)
|
|
267
|
+
|
|
268
|
+
## Development
|
|
269
|
+
|
|
270
|
+
```bash
|
|
271
|
+
# Install dependencies
|
|
272
|
+
bun install
|
|
273
|
+
|
|
274
|
+
# Run examples
|
|
275
|
+
bun run example:backend
|
|
276
|
+
bun run example:engine
|
|
277
|
+
bun run example:ui
|
|
278
|
+
|
|
279
|
+
# Run tests
|
|
280
|
+
bun test
|
|
281
|
+
|
|
282
|
+
# Run benchmarks
|
|
283
|
+
bun run benchmark
|
|
284
|
+
|
|
285
|
+
# Build package
|
|
286
|
+
bun run build
|
|
287
|
+
|
|
288
|
+
# Type check
|
|
289
|
+
bun run typecheck
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## Performance
|
|
293
|
+
|
|
294
|
+
Benchmarked on Apple M1:
|
|
295
|
+
- Simple log: ~0.05ms
|
|
296
|
+
- Log with metadata: ~0.08ms
|
|
297
|
+
- File write: ~0.15ms
|
|
298
|
+
- Throughput: >15,000 logs/second
|
|
299
|
+
|
|
300
|
+
## License
|
|
301
|
+
|
|
302
|
+
MIT
|
|
303
|
+
|
|
304
|
+
## Author
|
|
305
|
+
|
|
306
|
+
Octaviaflow Team
|