@nexe/logger 0.1.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/LICENSE +21 -0
- package/README.md +305 -0
- package/dist/config.d.ts +30 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +82 -0
- package/dist/config.js.map +1 -0
- package/dist/factory.d.ts +14 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +99 -0
- package/dist/factory.js.map +1 -0
- package/dist/formatters.d.ts +43 -0
- package/dist/formatters.d.ts.map +1 -0
- package/dist/formatters.js +110 -0
- package/dist/formatters.js.map +1 -0
- package/dist/global.d.ts +33 -0
- package/dist/global.d.ts.map +1 -0
- package/dist/global.js +130 -0
- package/dist/global.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +77 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +131 -0
- package/dist/logger.js.map +1 -0
- package/dist/serializers.d.ts +35 -0
- package/dist/serializers.d.ts.map +1 -0
- package/dist/serializers.js +151 -0
- package/dist/serializers.js.map +1 -0
- package/dist/test-error-logging.d.ts +3 -0
- package/dist/test-error-logging.d.ts.map +1 -0
- package/dist/test-error-logging.js +44 -0
- package/dist/test-error-logging.js.map +1 -0
- package/dist/transports.d.ts +56 -0
- package/dist/transports.d.ts.map +1 -0
- package/dist/transports.js +130 -0
- package/dist/transports.js.map +1 -0
- package/dist/types.d.ts +135 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +16 -0
- package/dist/types.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 aqz236
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,305 @@
|
|
1
|
+
# @nexe/logger
|
2
|
+
|
3
|
+
<div align="center">
|
4
|
+
|
5
|
+

