Introduction to Capmonster-logger
The capmonster-logger is a versatile and robust logging library that helps developers manage their application logs efficiently. This guide introduces capmonster-logger and its various APIs with practical code snippets, ensuring you can seamlessly integrate it into your project.
Getting Started
import CapmonsterLogger from 'capmonster-logger'; const logger = new CapmonsterLogger({ level: 'info', format: 'json' }); logger.info('This is an info log'); logger.error('This is an error log');
Logging Levels
logger.debug('This is a debug log'); logger.warn('This is a warning log'); logger.fatal('This is a fatal log');
Custom Log Formats
const customLogger = new CapmonsterLogger({ level: 'info', format: winston.format.combine( winston.format.colorize(), winston.format.timestamp(), winston.format.printf(({ timestamp, level, message }) => { return `${timestamp} ${level}: ${message}`; }) ), }); customLogger.info('This is a custom formatted info log');
Enabling File Logging
const fileLogger = new CapmonsterLogger({ level: 'info', transports: [ new winston.transports.File({ filename: 'app.log' }) ] }); fileLogger.info('This log will be written to app.log file');
Integrating with Express
const express = require('express'); const app = express(); app.use((req, res, next) => { logger.info(`Request URL: ${req.url}`); next(); }); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(3000, () => { logger.info('App listening on port 3000'); });
Capturing Unhandled Exceptions
const exceptionLogger = new CapmonsterLogger({ level: 'error', transports: [ new winston.transports.Console() ], exceptionHandlers: [ new winston.transports.File({ filename: 'exceptions.log' }) ] }); throw new Error('This is an unhandled exception');
By leveraging these powerful APIs of capmonster-logger, you can ensure comprehensive and efficient logging mechanisms in your application, enhancing debugging and monitoring capabilities.
Hash: 09b16a87fccd2353892bdd99f6c5f127201b8599cc27b3eae0d1f1fb320260c7