1.0.1 • Published 5 months ago

@nabinsaru/j-logger v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Custom Logger

A custom logger utility for Node.js with configurable styles and features. Allows the log saving to the folder path.

Installation

npm i @nabinsaru/j-logger

### Installation Development
```bash
# Clone the repository
git clone git@github.com:NabinSaru/j-logger.git
cd J-LOGGER

# Install dependencies
npm install

Configuration

The logger can be configured using a JSON file(Optional). Fields are also optionals. Create a j-logger.config.json file with the following structure:

{
  "color": "White",
  "backgroundColor": "Black",
  "saveLog": true,
  "logPath": "/log/j-logger.log",
  "textFormat": ["Bold", "Underline"],
  "stylizedMode": true
}

Available methods

  • getLogTypes() => get all available middleware log formats
  • TextColors() => available text colors
  • BgColors() => available background colors
  • BrightColors() => available bright text colors
  • FormattingOptions() => available text formatting options

Available Properties

  • SavePath(path_string)
  • TextColor(color_string)
  • BackgroundColor(color_string)
  • TextFormat(formats_string_array)
  • SaveLog(boolean) >>> This takes j-logger.log as default path if not configured.

Usage

// Import the JLogger class and other required modules
import { JLogger, requestLogger } from "@nabinsaru/j-logger";
// or
const { JLogger, requestLogger } = require("@nabinsaru/j-logger");


// Set custom log styles
JLogger.TextColor = "White";
JLogger.BackgroundColor = "Black";
JLogger.TextFormat = ["Bold", "Underline"];
JLogger.StylizedMode = true;

// Use the logger
JLogger.log("This is a log message.");
JLogger.info("This is an info message.");
JLogger.error("This is an error message.");
JLogger.warn("This is a warning message.");
JLogger.debug("This is a debug message.");

// Use requestLogger middleware
app.use(requestLogger("REQUEST"));

// Log types
const logTypes = JLogger.getLogTypes();
console.log("Available log types:", logTypes);

Middleware Usage

The requestLogger middleware can be used to log HTTP request details.

const express = require("express");
const { requestLogger } = require("@nabinsaru/j-logger");

const app = express();

// Use requestLogger middleware for logging requests
// Provide log format type
app.use(requestLogger("common"));

// Your routes and other middleware...

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

Request logger supports 4 distinct log types.

  • common
::1 - - [Wed, 13 Dec 2023 05:34:25 GMT] "GET / HTTP/1.1" 200 15 "-" "PostmanRuntime/7.29.4"
  • dev
GET / 200 5 ms - 15
  • combined
::1 - - [Wed, 13 Dec 2023 05:41:02 GMT] "GET / HTTP/1.1" 200 15 "-" "PostmanRuntime/7.29.4"
  • short
GET / 200 15 - 5 ms

Demo

Generic Log

JLogger.log("This is custom log initialized with config file.");
JLogger.error("This is an error.");
JLogger.warn("This is a warning.");
JLogger.info("This is an info.");
JLogger.cleanLog("This is a clean Log.");
JLogger.debug("This is a debug statement.");
JLogger.debugBox("This is a debug box.");

JLogger.TextColor = 'Red';
JLogger.BackgroundColor = 'White';
JLogger.StylizedMode = true;
JLogger.saveLog = true;
JLogger.log("This is updated custom log.");

with config file j-logger.config.json

{
  "color": "Red",
  "backgroundColor": "Blue",
  "textFormat": ["Italic", "Bold"]
}

Generic Log

Stylized Log

JLogger.error("This is an error.");
JLogger.warn("This is a warning.");
JLogger.info("This is an info.");
JLogger.debug("This is a debug statement.");

Stylized Log

Stylized middleware Log

License

This custom logger is open-source and available under the MIT License. Feel free to use, modify, and share!

Development and Publication Scripts

  • npm run prepare => transpile the typescript into js
  • npm run start:dev => start the transpiled code
  • npm run test:build => set the build environment
  • npm run test => start the test module

Contributions

If you want to contribute, create an issues and make a PR for the issues.

Bugs

This release is maintained by solo developer and maybe prone to bug. So, I would appreciate if you can create a bug issues.

1.0.1

5 months ago

1.0.0

5 months ago