https://github.com/appsaeed/loggest
A flexible logging utility package
https://github.com/appsaeed/loggest
javascript logger ndoejs
Last synced: 12 months ago
JSON representation
A flexible logging utility package
- Host: GitHub
- URL: https://github.com/appsaeed/loggest
- Owner: appsaeed
- Created: 2025-06-21T14:56:04.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-28T17:08:42.000Z (12 months ago)
- Last Synced: 2025-06-28T17:36:20.596Z (12 months ago)
- Topics: javascript, logger, ndoejs
- Language: TypeScript
- Homepage:
- Size: 242 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Fast & Pluggable logger
> A flexible, minimalistic logging utility package β fast and pluggable.
### β¨ Features
- β‘ Fast and lightweight
- π Plugin-based logging (File, Console, API, Custom)
- π§© Custom log drivers (e.g., database, HTTP)
- ποΈ File and remote/API logging
- π§ Context-aware and structured logs
- ποΈ Filterable and configurable log levels
Includes everything needed for structured logging, while remaining small and dependency-free.
## π¦ Installation
```bash
npm install loggest
```
## π Basic Usage
A logger instance can be initialized with one or more plugins to control how logs are processed.
### Inline Plugin Example
```ts
import { Logger } from "loggest";
const logger = new Logger({
plugins: [
{
handle: function (level, context, message, ...optional) {
console.log(level, message, ...optional);
},
},
],
});
logger.info({ name: "app", version: "1.x" });
logger.info("Application started", "additional context");
```
### Using Imported Plugins
```ts
import { Logger } from "loggest";
import FileLog from "loggest/dist/plugins/FileLog";
const logger = new Logger({
plugins: [new FileLog()],
});
logger.info({ name: "app", version: "1.x" });
logger.info("Application started", "additional context");
```
Plugins can be mixed and matched, and custom implementations can also be added as needed. See the [Available Plugins](#-available-plugins) section for more examples.
## βοΈ Advanced Configuration
```ts
import Logger from "loggest";
import FileLog from "loggest/dist/plugins/FileLog"
import ApiLog from "loggest/dist/plugins/ApiLog"
const logger = new Logger({
// customize level defalut: info, warn, error, debug, log.
levels: ["info", "warn"] //now only will log info, warn.
plugins: [
new ApiLog({ url: "https://example.com/logs" }),
//other plugins
],
context: { app: "my-app", env: "production" } // context object (optional),
format: (level, ctx, msg, ...rest) => `New message: ${msg}`,
filter: (level, ctx, msg, ...rest) => level !== "warn" // avoid warn log,
});
```
## π Available Plugins
The logger includes several built-in plugins for common logging use cases:
- `FileLog` β Writes logs to a file.
- `FetchLog` β Sends logs to an external HTTP endpoint.
- `ConsoleLog` β Outputs logs to the console.
- `CustomLog` β Allows defining custom behavior via plugin.
Custom plugins can be created using either an object or a class.
### π§ Object-based Plugin Example
```ts
const CustomLogger = {
/**
* Handle the plugin log
*/
handle: async (level: LogLevel, ctx, message: any, ...optional: any[]) => {
// Custom handling logic: store in a database, send to an API, etc.
},
};
```
### Class-based Plugin Example
```ts
export class ColoredConsoleLog {
/**
* Handle the plugin log
*/
async handle(level, ctx, message, ...optional) {
const colorMap = {
info: "\x1b[36m", // Cyan
warn: "\x1b[33m", // Yellow
error: "\x1b[31m", // Red
debug: "\x1b[90m", // Gray
};
console.log(`${colorMap[level] || ""}${level}:`, message, ...optional);
}
}
```
### β
Registering Plugins
```ts
const logger = new Logger({
plugins: [new ColoredConsoleLog(), CustomLogger],
});
logger.info("Server started successfully");
```
### π§© Plugin Interface for TypeScript
To enable type checking and IntelliSense support, implement the `Plugin` interface:
```ts
import { LogLevel, Plugin } from "loggest";
export class MyLogger implements Plugin {
//handle the plugin log
async handle(level: LogLevel, ctx: any, message: any, ...optional: any[]): Promise {
// Custom logic for handling log output
}
}
```
## π€ Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
1. Fork the repo
2. Create a branch
3. Commit changes
4. Open a PR
Letβs improve this project together!
## π License
MIT License β Saeed Hosan