Unlock the Full Potential of Logging with All-in-One Logger
The all-in-one-logger is a comprehensive and versatile logging library designed to simplify application monitoring and troubleshooting. It offers a wide variety of APIs to cater to different logging needs. In this article, we’ll explore the extensive capabilities of this library with practical examples and a full application example.
Basic Usage
Initialize a logger instance and start logging messages with different severity levels:
const logger = require('all-in-one-logger');
const log = logger.createLogger();
log.info('This is an info message');
log.warn('This is a warning');
log.error('This is an error');
log.debug('This is a debug message');
log.trace('This is a trace message');
Custom Formats and Transports
Customize log formats and add multiple transports to direct logs to different destinations:
const { transports, format } = require('all-in-one-logger');
const log = logger.createLogger({
format: format.combine(
format.timestamp(),
format.json()
),
transports: [
new transports.Console(),
new transports.File({ filename: 'app.log' })
]
});
log.info('Information with custom format');
Log Rotation
Implement log rotation to manage log file size and maintain performance:
const { transports } = require('all-in-one-logger');
const log = logger.createLogger({
transports: [
new transports.File({
filename: 'app.log',
maxsize: 10485760,
maxFiles: 5
})
]
});
log.info('Log message with rotation');
HTTP Transport
Send logs over HTTP to a remote server or logging service:
const axios = require('axios');
const { transports } = require('all-in-one-logger');
const log = logger.createLogger({
transports: [
new transports.Http({
url: 'https://example.com/logs',
axios
})
]
});
log.info('Log message to HTTP server');
Application Example
Here’s a simple node.js application using various all-in-one-logger APIs:
const express = require('express');
const logger = require('all-in-one-logger');
const app = express();
const port = 3000;
const log = logger.createLogger({
format: logger.format.json(),
transports: [
new logger.transports.Console(),
new logger.transports.File({ filename: 'app.log' })
]
});
app.use((req, res, next) => {
log.info(`Request: ${req.method} ${req.url}`);
next();
});
app.get('/', (req, res) => {
res.send('Hello, World!');
log.debug('Responded to /');
});
app.listen(port, () => {
log.info(`Server running at http://localhost:${port}`);
});
In this example, we set up a basic Express server with all-in-one-logger to log incoming requests and server responses.
Hash: 3e55be618532faed46b0517ebc1f8a4365f9ac8fb03589e9a06363a01083809f