Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartone committed Mar 1, 2025
1 parent c432b93 commit 9bc5765
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 63 deletions.
97 changes: 41 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,23 @@
[npm-image]: https://img.shields.io/npm/v/missionlog.svg?style=flat
[npm-url]: https://www.npmjs.com/package/missionlog

**Lightweight, configurable logging library** with **level-based filtering** and **tagging** to keep logs structured and readable.
**A lightweight, highly configurable logging library** with **level-based filtering** and **tag-based organization** for **structured, readable logs**—ideal for both **browser and server environments**.

---

## New in Version 2.0 🚀
## **🚀 What's New in Version 2.0 + Key Features**

- **ESM & CJS Support** Now works seamlessly with both **ES Modules (ESM)** and **CommonJS (CJS)**.
- **More Efficient Logging** Removed unnecessary internal error handling to improve performance.
- **Improved Tag Handling** Optimized proxy behavior for dynamic tag registration.
- **Better Config Defaults** Invalid log levels now correctly default to TRACE with a warning.

---

## ** Features**
✔️ **Small footprint (~1KB)**
✔️ **Filter logs by level:** `ERROR > WARN > INFO > DEBUG > TRACE`
✔️ **Tag-based filtering:** Assign log levels per **subsystem** (`'security'`, `'transporter'`, etc.)
✔️ **Customizable log output:**
- Style logs with **chalk**
- Send logs to a cloud service like **[Loggly](https://www.loggly.com/)**
- **Integrate seamlessly with Firebase Functions**
- **Styled console output in the browser**
✔️ **API mirrors `console`**, supports objects & rest parameters
✔️ **Works in both Node.js & browsers**
✔️ **TypeScript-ready** No need for `@types`
✔️ **Lightweight & Dependency-Free** Small (~1KB) with zero dependencies for fast, efficient logging.
✔️ **Seamless ESM & CJS Support** Works effortlessly with both **ES Modules (ESM)** and **CommonJS (CJS)**.
✔️ **Optimized Performance** O(1) log level lookups ensure minimal overhead.
✔️ **Level-Based Filtering** Control log visibility with levels: `ERROR > WARN > INFO > DEBUG > TRACE`.
✔️ **Tag-Based Logging** – Assign log levels per **subsystem** (e.g., `'security'`, `'transporter'`).
✔️ **Smarter Tag Handling** Enhanced proxy-based system for dynamic, intuitive tag registration.
✔️ **Flexible Output** Log to the console, **style with chalk**, or stream logs to **Firebase, AWS, or other services**.
✔️ **Seamless Integration** Works in **Node.js**, browsers, and **Firebase Functions**.
✔️ **Improved Defaults & Error Handling** Invalid log levels now gracefully default to **TRACE**.
✔️ **Console-Like API** Supports objects, rest parameters, and mirrors native `console` methods.
✔️ **TypeScript-Ready** Built-in types, no need for `@types`.

---

Expand All @@ -41,28 +33,6 @@ npm i missionlog
---

## **🚀 Getting Started**
### **1 Initialize the Logger**
```typescript
import { log, LogLevel } from 'missionlog';
import chalk from 'chalk';
// Define how each log level should be handled
const logger = {
[LogLevel.ERROR]: (tag, msg, params) => console.error(`[${chalk.red(tag)}]`, msg, ...params),
[LogLevel.WARN]: (tag, msg, params) => console.warn(`[${chalk.yellow(tag)}]`, msg, ...params),
[LogLevel.INFO]: (tag, msg, params) => console.log(`[${chalk.green(tag)}]`, msg, ...params),
[LogLevel.DEBUG]: (tag, msg, params) => console.log(`[${chalk.magenta(tag)}]`, msg, ...params),
[LogLevel.TRACE]: (tag, msg, params) => console.log(`[${chalk.cyan(tag)}]`, msg, ...params),
} as Record<LogLevel, (tag: string, msg: unknown, params: unknown[]) => void>;
// Initialize missionlog with tag levels & a custom handler
log.init(
{ transporter: 'INFO', security: 'ERROR', system: 'OFF' },
(level, tag, msg, params) => logger[level](tag, msg, params)
);
```

---

## **🗒 Basic Usage**
```typescript
Expand Down Expand Up @@ -95,19 +65,35 @@ log.init();

---

## **💡 Advanced Usage**
### **🔹 Firebase Functions Integration**

### **1 Example Browser Initialization**
```typescript
import { log, LogLevel } from 'missionlog';
import chalk from 'chalk';
// Define how each log level should be handled
const logger = {
[LogLevel.ERROR]: (tag, msg, params) => console.error(`[${chalk.red(tag)}]`, msg, ...params),
[LogLevel.WARN]: (tag, msg, params) => console.warn(`[${chalk.yellow(tag)}]`, msg, ...params),
[LogLevel.INFO]: (tag, msg, params) => console.log(`[${chalk.green(tag)}]`, msg, ...params),
[LogLevel.DEBUG]: (tag, msg, params) => console.log(`[${chalk.magenta(tag)}]`, msg, ...params),
[LogLevel.TRACE]: (tag, msg, params) => console.log(`[${chalk.cyan(tag)}]`, msg, ...params),
} as Record<LogLevel, (tag: string, msg: unknown, params: unknown[]) => void>;
// Initialize missionlog with tag levels & a custom handler
log.init(
{ transporter: 'INFO', security: 'ERROR', system: 'OFF' },
(level, tag, msg, params) => logger[level](tag, msg, params)
);
```

---

### **🔹 Example Firebase Functions Integration**
```typescript
import { debug, error, info, warn } from 'firebase-functions/logger';
import { log, LogLevel, tag } from 'missionlog';
// import from a settings
const logOptiosn = {
"functions": "DEBUG",
"system": "INFO",
"firestore": "WARN"
};
const firebaseLogger = {
[LogLevel.ERROR]: (tag, payload) => error({ tag, ...payload }),
[LogLevel.WARN]: (tag, payload) => warn({ tag, ...payload }),
Expand All @@ -117,10 +103,9 @@ const firebaseLogger = {
[LogLevel.OFF]: () => void 0,
};
log.init(settings.logOptions, (level, tag, msg, params) => {
firebaseLogger[level](tag, { ...params, message: msg });
});
log.init({ transporter: 'INFO', security: 'ERROR', system: 'OFF' },
(level, tag, msg, params) => firebaseLogger[level](tag, { ...params, message: msg })
);
```

---
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
"log",
"logger",
"logging",
"browser",
"node",
"mobile",
"front-end",
"backend",
"isomorphic",
"lightweight",
"typescript",
"minimal"
"esm",
"cjs",
"structured-logging",
"configurable",
"filtering",
"log-levels"
],
"engines": {
"node": ">=18.0.0"
Expand Down

0 comments on commit 9bc5765

Please sign in to comment.