0
# Basic Logging
1
2
loglevel provides five core logging methods that offer reliable output across all JavaScript environments with proper fallbacks and preserved line numbers.
3
4
## Capabilities
5
6
### trace
7
8
```javascript { .api }
9
/**
10
* Output trace message with full stack trace
11
* This will include a full stack trace in supported environments
12
* @param {...any} msg - Any data to log to the console
13
*/
14
trace(...msg: any[]): void;
15
```
16
17
**Usage Examples:**
18
19
```javascript
20
import log from 'loglevel';
21
22
log.setLevel('trace');
23
log.trace('Entering function processData()');
24
log.trace('Processing item:', { id: 123, name: 'example' });
25
log.trace('Variable state:', { count: 42, active: true });
26
```
27
28
### debug
29
30
```javascript { .api }
31
/**
32
* Output debug message to console
33
* Maps to console.debug() if available, otherwise console.log()
34
* @param {...any} msg - Any data to log to the console
35
*/
36
debug(...msg: any[]): void;
37
```
38
39
**Usage Examples:**
40
41
```javascript
42
import log from 'loglevel';
43
44
log.setLevel('debug');
45
log.debug('Cache miss for key:', 'user_123');
46
log.debug('API response:', { status: 200, data: [...] });
47
log.debug('Performance metric:', { duration: 245, memory: '12MB' });
48
```
49
50
### log
51
52
```javascript { .api }
53
/**
54
* Alias for the debug method
55
* Provides familiar console.log-style interface
56
* @param {...any} msg - Any data to log to the console
57
*/
58
log(...msg: any[]): void;
59
```
60
61
**Usage Examples:**
62
63
```javascript
64
import logger from 'loglevel';
65
66
logger.setLevel('debug');
67
logger.log('This is equivalent to debug()');
68
logger.log('User action:', 'clicked_button', { buttonId: 'submit' });
69
```
70
71
### info
72
73
```javascript { .api }
74
/**
75
* Output informational message
76
* Maps to console.info() if available, otherwise console.log()
77
* @param {...any} msg - Any data to log to the console
78
*/
79
info(...msg: any[]): void;
80
```
81
82
**Usage Examples:**
83
84
```javascript
85
import log from 'loglevel';
86
87
log.setLevel('info');
88
log.info('Application started successfully');
89
log.info('User logged in:', { userId: 456, username: 'john_doe' });
90
log.info('Configuration loaded:', { theme: 'dark', locale: 'en-US' });
91
```
92
93
### warn
94
95
```javascript { .api }
96
/**
97
* Output warning message
98
* Maps to console.warn() if available, otherwise console.log()
99
* @param {...any} msg - Any data to log to the console
100
*/
101
warn(...msg: any[]): void;
102
```
103
104
**Usage Examples:**
105
106
```javascript
107
import log from 'loglevel';
108
109
log.setLevel('warn');
110
log.warn('Deprecated API usage detected');
111
log.warn('Low disk space:', { available: '100MB', threshold: '500MB' });
112
log.warn('Rate limit approaching:', { requests: 95, limit: 100 });
113
```
114
115
### error
116
117
```javascript { .api }
118
/**
119
* Output error message
120
* Maps to console.error() if available, otherwise console.log()
121
* @param {...any} msg - Any data to log to the console
122
*/
123
error(...msg: any[]): void;
124
```
125
126
**Usage Examples:**
127
128
```javascript
129
import log from 'loglevel';
130
131
log.setLevel('error');
132
log.error('Database connection failed');
133
log.error('Validation error:', { field: 'email', message: 'Invalid format' });
134
log.error('Unhandled exception:', error.stack);
135
```
136
137
## Environment Behavior
138
139
loglevel automatically adapts to different environments:
140
141
- **Modern Browsers**: Uses appropriate console methods (console.trace, console.debug, etc.)
142
- **Old Browsers**: Falls back to console.log or no-op if console unavailable
143
- **Node.js**: Works with Node's console implementation
144
- **IE8/9**: Handles delayed console availability when dev tools are opened
145
146
```javascript
147
// Graceful handling - never throws errors
148
log.info('This works everywhere');
149
150
// Preserves line numbers and stack traces
151
log.debug('Check browser dev tools for accurate source location');
152
```