@avss-tech/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 +220 -0
- package/package.json +50 -0
package/README.md
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# @avss-tech/logger - Client SDK
|
|
2
|
+
|
|
3
|
+
Production-ready TypeScript SDK for the AVS Centralized Logging Platform.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
✅ **Async/Non-blocking** - Fire-and-forget logging
|
|
8
|
+
✅ **Retry Logic** - Exponential backoff with jitter
|
|
9
|
+
✅ **Batch Processing** - Optional queue for high-volume scenarios
|
|
10
|
+
✅ **Type-Safe** - Complete TypeScript definitions
|
|
11
|
+
✅ **Validation** - Input validation & sanitization
|
|
12
|
+
✅ **Error Handling** - Graceful degradation
|
|
13
|
+
✅ **Statistics** - Built-in metrics tracking
|
|
14
|
+
✅ **Production-Ready** - Security & best practices built-in
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @avss-tech/logger
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { AVSLogger } from '@avss-tech/logger';
|
|
26
|
+
|
|
27
|
+
const logger = new AVSLogger({
|
|
28
|
+
project: 'my-app',
|
|
29
|
+
apiKey: 'your-api-key',
|
|
30
|
+
endpoint: 'http://logging-service:4000/logs',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Log message
|
|
34
|
+
await logger.log({
|
|
35
|
+
message: 'Application started',
|
|
36
|
+
level: 'info',
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Log audit event
|
|
40
|
+
await logger.audit({
|
|
41
|
+
actor: { userId: 'user123' },
|
|
42
|
+
action: 'USER_LOGIN',
|
|
43
|
+
entity: { type: 'USER', id: 'user123' },
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## API Documentation
|
|
48
|
+
|
|
49
|
+
### Configuration
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
interface AVSLoggerConfig {
|
|
53
|
+
project: string; // Required: Project identifier
|
|
54
|
+
apiKey: string; // Required: API key for authentication
|
|
55
|
+
endpoint?: string; // Optional: Logging service URL
|
|
56
|
+
timeout?: number; // Optional: Request timeout in ms
|
|
57
|
+
enableQueue?: boolean; // Optional: Enable queue batching
|
|
58
|
+
batchSize?: number; // Optional: Batch size for queue
|
|
59
|
+
batchTimeout?: number; // Optional: Batch timeout in ms
|
|
60
|
+
maxRetries?: number; // Optional: Max retries for failed requests
|
|
61
|
+
initialRetryDelay?: number; // Optional: Initial retry delay in ms
|
|
62
|
+
debug?: boolean; // Optional: Enable debug logging
|
|
63
|
+
customHeaders?: Record<string, string>; // Optional: Custom headers
|
|
64
|
+
onFatalError?: (error: Error) => void; // Optional: Error callback
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Methods
|
|
69
|
+
|
|
70
|
+
#### log() - Generic Logging
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
await logger.log({
|
|
74
|
+
message: 'User action completed',
|
|
75
|
+
level: 'info',
|
|
76
|
+
userId: 'user123',
|
|
77
|
+
meta: { action: 'profile_update' }
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### audit() - Audit Logging
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
await logger.audit({
|
|
85
|
+
actor: { userId: 'user123', email: 'user@example.com' },
|
|
86
|
+
action: 'DOCUMENT_SIGNED',
|
|
87
|
+
entity: { type: 'DOCUMENT', id: 'doc456' },
|
|
88
|
+
metadata: { version: '2.0' }
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### access() - Access Logging
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
await logger.access({
|
|
96
|
+
endpoint: '/api/users',
|
|
97
|
+
method: 'GET',
|
|
98
|
+
statusCode: 200,
|
|
99
|
+
ip: '192.168.1.1',
|
|
100
|
+
responseTimeMs: 45,
|
|
101
|
+
userId: 'user123'
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### error() - Error Logging
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
await logger.error({
|
|
109
|
+
message: 'Database connection failed',
|
|
110
|
+
stack: error.stack,
|
|
111
|
+
context: { database: 'postgresql' },
|
|
112
|
+
userId: 'user123'
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Queue Management
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
const logger = new AVSLogger({
|
|
120
|
+
project: 'my-app',
|
|
121
|
+
apiKey: 'your-api-key',
|
|
122
|
+
enableQueue: true,
|
|
123
|
+
batchSize: 50,
|
|
124
|
+
batchTimeout: 5000
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Logs are automatically batched and sent
|
|
128
|
+
await logger.log({ message: 'Test' });
|
|
129
|
+
|
|
130
|
+
// Manually flush the queue
|
|
131
|
+
await logger.flush();
|
|
132
|
+
|
|
133
|
+
// Cleanup
|
|
134
|
+
logger.destroy();
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Statistics
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
const stats = logger.getStatistics();
|
|
141
|
+
console.log(stats);
|
|
142
|
+
// {
|
|
143
|
+
// totalLogsAttempted: 10,
|
|
144
|
+
// successfulLogs: 9,
|
|
145
|
+
// failedLogs: 1,
|
|
146
|
+
// retriedLogs: 2,
|
|
147
|
+
// averageResponseTimeMs: 35,
|
|
148
|
+
// queueLength: 0
|
|
149
|
+
// }
|
|
150
|
+
|
|
151
|
+
logger.resetStatistics();
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Error Handling
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
try {
|
|
158
|
+
await logger.log({
|
|
159
|
+
message: 'Important event',
|
|
160
|
+
level: 'warn'
|
|
161
|
+
});
|
|
162
|
+
} catch (error) {
|
|
163
|
+
console.error('Logging failed:', error);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Or use the returned LogResult
|
|
167
|
+
const result = await logger.log({ message: 'Test' });
|
|
168
|
+
if (!result.success) {
|
|
169
|
+
console.error(result.message);
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Best Practices
|
|
174
|
+
|
|
175
|
+
1. **Initialize once** - Create a single logger instance in your app
|
|
176
|
+
2. **Use async/await** - Handle promises properly for error handling
|
|
177
|
+
3. **Enable queue for production** - Batch logs to reduce server load
|
|
178
|
+
4. **Set custom headers** - Add authentication or tracking headers
|
|
179
|
+
5. **Monitor statistics** - Track logging performance
|
|
180
|
+
6. **Clean up on shutdown** - Call `destroy()` when app closes
|
|
181
|
+
|
|
182
|
+
## Project Structure
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
src/
|
|
186
|
+
├── index.ts # Main exports
|
|
187
|
+
├── logger.ts # Core SDK class
|
|
188
|
+
├── types.ts # TypeScript definitions
|
|
189
|
+
├── http-client.ts # HTTP client
|
|
190
|
+
├── retry.ts # Retry logic
|
|
191
|
+
├── queue.ts # Batch queue
|
|
192
|
+
├── validators.ts # Input validation
|
|
193
|
+
└── constants.ts # Configuration constants
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Build & Development
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
# Install dependencies
|
|
200
|
+
npm install
|
|
201
|
+
|
|
202
|
+
# Build the SDK
|
|
203
|
+
npm run build
|
|
204
|
+
|
|
205
|
+
# Watch mode
|
|
206
|
+
npm run watch
|
|
207
|
+
|
|
208
|
+
# Run tests
|
|
209
|
+
npm test
|
|
210
|
+
|
|
211
|
+
# Lint code
|
|
212
|
+
npm run lint
|
|
213
|
+
|
|
214
|
+
# Format code
|
|
215
|
+
npm run format
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## License
|
|
219
|
+
|
|
220
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@avss-tech/logger",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "TypeScript client SDK for AVS Centralized Logging Platform",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": ["dist"],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"watch": "tsc --watch",
|
|
11
|
+
"clean": "rm -rf dist",
|
|
12
|
+
"prebuild": "npm run clean",
|
|
13
|
+
"test": "jest",
|
|
14
|
+
"test:watch": "jest --watch",
|
|
15
|
+
"lint": "eslint src/**/*.ts",
|
|
16
|
+
"format": "prettier --write src/**/*.ts"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"logging",
|
|
20
|
+
"audit",
|
|
21
|
+
"monitoring",
|
|
22
|
+
"typescript",
|
|
23
|
+
"sdk",
|
|
24
|
+
"avs"
|
|
25
|
+
],
|
|
26
|
+
"author": "Your Organization",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"uuid": "^9.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "^20.0.0",
|
|
33
|
+
"typescript": "^5.0.0",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
35
|
+
"@typescript-eslint/parser": "^6.0.0",
|
|
36
|
+
"eslint": "^8.0.0",
|
|
37
|
+
"prettier": "^3.0.0",
|
|
38
|
+
"jest": "^29.0.0",
|
|
39
|
+
"@types/jest": "^29.0.0",
|
|
40
|
+
"ts-jest": "^29.0.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=16.0.0"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"registry": "https://registry.npmjs.org",
|
|
48
|
+
"access": "public"
|
|
49
|
+
}
|
|
50
|
+
}
|