Introduction to solid-logger
solid-logger
is a powerful, flexible logging library for JavaScript applications. Whether you are building an enterprise-level software or a small project, solid-logger
can be tailored to fit your needs, offering a wide range of APIs that can be easily integrated.
Key Features of solid-logger
- Flexible configuration options
- Support for multiple logging levels
- Customizable log formats
- Asynchronous logging
- Integration with various transport layers
Basic Usage
Let’s start with a simple example of how to use solid-logger
:
const logger = require('solid-logger');
// Basic configuration
logger.configure({
level: 'info',
format: 'json',
transports: [
new logger.transports.Console(),
new logger.transports.File({ filename: 'logs/app.log' })
]
});
logger.info('This is an info message');
logger.error('This is an error message');
Advanced API Examples
Custom Log Levels
const customLevels = {
levels: {
fatal: 0,
error: 1,
warn: 2,
info: 3,
debug: 4,
trace: 5
},
colors: {
fatal: 'red',
error: 'yellow',
warn: 'yellow',
info: 'green',
debug: 'blue',
trace: 'magenta'
}
};
logger.configure({
levels: customLevels.levels,
transports: [
new logger.transports.Console({
format: logger.format.combine(
logger.format.colorize(),
logger.format.simple()
)
})
]
});
logger.log('fatal', 'This is a fatal message');
Adding Metadata
logger.configure({
level: 'info',
format: logger.format.combine(
logger.format.timestamp(),
logger.format.json()
),
defaultMeta: { service: 'user-service' },
transports: [
new logger.transports.File({ filename: 'logs/combined.log' })
]
});
logger.info('User has registered', { userId: 123 });
Asynchronous Logging
const asyncTransport = new logger.transports.Console({
format: logger.format.simple(),
level: 'info',
handleExceptions: true
});
asyncTransport.log = async (info, callback) => {
setImmediate(() => {
async function sendLog() {
await sendToRemoteServer(info);
callback();
}
sendLog();
});
};
logger.add(asyncTransport);
logger.info('This is an async log message');
Building an App with solid-logger
Below is a sample application that demonstrates the usage of solid-logger
:
const express = require('express');
const logger = require('solid-logger');
const app = express();
// Logger configuration
logger.configure({
level: 'debug',
format: logger.format.combine(
logger.format.timestamp(),
logger.format.json()
),
transports: [
new logger.transports.Console(),
new logger.transports.File({ filename: 'logs/app.log' })
]
});
app.use((req, res, next) => {
logger.info({ message: 'Incoming request', url: req.url, method: req.method });
next();
});
app.get('/api', (req, res) => {
logger.debug('Handling /api route');
res.send('API response');
});
app.listen(3000, () => {
logger.info('Server is running on port 3000');
});
Integrating solid-logger
into your application is straightforward and enhances your ability to monitor and troubleshoot your application.
Hash: 3431e3be29e57b5fafd3cd10f0f98103085891cb7e426ae96ebff006020c5ad2