Skip to content

Commit 358e008

Browse files
committed
docs: document Log Rotation and Encryption
chore: wip
1 parent 19cd213 commit 358e008

File tree

2 files changed

+466
-0
lines changed

2 files changed

+466
-0
lines changed

docs/library.md

+204
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,210 @@ catch (error) {
209209
}
210210
```
211211

212+
### Log Rotation
213+
214+
Clarity provides flexible log rotation options to manage log files effectively:
215+
216+
```ts
217+
import { Logger } from 'clarity'
218+
219+
// Basic rotation with defaults
220+
const logger = new Logger('app', {
221+
rotation: {
222+
frequency: 'daily', // Rotate logs daily
223+
maxSize: 10485760, // 10MB max file size
224+
maxFiles: 5, // Keep 5 rotated files
225+
compress: false, // Don't compress old logs
226+
}
227+
})
228+
229+
// Advanced rotation configuration
230+
const advancedLogger = new Logger('app', {
231+
rotation: {
232+
// Time-based rotation
233+
frequency: 'daily', // Options: 'hourly', 'daily', 'weekly', 'monthly'
234+
rotateHour: 0, // Hour to rotate (0-23)
235+
rotateMinute: 0, // Minute to rotate (0-59)
236+
rotateDayOfWeek: 0, // Day of week to rotate (0-6, 0 is Sunday)
237+
rotateDayOfMonth: 1, // Day of month to rotate (1-31)
238+
239+
// Size-based rotation
240+
maxSize: 10 * 1024 * 1024, // Rotate when file reaches 10MB
241+
maxFiles: 5, // Keep 5 rotated files
242+
243+
// File handling
244+
compress: true, // Compress rotated logs
245+
encrypt: { // Encrypt rotated logs
246+
algorithm: 'aes-256-gcm',
247+
compress: true,
248+
}
249+
}
250+
})
251+
```
252+
253+
#### Rotation Configuration
254+
255+
1. **Time-Based Rotation**
256+
257+
```ts
258+
const logger = new Logger('app', {
259+
rotation: {
260+
// Rotate daily at midnight
261+
frequency: 'daily',
262+
rotateHour: 0,
263+
rotateMinute: 0,
264+
265+
// Or weekly on Sunday
266+
frequency: 'weekly',
267+
rotateDayOfWeek: 0,
268+
269+
// Or monthly on the 1st
270+
frequency: 'monthly',
271+
rotateDayOfMonth: 1,
272+
}
273+
})
274+
```
275+
276+
2. **Size-Based Rotation**
277+
278+
```ts
279+
const logger = new Logger('app', {
280+
rotation: {
281+
maxSize: 50 * 1024 * 1024, // 50MB
282+
maxFiles: 10, // Keep 10 files
283+
}
284+
})
285+
```
286+
287+
3. **Combined Rotation**
288+
289+
```ts
290+
const logger = new Logger('app', {
291+
rotation: {
292+
frequency: 'daily',
293+
maxSize: 10 * 1024 * 1024, // Also rotate if file reaches 10MB
294+
maxFiles: 5,
295+
compress: true, // Compress old logs
296+
}
297+
})
298+
```
299+
300+
#### File Management
301+
302+
```ts
303+
// Configure log directory and naming
304+
const logger = new Logger('app', {
305+
logDirectory: './logs', // Where to store logs
306+
logDatePattern: 'YYYY-MM-DD', // Date pattern in filenames
307+
maxLogSize: 10 * 1024 * 1024, // Maximum size per log file
308+
})
309+
```
310+
311+
#### Best Practices
312+
313+
1. **Storage Management**
314+
- Set appropriate `maxFiles` to prevent disk space issues
315+
- Enable compression for long-term storage
316+
- Use time-based rotation for compliance requirements
317+
318+
2. **Performance**
319+
- Balance `maxSize` with write frequency
320+
- Consider filesystem performance
321+
- Monitor rotation overhead
322+
323+
3. **Maintenance**
324+
- Regular cleanup of old log files
325+
- Verify rotation timing
326+
- Monitor disk space usage
327+
328+
### Encryption
329+
330+
Clarity supports encryption of log files for sensitive data protection:
331+
332+
```ts
333+
import { Logger } from 'clarity'
334+
335+
// Create a logger with encryption enabled
336+
const logger = new Logger('secure-app', {
337+
rotation: {
338+
encrypt: {
339+
algorithm: 'aes-256-gcm', // Supported: 'aes-256-cbc', 'aes-256-gcm'
340+
compress: true, // Optional compression before encryption
341+
}
342+
}
343+
})
344+
345+
// Logs are automatically encrypted
346+
await logger.info('Sensitive data', {
347+
creditCard: '4111-1111-1111-1111',
348+
ssn: '123-45-6789'
349+
})
350+
351+
// Reading encrypted logs
352+
const entries = await logger.readLog('path/to/logfile')
353+
// Entries are automatically decrypted
354+
```
355+
356+
#### Key Management
357+
358+
Clarity supports automatic key rotation and management:
359+
360+
```ts
361+
const logger = new Logger('secure-app', {
362+
rotation: {
363+
encrypt: {
364+
algorithm: 'aes-256-gcm',
365+
},
366+
keyRotation: {
367+
enabled: true,
368+
maxKeys: 3, // Keep last 3 keys for decrypting old logs
369+
}
370+
}
371+
})
372+
373+
// Manually manage keys if needed
374+
const { key, id } = logger.getCurrentKey()
375+
logger.setEncryptionKey('custom-key-id', Buffer.from('your-key'))
376+
```
377+
378+
#### Re-encryption
379+
380+
You can re-encrypt logs with new keys or algorithms:
381+
382+
```ts
383+
await logger.reEncryptLogFile(
384+
'old-logs.txt',
385+
'new-logs.txt',
386+
{
387+
algorithm: 'aes-256-gcm',
388+
compress: true
389+
}
390+
)
391+
392+
// Validate encryption
393+
const validation = await logger.validateEncryption('logs.txt')
394+
if (!validation.isValid) {
395+
console.error('Encryption issues:', validation.errors)
396+
}
397+
```
398+
399+
#### Best Practices
400+
401+
1. **Key Security**
402+
- Use environment variables for encryption keys
403+
- Rotate keys regularly
404+
- Back up keys securely
405+
406+
2. **Algorithm Selection**
407+
- Use `aes-256-gcm` for best security (authenticated encryption)
408+
- Enable compression for large logs
409+
- Consider performance impact on high-volume logging
410+
411+
3. **Validation**
412+
- Regularly validate encrypted logs
413+
- Keep backup copies before re-encryption
414+
- Monitor decryption errors
415+
212416
## TypeScript Integration
213417

214418
### Type-Safe Logging

0 commit comments

Comments
 (0)