DEV Community

Caleb Okpara
Caleb Okpara

Posted on

Simplify File Logging in Node.js with F-Log

Introduction:
Logging is an essential aspect of any application, allowing developers to track events, errors, and important information. To simplify file logging in Node.js, I've created F-Log, a versatile npm package. With F-Log, you can effortlessly incorporate robust logging capabilities into your projects, making debugging and troubleshooting a breeze.

Installation:
To get started with F-Log, simply install the package using npm:

npm i @drantaz/f-log
Enter fullscreen mode Exit fullscreen mode

Usage:
Once installed, you can use F-Log to create logs with ease. Here are a few examples of how you can use the package:

  1. Creating a Single Log: To create a single log, use the log function:
import { log } from '@drantaz/f-log';

log('Hello World', 'info');
// This will print 'Hello World' to the console 
// in a blue color and save it to a file named 
// 'logs.log'
Enter fullscreen mode Exit fullscreen mode

The third parameter of the log function is optional and can be used to specify whether to persist the log to the file. Setting it to false will prevent the log from being saved.

  1. Logging Multiple Texts: If you need to log multiple texts at once, you can use the logAll function:
import { logAll } from '@drantaz/f-log';

logAll('info', true, 'Log 1', 'Log 2', 'Log 3', ...);
// This will log each text and save them in 
// the 'logs.log' file
Enter fullscreen mode Exit fullscreen mode

Similar to the log function, the second parameter of logAll can be used to control whether the logs should be persisted to the file.

Reading Logs:
To retrieve your logs, you can use the getLogs function:

import { getLogs } from '@drantaz/f-log';

// Get all logs without grouping
const logs = getLogs(false);

// Get all logs with grouping
const logs = getLogs();

// Get logs by title without grouping
const logs = getLogs(false, 'info');

// Get logs by title with grouping
const logs = getLogs(true, 'info');
Enter fullscreen mode Exit fullscreen mode

By default, the logs are returned as an array. You can customize the output by specifying the grouping parameter and filtering logs by title.

Configuration:
To customize the configuration of F-Log, you can create a file called f-log.json at the root of your project. Here's an example of how you can override the log file path:

{
  "path": "./src/logs"
}
Enter fullscreen mode Exit fullscreen mode

Future Releases:
I am actively working on adding more configuration options and enhancements to F-Log. Stay tuned for future releases to unlock even more functionality.

Log Types:
F-Log supports various log types, each represented by a specific color:

Status Color
info blue
success green
warn orange
error red
critical darkred

Conclusion:
With F-Log, you can simplify file logging in your Node.js applications. By easily incorporating robust logging capabilities, you can streamline debugging and gain valuable insights into the behavior of your application. Give F-Log a try and take your logging to the next level!

https://www.npmjs.com/package/@drantaz/f-log


Top comments (0)