Introducing reliable-logger, the all-in-one logging solution designed to meet the diverse needs of modern applications. Whether you’re building a web app, mobile app, or any other type of software, reliable-logger has you covered with its user-friendly and feature-rich API.
Understanding the Basics of Reliable Logger
Reliable Logger is a versatile and powerful logging library that allows you to track events, errors, and other significant data points in your applications. It’s designed with simplicity and robustness in mind, making it an ideal choice for developers looking for a dependable logging tool.
Basic Setup
Setting up Reliable Logger is straightforward. You can install it via npm:
npm install reliable-logger
Once installed, you can import and initialize the logger:
const reliableLogger = require('reliable-logger');
const logger = reliableLogger.createLogger({
level: 'info',
transports: [
new reliableLogger.transports.Console(),
new reliableLogger.transports.File({ filename: 'app.log' })
]
});
API Examples
Logging Different Levels
Reliable Logger supports various logging levels – error, warn, info, verbose, debug, and silly:
logger.error('This is an error message');
logger.warn('This is a warning message');
logger.info('This is an info message');
logger.verbose('This is a verbose message');
logger.debug('This is a debug message');
logger.silly('This is a silly message');
Custom Transports
Create and use custom transports:
class CustomTransport {
log(info, callback) {
setImmediate(() => this.emit('logged', info));
console.log(`Custom log: ${info.message}`);
callback();
}
}
logger.add(new CustomTransport());
Handling Exceptions
Catch and log errors using the error handlers:
process.on('uncaughtException', (error) => {
logger.error(`Uncaught Exception: ${error.message}`);
});
process.on('unhandledRejection', (reason, promise) => {
logger.warn(`Unhandled Rejection at: ${promise}, reason: ${reason}`);
});
Stream Data to External Services
An example of streaming log data to an external service:
const http = require('http');
const request = http.request({
hostname: 'log-server.com',
port: 80,
path: '/logs',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
logger.on('data', (log) => {
request.write(JSON.stringify(log));
});
logger.on('finish', () => {
request.end();
});
Complete App Example
Here’s a full example of a Node.js application using Reliable Logger to log various events:
const express = require('express');
const reliableLogger = require('reliable-logger');
const app = express();
const logger = reliableLogger.createLogger({
level: 'info',
transports: [
new reliableLogger.transports.Console(),
new reliableLogger.transports.File({ filename: 'app.log' })
]
});
app.use((req, res, next) => {
logger.info(`${req.method} ${req.url}`);
next();
});
app.get('/', (req, res) => {
logger.verbose('Handling root request');
res.send('Hello, world!');
});
app.listen(3000, () => {
logger.info('Server is running on port 3000');
});
Reliable Logger is your go-to logging solution for creating reliable and maintainable applications. It offers a seamless and intuitive API that fits all your logging needs, from basic logging to advanced custom solutions.
Hash: 0ca1e907850c63a79432467aa5838496f29fe19d1417ba750bcf6cabdbae026e