|
6
|
+
|
7
|
+
**A powerful, modern logging solution for Nexe framework based on Pino**
|
8
|
+
|
9
|
+
[](https://www.npmjs.com/package/@nexe/logger)
|
10
|
+
[](https://www.npmjs.com/package/@nexe/logger)
|
11
|
+
[](https://github.com/aqz236/nexe-logger/blob/main/LICENSE)
|
12
|
+
[](https://www.typescriptlang.org/)
|
13
|
+
|
14
|
+
</div>
|
15
|
+
|
16
|
+
## ✨ Features
|
17
|
+
|
18
|
+
- 🚀 **High Performance** - Built on top of Pino, one of the fastest JSON loggers
|
19
|
+
- 🎨 **Beautiful Output** - Clean, readable console logs with customizable formatting
|
20
|
+
- 🔧 **TypeScript Native** - Full TypeScript support with comprehensive type definitions
|
21
|
+
- 🌍 **Environment Aware** - Different configurations for development, production, and testing
|
22
|
+
- 📝 **Structured Logging** - JSON logging with customizable serializers
|
23
|
+
- 🎯 **Context Support** - Add request IDs, user IDs, and custom context data
|
24
|
+
- 🔄 **Multiple Transports** - Console, file, rotating files, and custom transports
|
25
|
+
- 🛡️ **Security First** - Built-in redaction for sensitive information
|
26
|
+
- 📦 **Framework Integration** - Seamlessly integrates with Nexe framework
|
27
|
+
|
28
|
+

|
29
|
+
|
30
|
+
## 📦 Installation
|
31
|
+
|
32
|
+
```bash
|
33
|
+
# npm
|
34
|
+
npm install @nexe/logger
|
35
|
+
|
36
|
+
# yarn
|
37
|
+
yarn add @nexe/logger
|
38
|
+
|
39
|
+
# pnpm
|
40
|
+
pnpm add @nexe/logger
|
41
|
+
|
42
|
+
# bun
|
43
|
+
bun add @nexe/logger
|
44
|
+
```
|
45
|
+
|
46
|
+
## 🚀 Quick Start
|
47
|
+
|
48
|
+
### Basic Usage
|
49
|
+
|
50
|
+
```typescript
|
51
|
+
import { createLogger, logger } from '@nexe/logger';
|
52
|
+
|
53
|
+
// Use global logger
|
54
|
+
logger.info('Hello, World!');
|
55
|
+
logger.error('Something went wrong!', { error: 'details' });
|
56
|
+
|
57
|
+
// Create named logger
|
58
|
+
const apiLogger = createLogger('API');
|
59
|
+
apiLogger.info('API server started');
|
60
|
+
```
|
61
|
+
|
62
|
+
### With Nexe Framework
|
63
|
+
|
64
|
+
```typescript
|
65
|
+
import { Hono } from 'hono';
|
66
|
+
import { createLogger } from '@nexe/logger';
|
67
|
+
|
68
|
+
const logger = createLogger('app');
|
69
|
+
logger.info('🚀 Starting Nexe application...');
|
70
|
+
|
71
|
+
const app = new Hono();
|
72
|
+
app.get('/', c => {
|
73
|
+
return c.text('Hello Nexe!');
|
74
|
+
});
|
75
|
+
|
76
|
+
logger.info('✅ Application ready!');
|
77
|
+
export default app;
|
78
|
+
```
|
79
|
+
|
80
|
+
### Advanced Configuration
|
81
|
+
|
82
|
+
```typescript
|
83
|
+
import { createLogger, LogLevel } from '@nexe/logger';
|
84
|
+
|
85
|
+
const customLogger = createLogger('MyService', {
|
86
|
+
level: LogLevel.DEBUG,
|
87
|
+
transport: {
|
88
|
+
target: 'pino-pretty',
|
89
|
+
options: {
|
90
|
+
colorize: true,
|
91
|
+
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
|
92
|
+
},
|
93
|
+
},
|
94
|
+
});
|
95
|
+
|
96
|
+
customLogger.debug('Debug information');
|
97
|
+
customLogger.info('Service initialized');
|
98
|
+
customLogger.warn('Warning message');
|
99
|
+
customLogger.error('Error occurred');
|
100
|
+
```
|
101
|
+
|
102
|
+
## 📖 API Reference
|
103
|
+
|
104
|
+
### Core Functions
|
105
|
+
|
106
|
+
#### `createLogger(name?, config?)`
|
107
|
+
|
108
|
+
Creates a new logger instance with optional name and configuration.
|
109
|
+
|
110
|
+
```typescript
|
111
|
+
const logger = createLogger('ServiceName', {
|
112
|
+
level: LogLevel.INFO,
|
113
|
+
// ... other options
|
114
|
+
});
|
115
|
+
```
|
116
|
+
|
117
|
+
#### `logger` (Global Logger)
|
118
|
+
|
119
|
+
Pre-configured global logger instance.
|
120
|
+
|
121
|
+
```typescript
|
122
|
+
import { logger } from '@nexe/logger';
|
123
|
+
|
124
|
+
logger.info('Global log message');
|
125
|
+
```
|
126
|
+
|
127
|
+
### Logger Methods
|
128
|
+
|
129
|
+
```typescript
|
130
|
+
logger.fatal('Fatal error');
|
131
|
+
logger.error('Error message');
|
132
|
+
logger.warn('Warning message');
|
133
|
+
logger.info('Info message');
|
134
|
+
logger.debug('Debug message');
|
135
|
+
logger.trace('Trace message');
|
136
|
+
```
|
137
|
+
|
138
|
+
### Context Support
|
139
|
+
|
140
|
+
```typescript
|
141
|
+
// Set context for all subsequent logs
|
142
|
+
logger.setContext({ requestId: '123', userId: 'user456' });
|
143
|
+
logger.info('User action performed');
|
144
|
+
|
145
|
+
// Create child logger with context
|
146
|
+
const childLogger = logger.child({ module: 'auth' });
|
147
|
+
childLogger.info('Authentication successful');
|
148
|
+
```
|
149
|
+
|
150
|
+
## ⚙️ Configuration
|
151
|
+
|
152
|
+
### Environment-based Configuration
|
153
|
+
|
154
|
+
The logger automatically adapts based on `NODE_ENV`:
|
155
|
+
|
156
|
+
- **Development**: Pretty-printed colored output
|
157
|
+
- **Production**: JSON format optimized for log aggregation
|
158
|
+
- **Test**: Minimal logging to reduce noise
|
159
|
+
|
160
|
+
### Custom Configuration
|
161
|
+
|
162
|
+
```typescript
|
163
|
+
import { createLogger, LogLevel } from '@nexe/logger';
|
164
|
+
|
165
|
+
const logger = createLogger('MyApp', {
|
166
|
+
level: LogLevel.INFO,
|
167
|
+
redact: ['password', 'token'], // Hide sensitive fields
|
168
|
+
formatters: {
|
169
|
+
level: (label, number) => ({ level: number }),
|
170
|
+
bindings: bindings => ({ service: 'MyApp', ...bindings }),
|
171
|
+
},
|
172
|
+
serializers: {
|
173
|
+
req: req => ({ method: req.method, url: req.url }),
|
174
|
+
res: res => ({ statusCode: res.statusCode }),
|
175
|
+
},
|
176
|
+
});
|
177
|
+
```
|
178
|
+
|
179
|
+
## 🎨 Output Examples
|
180
|
+
|
181
|
+
### Development Output
|
182
|
+
|
183
|
+
```
|
184
|
+
[2025-07-27 14:08:19.149] INFO (MyService): User login successful
|
185
|
+
[2025-07-27 14:08:19.150] ERROR (Auth): Invalid credentials provided
|
186
|
+
error: "Invalid username or password"
|
187
|
+
requestId: "req_123456"
|
188
|
+
```
|
189
|
+
|
190
|
+
### Production Output
|
191
|
+
|
192
|
+
```json
|
193
|
+
{"level":30,"time":1643299699149,"name":"MyService","msg":"User login successful","requestId":"req_123456"}
|
194
|
+
{"level":50,"time":1643299699150,"name":"Auth","msg":"Invalid credentials provided","err":{"type":"AuthError","message":"Invalid username or password"}}
|
195
|
+
```
|
196
|
+
|
197
|
+
## 🔧 Transports
|
198
|
+
|
199
|
+
### Console Transport
|
200
|
+
|
201
|
+
```typescript
|
202
|
+
import { createConsoleTransport } from '@nexe/logger';
|
203
|
+
|
204
|
+
const transport = createConsoleTransport({
|
205
|
+
colorize: true,
|
206
|
+
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
|
207
|
+
});
|
208
|
+
```
|
209
|
+
|
210
|
+
### File Transport
|
211
|
+
|
212
|
+
```typescript
|
213
|
+
import { createFileTransport } from '@nexe/logger';
|
214
|
+
|
215
|
+
const transport = createFileTransport({
|
216
|
+
destination: './logs/app.log',
|
217
|
+
mkdir: true,
|
218
|
+
});
|
219
|
+
```
|
220
|
+
|
221
|
+
### Rotating Files
|
222
|
+
|
223
|
+
```typescript
|
224
|
+
import { createRotatingFileTransport } from '@nexe/logger';
|
225
|
+
|
226
|
+
const transport = createRotatingFileTransport({
|
227
|
+
filename: './logs/app-%DATE%.log',
|
228
|
+
frequency: 'daily',
|
229
|
+
maxSize: '10M',
|
230
|
+
maxFiles: '7',
|
231
|
+
});
|
232
|
+
```
|
233
|
+
|
234
|
+
## 🛡️ Security Features
|
235
|
+
|
236
|
+
### Automatic Redaction
|
237
|
+
|
238
|
+
```typescript
|
239
|
+
const logger = createLogger('SecureApp', {
|
240
|
+
redact: ['password', 'token', 'authorization', 'cookie'],
|
241
|
+
});
|
242
|
+
|
243
|
+
// This will automatically redact sensitive fields
|
244
|
+
logger.info('User data', {
|
245
|
+
username: 'john_doe',
|
246
|
+
password: 'secret123', // This will be redacted
|
247
|
+
});
|
248
|
+
```
|
249
|
+
|
250
|
+
### Safe Serialization
|
251
|
+
|
252
|
+
All serializers include error handling to prevent logging failures from crashing your application.
|
253
|
+
|
254
|
+
## 🧪 Testing
|
255
|
+
|
256
|
+
```typescript
|
257
|
+
import { createLogger, LogLevel } from '@nexe/logger';
|
258
|
+
|
259
|
+
// Create test logger with minimal output
|
260
|
+
const testLogger = createLogger('Test', {
|
261
|
+
level: LogLevel.WARN, // Only log warnings and errors in tests
|
262
|
+
});
|
263
|
+
```
|
264
|
+
|
265
|
+
## 📊 Star History
|
266
|
+
|
267
|
+
[](https://star-history.com/#aqz236/nexe-logger&Date)
|
268
|
+
|
269
|
+
## 📊 Performance
|
270
|
+
|
271
|
+
- **Zero-cost abstractions** when logging is disabled
|
272
|
+
- **Fast serialization** with Pino's optimized JSON stringification
|
273
|
+
- **Minimal memory footprint** with streaming architecture
|
274
|
+
- **Benchmark**: Up to 35,000 logs/second in production mode
|
275
|
+
|
276
|
+
## 🤝 Contributing
|
277
|
+
|
278
|
+
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
|
279
|
+
|
280
|
+
1. Fork the repository
|
281
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
282
|
+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
283
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
284
|
+
5. Open a Pull Request
|
285
|
+
|
286
|
+
## 📄 License
|
287
|
+
|
288
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
289
|
+
|
290
|
+
## 🔗 Links
|
291
|
+
|
292
|
+
- [Nexe Framework](https://github.com/aqz236/nexe)
|
293
|
+
- [Pino Logger](https://github.com/pinojs/pino)
|
294
|
+
- [Documentation](https://github.com/aqz236/nexe-logger/wiki)
|
295
|
+
- [Issues](https://github.com/aqz236/nexe-logger/issues)
|
296
|
+
|
297
|
+
---
|
298
|
+
|
299
|
+
<div align="center">
|
300
|
+
|
301
|
+
**Built with ❤️ for the Nexe ecosystem**
|
302
|
+
|
303
|
+
[⭐ Star us on GitHub](https://github.com/aqz236/nexe-logger) • [📝 Report Bug](https://github.com/aqz236/nexe-logger/issues) • [💡 Request Feature](https://github.com/aqz236/nexe-logger/issues)
|
304
|
+
|
305
|
+
</div>
|
package/dist/config.d.ts
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
import { LoggerConfig } from "./types";
|
2
|
+
/**
|
3
|
+
* 默认日志配置
|
4
|
+
*/
|
5
|
+
export declare const DEFAULT_CONFIG: LoggerConfig;
|
6
|
+
/**
|
7
|
+
* 开发环境配置
|
8
|
+
*/
|
9
|
+
export declare const DEVELOPMENT_CONFIG: Partial<LoggerConfig>;
|
10
|
+
/**
|
11
|
+
* 生产环境配置
|
12
|
+
*/
|
13
|
+
export declare const PRODUCTION_CONFIG: Partial<LoggerConfig>;
|
14
|
+
/**
|
15
|
+
* 测试环境配置
|
16
|
+
*/
|
17
|
+
export declare const TEST_CONFIG: Partial<LoggerConfig>;
|
18
|
+
/**
|
19
|
+
* 根据环境获取配置
|
20
|
+
*/
|
21
|
+
export declare function getEnvironmentConfig(env?: string): Partial<LoggerConfig>;
|
22
|
+
/**
|
23
|
+
* 从环境变量创建配置
|
24
|
+
*/
|
25
|
+
export declare function createConfigFromEnv(): Partial<LoggerConfig>;
|
26
|
+
/**
|
27
|
+
* 合并配置
|
28
|
+
*/
|
29
|
+
export declare function mergeConfig(...configs: Partial<LoggerConfig>[]): LoggerConfig;
|
30
|
+
//# sourceMappingURL=config.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA+B,MAAM,SAAS,CAAC;AAEpE;;GAEG;AACH,eAAO,MAAM,cAAc,EAAE,YAU5B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,EAAE,OAAO,CAAC,YAAY,CAGpD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,EAAE,OAAO,CAAC,YAAY,CAGnD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,EAAE,OAAO,CAAC,YAAY,CAE7C,CAAC;AAEF;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAexE;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAAC,YAAY,CAAC,CAa3D;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,GAAG,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,EAAE,GAAG,YAAY,CAI7E"}
|
package/dist/config.js
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.TEST_CONFIG = exports.PRODUCTION_CONFIG = exports.DEVELOPMENT_CONFIG = exports.DEFAULT_CONFIG = void 0;
|
4
|
+
exports.getEnvironmentConfig = getEnvironmentConfig;
|
5
|
+
exports.createConfigFromEnv = createConfigFromEnv;
|
6
|
+
exports.mergeConfig = mergeConfig;
|
7
|
+
const types_1 = require("./types");
|
8
|
+
/**
|
9
|
+
* 默认日志配置
|
10
|
+
*/
|
11
|
+
exports.DEFAULT_CONFIG = {
|
12
|
+
level: types_1.LogLevel.INFO,
|
13
|
+
timestamp: true,
|
14
|
+
name: "HestJS",
|
15
|
+
messageKey: "msg",
|
16
|
+
errorKey: "err",
|
17
|
+
base: {
|
18
|
+
pid: process.pid,
|
19
|
+
hostname: require("os").hostname(),
|
20
|
+
},
|
21
|
+
};
|
22
|
+
/**
|
23
|
+
* 开发环境配置
|
24
|
+
*/
|
25
|
+
exports.DEVELOPMENT_CONFIG = {
|
26
|
+
level: types_1.LogLevel.DEBUG,
|
27
|
+
// 不在这里设置 transport,让 factory 使用 transports.ts 中的配置
|
28
|
+
};
|
29
|
+
/**
|
30
|
+
* 生产环境配置
|
31
|
+
*/
|
32
|
+
exports.PRODUCTION_CONFIG = {
|
33
|
+
level: types_1.LogLevel.INFO,
|
34
|
+
redact: ["password", "token", "authorization", "cookie"],
|
35
|
+
};
|
36
|
+
/**
|
37
|
+
* 测试环境配置
|
38
|
+
*/
|
39
|
+
exports.TEST_CONFIG = {
|
40
|
+
level: types_1.LogLevel.WARN,
|
41
|
+
};
|
42
|
+
/**
|
43
|
+
* 根据环境获取配置
|
44
|
+
*/
|
45
|
+
function getEnvironmentConfig(env) {
|
46
|
+
const environment = env || process.env.NODE_ENV || "development";
|
47
|
+
switch (environment) {
|
48
|
+
case "production":
|
49
|
+
case "prod":
|
50
|
+
return exports.PRODUCTION_CONFIG;
|
51
|
+
case "test":
|
52
|
+
case "testing":
|
53
|
+
return exports.TEST_CONFIG;
|
54
|
+
case "development":
|
55
|
+
case "dev":
|
56
|
+
default:
|
57
|
+
return exports.DEVELOPMENT_CONFIG;
|
58
|
+
}
|
59
|
+
}
|
60
|
+
/**
|
61
|
+
* 从环境变量创建配置
|
62
|
+
*/
|
63
|
+
function createConfigFromEnv() {
|
64
|
+
const env = process.env;
|
65
|
+
const config = {};
|
66
|
+
if (env.LOG_LEVEL) {
|
67
|
+
config.level = env.LOG_LEVEL;
|
68
|
+
}
|
69
|
+
if (env.LOG_NAME) {
|
70
|
+
config.name = env.LOG_NAME;
|
71
|
+
}
|
72
|
+
return config;
|
73
|
+
}
|
74
|
+
/**
|
75
|
+
* 合并配置
|
76
|
+
*/
|
77
|
+
function mergeConfig(...configs) {
|
78
|
+
return configs.reduce((merged, config) => ({ ...merged, ...config }), {
|
79
|
+
...exports.DEFAULT_CONFIG,
|
80
|
+
});
|
81
|
+
}
|
82
|
+
//# sourceMappingURL=config.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;AA2CA,oDAeC;AAKD,kDAaC;AAKD,kCAIC;AArFD,mCAAoE;AAEpE;;GAEG;AACU,QAAA,cAAc,GAAiB;IAC1C,KAAK,EAAE,gBAAQ,CAAC,IAAI;IACpB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE,KAAK;IACjB,QAAQ,EAAE,KAAK;IACf,IAAI,EAAE;QACJ,GAAG,EAAE,OAAO,CAAC,GAAG;QAChB,QAAQ,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE;KACnC;CACF,CAAC;AAEF;;GAEG;AACU,QAAA,kBAAkB,GAA0B;IACvD,KAAK,EAAE,gBAAQ,CAAC,KAAK;IACrB,mDAAmD;CACpD,C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GAAG,sBAAc;KAClB,CAAiB,CAAC;AACrB,CAAC"}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import type { Logger, LoggerConfig } from "./types";
|
2
|
+
/**
|
3
|
+
* 创建 Logger 实例
|
4
|
+
*/
|
5
|
+
export declare function createLogger(name?: string, config?: Partial<LoggerConfig>): Logger;
|
6
|
+
/**
|
7
|
+
* 创建带有上下文的 Logger
|
8
|
+
*/
|
9
|
+
export declare function createLoggerWithContext(name: string, context: Record<string, any>, config?: Partial<LoggerConfig>): Logger;
|
10
|
+
/**
|
11
|
+
* 创建子 Logger
|
12
|
+
*/
|
13
|
+
export declare function createChildLogger(parent: Logger, bindings: Record<string, any>): Logger;
|
14
|
+
//# sourceMappingURL=factory.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEpD;;GAEG;AACH,wBAAgB,YAAY,CAC1B,IAAI,CAAC,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,GAC7B,MAAM,CA4ER;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC5B,MAAM,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,GAC7B,MAAM,CAGR;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC5B,MAAM,CAER"}
|
package/dist/factory.js
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.createLogger = createLogger;
|
7
|
+
exports.createLoggerWithContext = createLoggerWithContext;
|
8
|
+
exports.createChildLogger = createChildLogger;
|
9
|
+
const pino_1 = __importDefault(require("pino"));
|
10
|
+
const pino_pretty_1 = __importDefault(require("pino-pretty"));
|
11
|
+
const config_1 = require("./config");
|
12
|
+
const formatters_1 = require("./formatters");
|
13
|
+
const logger_1 = require("./logger");
|
14
|
+
const serializers_1 = require("./serializers");
|
15
|
+
/**
|
16
|
+
* 创建 Logger 实例
|
17
|
+
*/
|
18
|
+
function createLogger(name, config) {
|
19
|
+
// 合并配置:默认配置 -> 环境配置 -> 环境变量配置 -> 用户配置
|
20
|
+
const envConfig = (0, config_1.getEnvironmentConfig)();
|
21
|
+
const envVarConfig = (0, config_1.createConfigFromEnv)();
|
22
|
+
const userConfig = config || {};
|
23
|
+
// 如果提供了名称,添加到配置中
|
24
|
+
if (name) {
|
25
|
+
userConfig.name = name;
|
26
|
+
}
|
27
|
+
const finalConfig = (0, config_1.mergeConfig)(envConfig, envVarConfig, userConfig);
|
28
|
+
// 设置默认序列化器和格式化器
|
29
|
+
if (!finalConfig.serializers) {
|
30
|
+
finalConfig.serializers = (0, serializers_1.getDefaultSerializers)();
|
31
|
+
}
|
32
|
+
if (!finalConfig.formatters) {
|
33
|
+
finalConfig.formatters = (0, formatters_1.getDefaultFormatters)();
|
34
|
+
}
|
35
|
+
// 根据环境创建 logger
|
36
|
+
const environment = process.env.NODE_ENV || "development";
|
37
|
+
let pinoLogger;
|
38
|
+
if (environment === "development" || environment === "dev") {
|
39
|
+
// 开发环境使用 pino-pretty 作为流
|
40
|
+
const stream = (0, pino_pretty_1.default)({
|
41
|
+
levelFirst: false,
|
42
|
+
colorize: true,
|
43
|
+
ignore: "pid,hostname,error", // 忽略重复的error字段,只显示err
|
44
|
+
translateTime: "yy-mm-dd HH:MM:ss.l",
|
45
|
+
messageFormat: "{msg}",
|
46
|
+
customLevels: {
|
47
|
+
fatal: 60,
|
48
|
+
error: 50,
|
49
|
+
warn: 40,
|
50
|
+
info: 30,
|
51
|
+
debug: 20,
|
52
|
+
trace: 10,
|
53
|
+
},
|
54
|
+
customColors: {
|
55
|
+
60: "bgRed",
|
56
|
+
50: "red",
|
57
|
+
40: "yellow",
|
58
|
+
30: "green",
|
59
|
+
20: "blue",
|
60
|
+
10: "gray",
|
61
|
+
},
|
62
|
+
});
|
63
|
+
const pinoConfig = {
|
64
|
+
name: finalConfig.name || name || "HestJS",
|
65
|
+
level: finalConfig.level || "debug",
|
66
|
+
serializers: finalConfig.serializers || {},
|
67
|
+
formatters: finalConfig.formatters || {},
|
68
|
+
};
|
69
|
+
pinoLogger = (0, pino_1.default)(pinoConfig, stream);
|
70
|
+
}
|
71
|
+
else {
|
72
|
+
// 生产环境直接输出 JSON 到 stdout
|
73
|
+
pinoLogger = (0, pino_1.default)({
|
74
|
+
name: finalConfig.name || "HestJS",
|
75
|
+
level: finalConfig.level || "info",
|
76
|
+
serializers: finalConfig.serializers || {},
|
77
|
+
formatters: finalConfig.formatters || {},
|
78
|
+
});
|
79
|
+
}
|
80
|
+
// 如果有 name,创建一个 child logger 来包含 name
|
81
|
+
const loggerName = name || finalConfig.name || "HestJS";
|
82
|
+
const loggerWithName = pinoLogger.child({ name: loggerName });
|
83
|
+
// 返回 HestLogger 实例
|
84
|
+
return new logger_1.HestLogger(loggerWithName);
|
85
|
+
}
|
86
|
+
/**
|
87
|
+
* 创建带有上下文的 Logger
|
88
|
+
*/
|
89
|
+
function createLoggerWithContext(name, context, config) {
|
90
|
+
const logger = createLogger(name, config);
|
91
|
+
return logger.setContext(context);
|
92
|
+
}
|
93
|
+
/**
|
94
|
+
* 创建子 Logger
|
95
|
+
*/
|
96
|
+
function createChildLogger(parent, bindings) {
|
97
|
+
return parent.child(bindings);
|
98
|
+
}
|
99
|
+
//# sourceMappingURL=factory.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"factory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,IAAY,EACZ,OAA4B,EAC5B,MAA8B;IAE9B,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC1C,OAAO,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;AACpC,CAAC;AAED;;GAEG;AACH,SAAgB,iBAAiB,CAC/B,MAAc,EACd,QAA6B;IAE7B,OAAO,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;AAChC,CAAC"}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
/**
|
2
|
+
* 日志级别格式化器 - 简洁版本,只保留数字级别
|
3
|
+
*/
|
4
|
+
export declare const levelFormatter: (_label: string, number: number) => {
|
5
|
+
level: number;
|
6
|
+
};
|
7
|
+
/**
|
8
|
+
* 时间戳格式化器
|
9
|
+
*/
|
10
|
+
export declare const timestampFormatter: () => {
|
11
|
+
timestamp: string;
|
12
|
+
};
|
13
|
+
/**
|
14
|
+
* 绑定数据格式化器 - 默认返回空对象,保持简洁
|
15
|
+
*/
|
16
|
+
export declare const bindingsFormatter: (_bindings: Record<string, any>) => {};
|
17
|
+
/**
|
18
|
+
* 日志对象格式化器 - 标准化日志结构
|
19
|
+
*/
|
20
|
+
export declare const logFormatter: (object: Record<string, any>) => {
|
21
|
+
"@timestamp": any;
|
22
|
+
level: any;
|
23
|
+
message: any;
|
24
|
+
};
|
25
|
+
/**
|
26
|
+
* 错误格式化器 - 专门处理错误对象
|
27
|
+
*/
|
28
|
+
export declare const errorFormatter: (object: Record<string, any>) => Record<string, any>;
|
29
|
+
/**
|
30
|
+
* 获取默认格式化器
|
31
|
+
*/
|
32
|
+
export declare function getDefaultFormatters(): {
|
33
|
+
level: (_label: string, number: number) => {
|
34
|
+
level: number;
|
35
|
+
};
|
36
|
+
bindings: (_bindings: Record<string, any>) => {};
|
37
|
+
log: (object: Record<string, any>) => Record<string, any>;
|
38
|
+
};
|
39
|
+
/**
|
40
|
+
* 创建自定义格式化器
|
41
|
+
*/
|
42
|
+
export declare function createFormatter<T = Record<string, any>>(fn: (object: T) => T): (object: T) => T;
|
43
|
+
//# sourceMappingURL=formatters.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,MAAM,EAAE,QAAQ,MAAM;;CAS5D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB;;CAI9B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,WAAW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,OAa/D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;;;;CAUvD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,wBAezD,CAAC;AAEF;;GAEG;AACH,wBAAgB,oBAAoB;oBA5EG,MAAM,UAAU,MAAM;;;0BAuBhB,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;kBAyD9C,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;EAMpC;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACrD,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,GACnB,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,CAalB"}
|