Introduction to Focus Logger
Focus Logger is a powerful and flexible logging library designed to enhance your JavaScript and Node.js applications. It allows developers to output logs in various formats, manage log levels, and provides dozens of useful APIs to cater to various logging needs.
API Examples
Basic Usage
const { createLogger, transports, format } = require('focus-logger'); const logger = createLogger({ level: 'info', format: format.combine( format.timestamp(), format.json() ), transports: [ new transports.Console(), new transports.File({ filename: 'combined.log' }) ] }); logger.info("Hello, Focus Logger!");
Custom Levels
const customLevels = { levels: { error: 0, warning: 1, info: 2, debug: 3 }, colors: { error: 'red', warning: 'yellow', info: 'green', debug: 'blue' } }; const customLogger = createLogger({ levels: customLevels.levels, transports: [new transports.Console({ format: format.colorize({ all: true }) })] }); customLogger.debug("This is a debug message");
Logging HTTP Requests
const express = require('express'); const app = express(); app.use((req, res, next) => { logger.info(`${req.method} ${req.url}`); next(); }); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(3000, () => logger.info('Server is running on port 3000'));
Customizing Formats
const myLogger = createLogger({ format: format.combine( format.label({ label: 'custom-label' }), format.timestamp(), format.printf(({ level, message, label, timestamp }) => { return `${timestamp} [${label}] ${level}: ${message}`; }) ), transports: [new transports.Console()] }); myLogger.error('This is a custom formatted error message');
Using External Transports
const { ElasticsearchTransport } = require('focus-logger-elasticsearch'); const esTransport = new ElasticsearchTransport({ level: 'info', clientOpts: { node: 'http://localhost:9200' } }); const esLogger = createLogger({ transports: [esTransport] }); esLogger.info('This message is sent to Elasticsearch');
Complete Application Example
const express = require('express'); const { createLogger, transports, format } = require('focus-logger'); const app = express(); const logger = createLogger({ level: 'info', format: format.combine( format.timestamp(), format.json() ), transports: [ new transports.Console(), new transports.File({ filename: 'app.log' }) ] }); app.use((req, res, next) => { logger.info({ message: 'HTTP Request', method: req.method, url: req.url }); next(); }); app.get('/', (req, res) => { res.send('Welcome to the Focus Logger App!'); }); app.listen(3000, () => logger.info('Application is running on port 3000'));
Focus Logger is highly customizable and can be enhanced with external transports like Elasticsearch, providing flexibility and power unmatched by simpler logging libraries.
Don’t miss out on leveraging Focus Logger to enhance your development workflow!
Hash: 953ab57a37c8ac4c6c442b7deef418f9f1ad09efa367f6e57339f414c95c7530