Introduction to ic-logger
The ic-logger is a powerful logging library used for capturing and managing application logs efficiently. It provides a wide range of features and APIs that help developers to easily integrate logging into their applications. This guide offers an in-depth look at ic-logger and showcases dozens of useful APIs with code snippets.
Installation
npm install ic-logger
Basic Usage
const Logger = require('ic-logger');
const logger = new Logger('app');
logger.info('This is an informational message');
logger.warn('This is a warning message');
logger.error('This is an error message');
Advanced Configurations
const Logger = require('ic-logger');
const options = {
level: 'debug',
format: 'json',
transports: ['console', 'file'],
fileOptions: {
filename: 'app.log',
maxSize: '10m'
}
};
const logger = new Logger('app', options);
logger.debug('This is a debug message');
logger.info('This is an informational message');
logger.warn('This is a warning message');
logger.error('This is an error message');
API Examples
logger.info('Starting the application...');
logger.warn('Low disk space');
logger.error('Unhandled Exception', { exception: new Error('Something went wrong') });
function processUserInput(input) {
logger.debug('Processing user input', { input });
try {
// Process input
} catch (error) {
logger.error('Error processing user input', { error });
}
}
logger.info('Application started successfully');
Logging Levels
logger.fatal('Fatal message');
logger.trace('Trace message');
logger.debug('Debug message');
logger.info('Info message');
logger.warn('Warn message');
logger.error('Error message');
logger.silent('Silent mode');
Custom Transports
const customTransport = {
log: (level, message) => {
// Custom log handling
}
};
const logger = new Logger('app', {
transports: [customTransport]
});
logger.info('Logging with custom transport');
Application Example
Here’s a complete example of an application integrating several ic-logger APIs:
const Logger = require('ic-logger');
const express = require('express');
const app = express();
const port = 3000;
const logger = new Logger('app', {
level: 'info',
transports: ['file', 'console'],
fileOptions: { filename: 'server.log' },
});
app.use((req, res, next) => {
logger.info('Received request', { method: req.method, url: req.url });
next();
});
app.get('/', (req, res) => {
logger.debug('Handling get request for root path');
res.send('Hello, World!');
});
app.listen(port, () => {
logger.info(`Server running at http://localhost:${port}/`);
});
By following this guide, you can make your application logging more efficient and comprehensive using the ic-logger.
Hash: 4027e9d720995dfdf57ce7c8f7cfe3c6b2fce5095f88babe0fbd6d5a8a7db32a