Skip to content

πŸ“ A powerful, flexible, and easy-to-use logging library for JS/TS projects. πŸ—ƒοΈ

License

Notifications You must be signed in to change notification settings

mutasim77/logify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Banner

πŸ“ Logify is a powerful, flexible, and easy-to-use logging library for JS/TS projects. πŸ—ƒοΈ

Features πŸš€

  • πŸ”΄ Multiple log levels (ERROR, WARN, INFO, DEBUG)
  • 🎨 Customizable formatters (Text and JSON)
  • πŸ”Œ Extensible transport system (Console and File included)
  • βš™οΈ Fully configurable
  • πŸ“± React and Next.js(and any JS lib) friendly
  • πŸ›‘οΈ Written in TypeScript for type safety

Installation πŸ“¦

npm install logify-ts

Quick Start πŸƒβ€β™‚οΈ

Here's an example of how to use the logger in a Next.js API route:

  1. Create a file called logger.ts
your-project
    |_ app 
    |_ public
    |_ logger.ts πŸ‘ˆ

Add the following code in logger.ts file

import { Logger, LogLevel, ConsoleTransport, FileTransport, JsonFormatter } from 'logify-ts';

export const logger = new Logger({
    level: LogLevel.DEBUG,
    transports: [
        new ConsoleTransport(),
        new FileTransport('app.log', new JsonFormatter())
    ]
});

Note

Logify is a flexible logging library that allows you to easily configure multiple transports and formatters for your logs.

  1. Use logger in an API route Create a file api/test/route.ts inside app dir
your-project
    |_ app 
    |  |_api πŸ‘ˆ
    |    |_test πŸ‘ˆ
    |       |_route.ts πŸ‘ˆ
    |
    |_ public
    |_ logger.ts 
import { logger } from "@/logger";
import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest) {
    logger.info(`Incoming GET request to /api/logger`);

    // Log request details
    logger.debug('Request details', {
        url: req.url,
        method: req.method,
        headers: Object.fromEntries(req.headers),
    });

    // Log query parameters
    const searchParams = req.nextUrl.searchParams;
    logger.debug('Query parameters', Object.fromEntries(searchParams));

    try {
        // Simulate some processing
        const result = await someAsyncOperation();

        logger.info('Operation successful', { result });

        return NextResponse.json({
            message: "success",
            status: 200,
            data: result
        });
    } catch (error) {
        logger.error('Error in GET /api/logger', {
            error: error instanceof Error ? error.message : 'Unknown error',
            stack: error instanceof Error ? error.stack : undefined
        });

        return NextResponse.json({
            message: "Internal server error",
            status: 500,
        }, { status: 500 });
    } finally {
        logger.debug('Request processing completed');
    }
}

async function someAsyncOperation() {
    await new Promise(resolve => setTimeout(resolve, 100));
    return { key: 'value' };
}

Note

This example provides a practical, real-world use case of your logger in a Next.js API route. It's concise yet informative, showing how to log different types of information at various stages of request processing.

🀝 Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  • Fork the Project
  • Create your Feature Branch (git checkout -b feature/AmazingFeature)
  • Commit your Changes (git commit -m 'Add some AmazingFeature')
  • Push to the Branch (git push origin feature/AmazingFeature)
  • Open a Pull Request

License πŸ“„

LogSage is MIT licensed. See the LICENSE file for details.

Support πŸ†˜

If you encounter any issues or have questions, please file an issue on GitHub issue tracker.

Built with ❀️ by mutasim

Happy Logging! πŸŽ‰