Introducing Magnus Logger
Magnus Logger is a powerful and versatile logging library designed to provide developers with an easy-to-use tool for managing and analyzing logs in their applications. With dozens of useful API methods, Magnus Logger can cater to various logging needs, ensuring that capturing, categorizing, and retrieving log information becomes a seamless process.
Getting Started
To start using Magnus Logger, you need to install it via your package manager. For example:
npm install magnus-logger
Basic Configuration
Here’s how you can set up Magnus Logger with basic configuration:
const logger = require('magnus-logger');
// Setting up the basic configuration
const config = {
level: 'info',
format: 'json',
transports: ['console', 'file'],
fileOptions: {
filename: 'app.log'
}
};
logger.configure(config);
API Methods
1. Logging Messages
Capture different levels of log messages using:
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error('This is an error message');
2. Dynamic Log Levels
logger.setLevel('debug');
logger.debug('This is a debug message');
3. Custom Formats
Create custom formats to structure log output:
logger.setFormat((info) => {
return `${info.timestamp} ${info.level.toUpperCase()}: ${info.message}`;
});
4. Tagged Logging
Attaching tags to logs:
logger.info('User logged in', { tags: ['auth', 'user'] });
5. Conditional Logging
logger.info('This log will appear only if condition is true', { condition: true });
6. Log Rotation
const rotatingConfig = {
level: 'info',
format: 'json',
transports: ['rotatingFile'],
fileOptions: {
filename: 'app.log',
maxFiles: 5,
maxSize: '10m'
}
};
logger.configure(rotatingConfig);
App Example
Below is an example of a simple application utilizing Magnus Logger:
const express = require('express');
const bodyParser = require('body-parser');
const logger = require('magnus-logger');
// Configure the logger
const config = {
level: 'info',
format: 'json',
transports: ['console', 'file'],
fileOptions: {
filename: 'app.log'
}
};
logger.configure(config);
const app = express();
app.use(bodyParser.json());
// Middleware to log all incoming requests
app.use((req, res, next) => {
logger.info(`Incoming request on ${req.path}`);
next();
});
// Sample route
app.get('/', (req, res) => {
logger.info('Handling GET request for root route');
res.send('Hello, world!');
});
// Start the server
app.listen(3000, () => {
logger.info('Server started on port 3000');
});
By integrating Magnus Logger into your applications, you can drastically improve logging efficiency, gain better insights, and enhance the debugging process.
Hash: 0c8437d5bb3d6db30138db8af808eb4350c923dc2e0c64595869a0010f11e976