Discover the Power of lighthouse-logger
Logging is an essential part of any application. With lighthouse-logger
, you can streamline your logging process and gain better insights into your application’s behavior. This guide will introduce you to lighthouse-logger
and provide comprehensive API explanations along with useful code snippets.
Getting Started with lighthouse-logger
To get started, install the lighthouse-logger
package via npm:
npm install lighthouse-logger
Basic Usage
Here is a basic example of how to use lighthouse-logger
:
const logger = require('lighthouse-logger');
logger.info('Info message'); logger.warn('Warn message'); logger.error('Error message');
Creating a Logger with Custom Configuration
You can create a logger instance with custom configurations:
const { createLogger } = require('lighthouse-logger');
const customLogger = createLogger({
level: 'debug',
format: combine(
timestamp(),
printf(info => `${info.timestamp}: ${info.level.toUpperCase()}: ${info.message}`)
),
transports: [
new transports.Console(),
new transports.File({ filename: 'app.log' })
]
});
customLogger.debug('Debug message'); customLogger.info('Info message');
Logging HTTP Requests
The httpLogger
middleware integrates seamlessly with Express applications to log HTTP requests:
const express = require('express'); const { httpLogger } = require('lighthouse-logger'); const app = express();
app.use(httpLogger());
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Logging Middleware
The loggingMiddleware
can be added to any middleware stack to log process flow:
const { loggingMiddleware } = require('lighthouse-logger'); app.use(loggingMiddleware);
Handling Multiple Logger Instances
If you need to handle multiple logger instances in different parts of your application, here is an example:
const logger1 = createLogger({ level: 'info', transports: [new transports.Console()] }); const logger2 = createLogger({ level: 'error', transports: [new transports.File({ filename: 'error.log' })] });
logger1.info('This is an info message'); logger2.error('This is an error message');
Full Application Example
Below is a full application example that demonstrates the use of several lighthouse-logger
APIs:
const express = require('express'); const { createLogger, httpLogger, loggingMiddleware } = require('lighthouse-logger');
const app = express();
const logger = createLogger({
level: 'debug',
format: combine(
timestamp(),
printf(info => `${info.timestamp}: ${info.level.toUpperCase()}: ${info.message}`)
),
transports: [
new transports.Console(),
new transports.File({ filename: 'app.log' })
]
});
app.use(httpLogger());
app.use(loggingMiddleware);
app.get('/', (req, res) => {
logger.info('Root endpoint hit');
res.send('Hello World!');
});
app.listen(3000, () => {
logger.debug('Server running on port 3000');
});
With lighthouse-logger
, you can make your logging more effective and your debugging process smoother. Try integrating it into your project today!
Hash: c385a62f856c3780242eb8f6fe6cd8c4dd91873312d7c34180b5840249a0de24