1.6.1 โข Published 7 months ago
@odg/log v1.6.1
Table of Contents
- ๐ Benefits
- ๐ Libraries
- ๐ Dependencies
- โฉ Get Started
- ๐ฐ Usage
- ๐ฐ Free Sample
- ๐ป Prepare to develop
๐ 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
yarn 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
yarn build
andyarn start
again
yarn build && yarn start
# or
yarn dev
๐งช Teste Code
To Test execute this command
yarn test
# or
yarn test:watch