0
# Default Logger
1
2
Direct logging methods available on the winston module for quick access without creating logger instances.
3
4
## Capabilities
5
6
### Direct Logging Methods
7
8
Winston provides a default logger instance accessible directly through the winston module, allowing for immediate logging without explicit logger creation.
9
10
```javascript { .api }
11
/**
12
* Core logging method using default logger
13
* @param level - Log level string
14
* @param message - Log message
15
* @param meta - Additional metadata
16
* @returns Default logger instance
17
*/
18
function log(level: string, message: string, ...meta: any[]): Logger;
19
20
/**
21
* Log at error level using default logger
22
* @param message - Error message
23
* @param meta - Additional metadata
24
* @returns Default logger instance
25
*/
26
function error(message: string, ...meta: any[]): Logger;
27
28
/**
29
* Log at warn level using default logger
30
* @param message - Warning message
31
* @param meta - Additional metadata
32
* @returns Default logger instance
33
*/
34
function warn(message: string, ...meta: any[]): Logger;
35
36
/**
37
* Log at info level using default logger
38
* @param message - Info message
39
* @param meta - Additional metadata
40
* @returns Default logger instance
41
*/
42
function info(message: string, ...meta: any[]): Logger;
43
44
/**
45
* Log at http level using default logger
46
* @param message - HTTP-related message
47
* @param meta - Additional metadata
48
* @returns Default logger instance
49
*/
50
function http(message: string, ...meta: any[]): Logger;
51
52
/**
53
* Log at verbose level using default logger
54
* @param message - Verbose message
55
* @param meta - Additional metadata
56
* @returns Default logger instance
57
*/
58
function verbose(message: string, ...meta: any[]): Logger;
59
60
/**
61
* Log at debug level using default logger
62
* @param message - Debug message
63
* @param meta - Additional metadata
64
* @returns Default logger instance
65
*/
66
function debug(message: string, ...meta: any[]): Logger;
67
68
/**
69
* Log at silly level using default logger
70
* @param message - Silly level message
71
* @param meta - Additional metadata
72
* @returns Default logger instance
73
*/
74
function silly(message: string, ...meta: any[]): Logger;
75
76
// CLI-specific level methods (available when using CLI levels)
77
/**
78
* Log at help level using default logger (CLI levels)
79
* @param message - Help message
80
* @param meta - Additional metadata
81
* @returns Default logger instance
82
*/
83
function help(message: string, ...meta: any[]): Logger;
84
85
/**
86
* Log at data level using default logger (CLI levels)
87
* @param message - Data message
88
* @param meta - Additional metadata
89
* @returns Default logger instance
90
*/
91
function data(message: string, ...meta: any[]): Logger;
92
93
/**
94
* Log at prompt level using default logger (CLI levels)
95
* @param message - Prompt message
96
* @param meta - Additional metadata
97
* @returns Default logger instance
98
*/
99
function prompt(message: string, ...meta: any[]): Logger;
100
101
/**
102
* Log at input level using default logger (CLI levels)
103
* @param message - Input message
104
* @param meta - Additional metadata
105
* @returns Default logger instance
106
*/
107
function input(message: string, ...meta: any[]): Logger;
108
109
// Syslog-specific level methods (available when using syslog levels)
110
/**
111
* Log at emergency level using default logger (syslog levels)
112
* @param message - Emergency message
113
* @param meta - Additional metadata
114
* @returns Default logger instance
115
*/
116
function emerg(message: string, ...meta: any[]): Logger;
117
118
/**
119
* Log at alert level using default logger (syslog levels)
120
* @param message - Alert message
121
* @param meta - Additional metadata
122
* @returns Default logger instance
123
*/
124
function alert(message: string, ...meta: any[]): Logger;
125
126
/**
127
* Log at critical level using default logger (syslog levels)
128
* @param message - Critical message
129
* @param meta - Additional metadata
130
* @returns Default logger instance
131
*/
132
function crit(message: string, ...meta: any[]): Logger;
133
134
/**
135
* Log at warning level using default logger (syslog levels)
136
* @param message - Warning message
137
* @param meta - Additional metadata
138
* @returns Default logger instance
139
*/
140
function warning(message: string, ...meta: any[]): Logger;
141
142
/**
143
* Log at notice level using default logger (syslog levels)
144
* @param message - Notice message
145
* @param meta - Additional metadata
146
* @returns Default logger instance
147
*/
148
function notice(message: string, ...meta: any[]): Logger;
149
```
150
151
**Usage Examples:**
152
153
```javascript
154
const winston = require('winston');
155
156
// Direct logging using default logger
157
winston.error('Application error occurred');
158
winston.warn('This is a warning message');
159
winston.info('Application started successfully');
160
winston.debug('Debug information');
161
162
// With metadata
163
winston.error('Database connection failed', {
164
error: 'ECONNREFUSED',
165
host: 'localhost',
166
port: 5432
167
});
168
169
winston.info('User login', {
170
userId: 123,
171
username: 'john_doe',
172
timestamp: new Date().toISOString()
173
});
174
```
175
176
### Default Logger Management
177
178
Methods for managing the default logger's transports and configuration.
179
180
```javascript { .api }
181
/**
182
* Add a transport to the default logger
183
* @param transport - Transport instance to add
184
* @returns Default logger instance
185
*/
186
function add(transport: Transport): Logger;
187
188
/**
189
* Remove a transport from the default logger
190
* @param transport - Transport instance to remove
191
* @returns Default logger instance
192
*/
193
function remove(transport: Transport): Logger;
194
195
/**
196
* Remove all transports from the default logger
197
* @returns Default logger instance
198
*/
199
function clear(): Logger;
200
201
/**
202
* Reconfigure the default logger
203
* @param options - New configuration options
204
*/
205
function configure(options: LoggerOptions): void;
206
207
/**
208
* Create a child logger from the default logger
209
* @param defaultMeta - Additional metadata for child logger
210
* @returns Child logger instance
211
*/
212
function child(options: object): Logger;
213
```
214
215
**Usage Examples:**
216
217
```javascript
218
const winston = require('winston');
219
220
// Add console transport to default logger
221
winston.add(new winston.transports.Console({
222
format: winston.format.simple()
223
}));
224
225
// Add file transport
226
winston.add(new winston.transports.File({
227
filename: 'app.log'
228
}));
229
230
// Configure default logger
231
winston.configure({
232
level: 'debug',
233
format: winston.format.combine(
234
winston.format.timestamp(),
235
winston.format.json()
236
),
237
transports: [
238
new winston.transports.Console(),
239
new winston.transports.File({ filename: 'combined.log' })
240
]
241
});
242
243
// Create child logger
244
const childLogger = winston.child({ service: 'auth' });
245
childLogger.info('Authentication successful');
246
```
247
248
### Query and Streaming
249
250
Methods for querying logged messages and creating streams from the default logger.
251
252
```javascript { .api }
253
/**
254
* Query logged messages from the default logger
255
* @param options - Query options
256
* @param callback - Callback function for results
257
* @returns Query results or promise
258
*/
259
function query(
260
options?: QueryOptions,
261
callback?: (err: Error, results: any) => void
262
): any;
263
264
/**
265
* Create a readable stream of log messages from default logger
266
* @param options - Stream options
267
* @returns Readable stream of log messages
268
*/
269
function stream(options?: any): ReadableStream;
270
271
interface QueryOptions {
272
/** Number of rows to return */
273
rows?: number;
274
/** Limit number of results */
275
limit?: number;
276
/** Starting index */
277
start?: number;
278
/** Start date filter */
279
from?: Date;
280
/** End date filter */
281
until?: Date;
282
/** Sort order ('asc' or 'desc') */
283
order?: 'asc' | 'desc';
284
/** Fields to include */
285
fields?: any;
286
}
287
```
288
289
**Usage Examples:**
290
291
```javascript
292
const winston = require('winston');
293
294
// Query recent error logs
295
winston.query({
296
from: new Date(Date.now() - 24 * 60 * 60 * 1000), // Last 24 hours
297
until: new Date(),
298
limit: 10,
299
start: 0,
300
order: 'desc'
301
}, (err, results) => {
302
if (err) {
303
console.error('Query failed:', err);
304
} else {
305
console.log('Recent logs:', results);
306
}
307
});
308
309
// Create log stream
310
const logStream = winston.stream({ start: -1 });
311
logStream.on('log', (log) => {
312
console.log('New log entry:', log);
313
});
314
```
315
316
### Default Logger Properties
317
318
Global properties available on the winston module for accessing default logger state.
319
320
```javascript { .api }
321
/**
322
* Current log level of default logger
323
*/
324
let level: string;
325
326
/**
327
* Exception handler instance for default logger
328
*/
329
let exceptions: ExceptionHandler;
330
331
/**
332
* Rejection handler instance for default logger
333
*/
334
let rejections: RejectionHandler;
335
336
/**
337
* Exit behavior on error for default logger
338
*/
339
let exitOnError: Function | boolean;
340
341
/**
342
* Access to default logger's transports and handlers
343
*/
344
let default: {
345
exceptionHandlers: Transport[];
346
rejectionHandlers: Transport[];
347
transports: Transport[];
348
};
349
```
350
351
**Usage Examples:**
352
353
```javascript
354
const winston = require('winston');
355
356
// Check current log level
357
console.log('Current level:', winston.level);
358
359
// Set log level
360
winston.level = 'debug';
361
362
// Configure exit behavior
363
winston.exitOnError = false;
364
365
// Access default logger transports
366
console.log('Active transports:', winston.default.transports.length);
367
368
// Handle exceptions with default logger
369
winston.exceptions.handle(
370
new winston.transports.File({ filename: 'exceptions.log' })
371
);
372
373
// Handle rejections with default logger
374
winston.rejections.handle(
375
new winston.transports.File({ filename: 'rejections.log' })
376
);
377
```
378
379
## Important Notes
380
381
- The default logger starts with **no transports** configured, which may lead to high memory usage if logs are generated without output destinations
382
- Always add at least one transport to the default logger before logging:
383
384
```javascript
385
const winston = require('winston');
386
387
// Add a transport before logging
388
winston.add(new winston.transports.Console());
389
390
// Now logging will work properly
391
winston.info('This will be output to console');
392
```
393
394
- For production applications, consider creating dedicated logger instances rather than using the default logger for better control and isolation:
395
396
```javascript
397
// Preferred approach for production
398
const logger = winston.createLogger({
399
level: 'info',
400
format: winston.format.json(),
401
transports: [
402
new winston.transports.File({ filename: 'app.log' })
403
]
404
});
405
406
logger.info('Application started');
407
```