ODG Log Interface By Dragons Gamers
Log Package interface
Using Stanley
Table of Contents
Benefits
- Logs Pattern interface
- Similar PHP psr-3
Libraries
Dependencies
- Node.js 16 or later
- Yarn Optional/Recommended
- ODG TsConfig Last Version
- ODG Exception
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 buildandbun run startagain
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