-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger-example.ts
More file actions
263 lines (210 loc) · 6.69 KB
/
logger-example.ts
File metadata and controls
263 lines (210 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* Logger Example
*
* This example demonstrates the configurable logging capabilities
* of ObjectStack Kernel that work in both Node.js and browser environments.
*
* Node.js: Uses Pino for high-performance structured logging
* Browser: Uses simple console-based logger
*/
import { ObjectKernel, createLogger, type Plugin, type PluginContext } from '@objectstack/core';
// Example 1: Kernel with Different Logger Configurations
async function exampleKernelLogging() {
console.log('\n=== Example 1: Kernel with Different Logger Configurations ===\n');
// Pretty format logger (colored output)
const kernel = new ObjectKernel({
logger: {
level: 'debug',
format: 'pretty'
}
});
const testPlugin: Plugin = {
name: 'test-plugin',
init: async (ctx: PluginContext) => {
ctx.logger.info('Plugin initialized', { version: '1.0.0' });
}
};
console.log('Starting kernel with pretty format:');
kernel.use(testPlugin);
await kernel.bootstrap();
await kernel.shutdown();
}
// Example 2: Standalone Logger Usage
async function exampleStandaloneLogger() {
console.log('\n=== Example 2: Standalone Logger Usage ===\n');
const logger = createLogger({
level: 'debug',
format: 'pretty',
sourceLocation: false
});
// Basic logging
logger.debug('Debug message for development');
logger.info('Application started');
logger.warn('Resource usage is high', { cpu: 85, memory: 90 });
// Error logging
try {
throw new Error('Something went wrong');
} catch (error) {
logger.error('Operation failed', error as Error, { operation: 'database-query' });
}
await logger.destroy();
}
// Example 3: Child Loggers with Context
async function exampleChildLoggers() {
console.log('\n=== Example 3: Child Loggers with Context ===\n');
const logger = createLogger({
level: 'info',
format: 'json'
});
// Create a child logger for API requests
const apiLogger = logger.child({
component: 'api',
version: 'v1'
});
// Create request-specific logger
const requestLogger = apiLogger.child({
requestId: 'req-123',
userId: 'user-456',
method: 'POST',
path: '/api/users'
});
requestLogger.info('Request received');
requestLogger.info('Processing request');
requestLogger.info('Request completed', { duration: 125 });
// Note: Only destroy the parent logger; child loggers share the same file writer
await logger.destroy();
}
// Example 4: Distributed Tracing
async function exampleDistributedTracing() {
console.log('\n=== Example 4: Distributed Tracing ===\n');
const logger = createLogger({
level: 'info',
format: 'json'
});
// Simulate distributed trace
const traceId = 'trace-abc-123';
const spanId = 'span-xyz-789';
const tracedLogger = logger.withTrace(traceId, spanId);
tracedLogger.info('Starting distributed operation');
tracedLogger.info('Calling remote service');
tracedLogger.info('Operation completed');
await logger.destroy();
}
// Example 5: Sensitive Data Redaction
async function exampleSensitiveDataRedaction() {
console.log('\n=== Example 5: Sensitive Data Redaction ===\n');
const logger = createLogger({
level: 'info',
format: 'json',
redact: ['password', 'token', 'apiKey', 'creditCard']
});
// Sensitive data will be automatically redacted
logger.info('User login attempt', {
username: 'john.doe',
password: 'super-secret-123', // Will be redacted
email: 'john@example.com'
});
logger.info('API call', {
endpoint: '/api/payment',
apiKey: 'sk_live_123456789', // Will be redacted
amount: 100
});
logger.info('Payment processing', {
userId: 'user-123',
creditCard: '4111-1111-1111-1111', // Will be redacted
amount: 99.99
});
await logger.destroy();
}
// Example 6: Plugin with Logger
async function examplePluginLogging() {
console.log('\n=== Example 6: Plugin with Logger ===\n');
const databasePlugin: Plugin = {
name: 'database',
init: async (ctx: PluginContext) => {
ctx.logger.info('Connecting to database', {
host: 'localhost',
port: 5432,
database: 'myapp'
});
// Simulate connection
await new Promise(resolve => setTimeout(resolve, 100));
const db = { connected: true };
ctx.registerService('db', db);
ctx.logger.info('Database connected successfully');
},
start: async (ctx: PluginContext) => {
ctx.logger.info('Database plugin started');
},
destroy: async () => {
console.log('[Database] Disconnecting...');
}
};
const apiPlugin: Plugin = {
name: 'api',
dependencies: ['database'],
init: async (ctx: PluginContext) => {
const db = ctx.getService('db') as { connected: boolean };
ctx.logger.info('API plugin initialized', { dbConnected: db.connected });
ctx.registerService('api', { server: 'http://localhost:3000' });
},
start: async (ctx: PluginContext) => {
ctx.logger.info('API server starting', { port: 3000 });
ctx.logger.info('API server ready');
}
};
const kernel = new ObjectKernel({
logger: {
level: 'info',
format: 'pretty'
}
});
kernel.use(databasePlugin);
kernel.use(apiPlugin);
await kernel.bootstrap();
await kernel.shutdown();
}
// Example 7: Different Log Formats
async function exampleLogFormats() {
console.log('\n=== Example 7: Different Log Formats ===\n');
const message = 'User action';
const metadata = { userId: '123', action: 'create', resource: 'document' };
console.log('JSON format:');
const jsonLogger = createLogger({ format: 'json' });
jsonLogger.info(message, metadata);
await jsonLogger.destroy();
console.log('\nText format:');
const textLogger = createLogger({ format: 'text' });
textLogger.info(message, metadata);
await textLogger.destroy();
console.log('\nPretty format:');
const prettyLogger = createLogger({ format: 'pretty' });
prettyLogger.info(message, metadata);
await prettyLogger.destroy();
}
// Run all examples
async function main() {
console.log('ObjectStack Logger Examples');
console.log('============================\n');
await exampleKernelLogging();
await exampleStandaloneLogger();
await exampleChildLoggers();
await exampleDistributedTracing();
await exampleSensitiveDataRedaction();
await examplePluginLogging();
await exampleLogFormats();
console.log('\n✅ All examples completed!\n');
}
// Run if executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(console.error);
}
export {
exampleKernelLogging,
exampleStandaloneLogger,
exampleChildLoggers,
exampleDistributedTracing,
exampleSensitiveDataRedaction,
examplePluginLogging,
exampleLogFormats
};