1.1.1 • Published 12 months ago

give-logger v1.1.1

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

Here is the exact README.md file content that you can copy and paste:

# Winston Logger Middleware for Express

A customizable Winston logger middleware for Express.js applications. It provides easy logging of requests, responses, and errors with customizable log levels and formats.

## Features

- Logs requests and responses in Express.
- Customizable log levels (e.g., `info`, `warn`, `error`).
- Supports both console and file-based logging.
- Supports TypeScript and JavaScript usage.

## Installation

### NPM

You can install this package from npm:

```bash
npm install give-logger

Yarn

If you are using Yarn:

yarn add give-logger

Usage

Basic Setup

In your Express.js app, you can import and use the logger middleware to log incoming requests and responses.

JavaScript

const express = require('express');
const Logger = require('give-logger'); // Import the Logger

const app = express();

// Use the requestLogger middleware to log incoming requests and responses
app.use(Logger.requestLogger());

// Custom routes
app.get('/', (req, res) => {
  
  Logger.info('This is an info log from the root route');
  res.send('Hello, world!');
});

app.get('/error', (req, res) => {
  
  Logger.error('This is an error log');
  res.status(500).send('Something went wrong!');
});

// Start the server
const port = 3000;
app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`);
});

TypeScript

import express from 'express';
import { Logger } from 'give-logger';

const app = express();

// Use the logger middleware
app.use(Logger.requestLogger());

app.get('/', (req, res) => {
  Logger.info('Handling GET /');
  res.send('Hello, World!');
});

app.get('/error', (req, res) => {
  Logger.error('Handling GET /error');
  res.send('handling GET /error');
});

app.get('/debug', (req, res) => {
  Logger.debug('Handling GET /debug');
  res.send('handling GET /debug');
});

app.get('/warn', (req, res) => {
  Logger.warn('Handling GET /warn');
  res.send('handling GET /warn');
});

const port = 3000;
app.listen(port, () => {
  Logger.info(`Server is running on port ${port}`);
});

Logger Configuration

By default, the logger writes logs to the console and a file (logs/app.log). You can customize the log level and the format.

Available Log Levels

  • info
  • warn
  • error

To change the log level or customize other settings, you can modify the Logger constructor:

const logger = new Logger('warn'); // Sets the default log level to 'warn'

Middleware for Request/Response Logging

To log requests and responses, use the requestLogger middleware. This middleware logs details of incoming HTTP requests and the corresponding responses, including the status code.

app.use(Logger.requestLogger());

The logs will include details such as:

  • HTTP method (e.g., GET, POST)
  • URL
  • Response status code

Development

To contribute to the development of this package:

  1. Clone the repository.
  2. Install dependencies:

    npm install
  3. Build the TypeScript code (if applicable):

    npm run build
  4. Run tests (if applicable):

    npm test
  5. Push your changes and create a pull request.

1.1.1

12 months ago

1.1.0

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

1 year ago