npm.io
1.20.0 • Published 1 month ago

@odg/log

Licence
MIT
Version
1.20.0
Deps
2
Size
120 kB
Vulns
0
Weekly
0
Stars
2

Stanley Imagem
ODG Log Interface By Dragons Gamers

Log Package interface

Using Stanley

codecov Stargazers Made by ODGodinho Forks Repository size GitHub last commit License StyleCI

Table of Contents


Benefits

  • Logs Pattern interface
  • Similar PHP psr-3

Libraries

Dependencies

Get Started


Use Log Interface

Install in your project using this command

yarn add @odg/log
Usage
Exception

InvalidArgumentException dispatch if send invalid arguments

Implementation
  • LoggerAwareInterface Implements this if your class dependencies Logger
  • LogLevel it is an enum with the types of logs
  • LogLevelType is a list of log types
  • LoggerInterface for create a logger class
  • AbstractLogger Abstract class to logger implements only log function
  • NullLogger Generic logger without action
Free Sample
Class Aware Log

If you class dependencies LoggerInstance use interface LoggerAwareInterface

if you prefer you can depend on the log class in your constructor

import { LoggerAwareInterface, LoggerInterface } from "@odg/log";

export class LoggerAwareExample implements LoggerAwareInterface {

    private logger?: LoggerInterface;

    public setLogger(logger: LoggerInterface): void {
        this.logger = logger;
    }

    public runExampleClass(): void {
        try {
            // Anything code
        } catch (error) {
            this.logger?.debug(error);
        }
    }

}
Create Logger Class
/**
 * This Example Logger using console.log
 *
 * @author Dragons Gamers <https://github.com/ODGodinho>
 */
export class ConsoleLogger extends AbstractLogger {

    /**
     * Logs with an arbitrary level.
     *
     * @param {LogLevel} level Log level
     * @param {unknown} message Message Log
     * @param {TContext} context Context Message replace
     *
     * @returns {Promise<void>}
     */
    public async log(level: LogLevel, message: unknown, context?: TContext): Promise<void> {
        const log = await this.parser(level, message, context); // Use this for plugins load

        return console.log(`Level: ${log.level} >> ${String(log.message)}`, log.context);
    }

}

Prepare To Develop

Copy .env.example to .env and add the values according to your needs.

Start Project

First install dependencies with the following command

bun install
# or
npm install

Build and Run

To build the project, you can use the following command

if you change files, you need to run bun run build and bun run start again

bun run build && bun run start
# or
bun run dev

Teste Code

To Test execute this command

bun run test
# or
bun run test:watch