Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Node CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: npm install
- run: npm test
58 changes: 58 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# ===============================
# Node dependencies
# ===============================
node_modules/

# ===============================
# Logs
# ===============================
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# ===============================
# Environment variables
# ===============================
.env
.env.*
!.env.example

# ===============================
# OS / Editor junk
# ===============================
.DS_Store
Thumbs.db
.idea/
.vscode/
*.swp
*.swo

# ===============================
# Build / cache
# ===============================
dist/
build/
coverage/
.cache/
.tmp/

# ===============================
# Test artifacts
# ===============================
jest-cache/
.nyc_output/

# ===============================
# npm / yarn / pnpm
# ===============================
package-lock.json
yarn.lock
pnpm-lock.yaml

# ===============================
# Optional: local test logs
# ===============================
logs-test/
44 changes: 44 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Changelog

All notable changes to this project will be documented in this file.

This project follows [Semantic Versioning](https://semver.org/).

---

## [1.2.0] – 2025-01-XX

### Added
- Support for multiple arguments (console-style logging).
- Automatic detection and logging of `Error` objects with stack traces.
- Optional JSON logging mode for structured logs.
- Optional size-based log rotation.
- Factory API: `Logger.createLogger(options)`.

### Changed
- Improved internal log formatting and safety.
- Console output now safely falls back to `console.log` when needed.

### Fixed
- README now accurately reflects error logging behavior.

### Backward Compatibility
- All existing v1.x usage remains fully supported.
- No breaking changes introduced.

---

## [1.1.0] – 2024-XX-XX

### Added
- Error object support (message + stack trace).
- Improved README accuracy and clarity.

---

## [1.0.4] – Initial Stable Release

### Added
- Basic file and console logging.
- Support for `info`, `warn`, and `error` levels.
- Daily log files organized by level.
217 changes: 182 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,201 @@
# Node-Error-Logger.JS
# Node-Error-Logger.js

> A simple and efficient Node.js package for logging errors and messages to files and the console.
![CI](https://github.com/makstyle119/node-error-logger.js/actions/workflows/test.yml/badge.svg)

## Features:
- Supports error, warning, and info logs.
- Logs messages with timestamps and severity levels.
- Saves logs to separate files based on date and level.
- Provides clear and concise API for logging.
> A lightweight, zero-dependency logger for Node.js with file logging, error stacks, and console-style usage.

## Installation:
```Bash
This package is designed for developers who want **simple, predictable logging** without pulling in large logging frameworks.

---

## ✨ Features

* ✅ Zero dependencies
* ✅ Supports `info`, `warn`, and `error` levels
* ✅ Console-style logging (multiple arguments like `console.log`)
* ✅ Automatic error stack trace logging
* ✅ Logs stored by **date and level**
* ✅ Optional JSON logs (production-friendly)
* ✅ Optional size-based log rotation
* ✅ Async, non-blocking file writes
* ✅ Backward compatible across all v1.x versions

---

## 📦 Installation

```bash
npm i node-error-logger.js
```

## Usage
1. Import the Logger module:
```JavaScript
---

## 🚀 Basic Usage (Backward Compatible)

```js
const Logger = require('node-error-logger.js');

Logger.info('Application started');
Logger.warn('Low memory warning');
Logger.error('Something went wrong');
```
2. Use the provided logging functions:
```JavaScript
Logger.info('Application started successfully.');

// Log an error with an optional error object
const myError = new Error('Something went wrong!');
Logger.error('An error occurred!', myError);
This works exactly the same as earlier versions.

---

## 🧩 Multiple Arguments (Console-style)

Just like `console.log`, you can pass multiple values:

```js
Logger.info('User created', userId, userData);
Logger.warn('Invalid input', { field: 'email' });
```

## API:
- `Logger.info(message):` Logs an informational message.
- `Logger.warn(message):` Logs a warning message.
- `Logger.error(message, error):` Logs an error message. (Optional error object for stack trace)
Extra values are treated as **metadata**.

## Example:
```JavaScript
const Logger = require('node-error-logger.js');
---

## ❌ Error Object Support (Automatic)

// Log messages throughout your application
Logger.info('Processing data...');
Pass an `Error` anywhere in the arguments:

```js
try {
// Your application logic here
} catch (error) {
Logger.error('An error occurred!', error);
throw new Error('Database connection failed');
} catch (err) {
Logger.error('Unhandled exception', err, { retry: false });
}
```

✔ Logs the error message
✔ Logs the full stack trace
✔ No extra configuration required

Logger.warn('A potential issue might arise.');
---

## 📂 Log Output Structure

Logs are written to the application root:

```
logs/
├── info/
│ └── 2025-01-01-info.log
├── warn/
│ └── 2025-01-01-warn.log
└── error/
└── 2025-01-01-error.log
```

---

## 🧪 JSON Logging Mode (Optional)

Ideal for production environments and log processors.

```js
const Logger = require('node-error-logger.js');

const logger = Logger.createLogger({ json: true });

logger.error('Payment failed', new Error('timeout'), { orderId: 123 });
```

### Example JSON Output

```json
{
"timestamp": "2025-01-01T12:00:00.000Z",
"level": "error",
"message": "Payment failed",
"meta": [{ "orderId": 123 }],
"error": {
"name": "Error",
"message": "timeout",
"stack": "..."
}
}
```

## Benefits:
- Improves debugging by providing detailed logs.
- Helps monitor application behavior and identify errors.
- Easy to integrate into your Node.js projects.
---

## 🔁 Log Rotation (Optional)

Enable size-based log rotation to prevent large log files.

```js
const logger = Logger.createLogger({
rotate: true,
maxSizeMB: 10
});
```

* Rotation is **disabled by default**
* Old files are renamed with a timestamp

---

## 🛠 API Reference

### `Logger.info(...args)`

Logs informational messages.

### `Logger.warn(...args)`

Logs warnings.

### `Logger.error(...args)`

Logs errors and automatically captures stack traces when an `Error` is provided.

### `Logger.createLogger(options)`

Creates a new logger instance with custom options.

#### Options

| Option | Type | Default | Description |
| ----------- | ------- | ------- | --------------------------------- |
| `json` | boolean | `false` | Enable JSON log output |
| `rotate` | boolean | `false` | Enable size-based log rotation |
| `maxSizeMB` | number | `5` | Max log file size before rotation |

---

## 🔒 Backward Compatibility Guarantee

All existing usage patterns are **fully supported** in all `v1.x` releases.

New features are:

* optional
* opt-in
* non-breaking

---

## ❗ What This Package Is (and Is Not)

### ✅ This package is:

* Simple
* Lightweight
* Predictable
* Ideal for small services, scripts, and APIs

### ❌ This package is NOT:

* A replacement for Winston or Pino.
* A plugin-based logging framework.
* Designed for massive distributed systems.

If you need advanced transports or integrations, a full-featured logger may be a better fit.

---

## 📄 License

MIT © Mohammad Moiz Ali (MAKSTYLE119)
Loading