Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1

Merged
merged 3 commits into from
Mar 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
docs(生成文档): 添加md文档
fengxinming committed Mar 21, 2025
commit d1d44dcd60396cb1f9326dd70519431f63d9ce7c
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -3,7 +3,14 @@
"fe",
"fe/ts"
],
"plugins": [
"simple-import-sort"
],
"globals": {
"__DEV__": true
},
"rules": {
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error"
}
}
131 changes: 7 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
@@ -9,8 +9,6 @@

> 🚀 A flexible and efficient Node.js logging library supporting console output, file rolling, custom formats, and log levels.
## 📅 [中文](README_zh-CN.md) | English

---

## 🌟 Core Features
@@ -27,8 +25,13 @@
- **Context Management**
Supports Mapped Diagnostic Context (MDC) for attaching request IDs, user information, etc.

- **High-Performance & Cluster Support**
Non-blocking I/O, backpressure control, and file locking for reliable logging in high-concurrency environments.
---

## 📅 Documentation

For detailed usage instructions and API references, please visit the official documentation:

👉 [View Full Documentation](https://fengxinming.github.io/base-log-factory/)

---

@@ -55,35 +58,6 @@ const logger = logFactory.getLogger('app');
logger.info('Server started successfully');
```

### 3. Advanced Configuration Example
```typescript
// Configure rolling files, custom formats, and context
import {
LogFactory,
ConsoleAppender,
FileAppender,
DateFileAppender,
PatternLayout
} from 'base-log-factory';

const logFactory = new LogFactory({
level: 'DEBUG', // Set global log level
appenders: [
new ConsoleAppender(),
new FileAppender('./logs/app.log', {
layout: new PatternLayout('[%d{YYYY-MM-DD HH:mm:ss}] [%p] %c - %m %x{userId}'),
maxSize: 1024 * 1024, // 1MB
backups: 5,
compress: true // Enable GZIP compression
}),
new DateFileAppender('./logs/app.daily.log', {
pattern: 'YYYY-MM-DD',
backups: 7 // Keep 7 days of logs
})
]
});
```

---

## 🛠 Core Concepts
@@ -108,97 +82,6 @@ const logFactory = new LogFactory({

---

## 🎁 Usage Examples

### Example 1: Context-aware API Logging
```typescript
// Attach user ID to request logs
app.get('/api/data', (req, res) => {
const userId = req.user.id;
logger.addContext('userId', userId);
try {
// Business logic
logger.debug('Processing request');
res.send(data);
} catch (err) {
logger.error('Request failed', err);
} finally {
logger.removeContext('userId'); // Clean up context
}
});
```

### Example 2: Custom Log Format
```typescript
// Define format with request ID and timestamp
const layout = new PatternLayout(
'[%d{YYYY-MM-DD HH:mm:ss.SSS}] [%p] %c - %m %x{requestId} %t'
);

// Apply to FileAppender
new FileAppender('./logs/app.log', { layout });
```

### Example 3: Cluster Configuration
```typescript
// Use worker ID for log separation
const cluster = require('cluster');
const logFactory = new LogFactory({
appenders: [
new FileAppender(`./logs/app_${cluster.worker.id}.log`, {
layout: new BasicLayout(),
maxSize: 1024 * 1024 * 5 // 5MB
})
]
});
```

### Example 4: Dynamic Level Adjustment
```typescript
// Change global log level at runtime
logFactory.level = 'DEBUG'; // Enable DEBUG level
logger.debug('Debug message now visible'); // Output enabled

// Restore default level
logFactory.level = Level.INFO;
```

### Example 5: Resource Cleanup on Process Exit
```typescript
// Ensure all logs are flushed before exit
process.on('exit', async () => {
await logFactory.dispose(); // Wait for appenders to finalize
});

// Handle uncaught exceptions
process.on('uncaughtException', async (err) => {
logger.error('Process crashed', err);
await logFactory.dispose();
process.exit(1);
});
```

---

## ⚡ Performance Recommendations

1. **Asynchronous Writes**
Use asynchronous mode with `syncThreshold` for controlled synchronization:
```typescript
new FileAppender('app.log', {
syncThreshold: 10 * 1024, // Sync every 10KB
syncInterval: 5000 // Sync every 5 seconds
});
```

2. **File Locking**
Enable `useLock: true` in cluster environments:
```typescript
new FileAppender('app.log', { useLock: true });
```

---

## 📝 Contribution Guide

1. **Development Setup**
220 changes: 0 additions & 220 deletions README_zh-CN.md

This file was deleted.

Loading