log-vista
Version:
This is a project which capture logs and stores in database and display on web inreal time
127 lines (98 loc) β’ 3.76 kB
Markdown
**Logvista** is a self-hosted log ingestion service.
```bash
npm install log-vista
```
```bash
npx logvista init
```
```bash
npx logvista start
```
```bash
curl -X POST http://localhost:5566/logs -H "Content-Type: application/json" -d '{ "timestamp": "2025-06-05T18:20:00Z", "level": "error", "message": "Payment failed", "tag": "user", "metadata": {"userId": "123"}, "projectId": "xxx", "clientId": "yyy" }'
```
```bash
curl 'http://localhost:5566/logs?projectId=xxx&clientId=yyy&page=1&limit=50'
```
---
MIT License Β© Your Name
An efficient, customizable, and easy-to-use logging library for Node.js applications! This logger captures various log levels (info, warning, error, etc.), stores them in a MySQL database, and displays logs in a structured JSON format on the console/terminal. Ideal for real-time log tracking with an optional server!
## β¨ Features
- πΉ **Log Levels**: Manage logs across levelsβinfo, warn, error, etc.
- πΉ **Database Storage**: Logs are saved to MySQL using connection pooling for efficient performance.
- πΉ **JSON Output**: Logs appear in clean JSON with timestamps for clarity.
- πΉ **Real-Time Viewing (Optional)**: Track logs live with the optional server.
- πΉ **Configurable Setup**: Quick setup with `config.json`.
## π₯ Installation
Install the package:
```bash
npm install customlogger
```
π Prerequisites
Node.js (latest stable version)
MySQL Database
mysql2 npm package for MySQL support
## π Quick Start
1οΈβ£ Configure Your Database
Create a config.json file in your project root:
```json
{
"host": "your_mysql_host",
"user": "your_database_user",
"password": "your_database_password",
"database": "your_database_name",
"port": 3306
}
```
2οΈβ£ Import and Initialize Logger
In your main file, import the logger and initialize the database:
```javascript
const { logMessage, initializeDatabase } = require('customlogger');
// Initialize the database
initializeDatabase();
```
3οΈβ£ Log Your Messages
Add logs across various levels like so:
```javascript
Copy code
(async () => {
const now = new Date().toISOString();
await logMessage('info', 'This is an info message', { key: 'value' }, now);
await logMessage('warn', 'This is a warning', { warningCode: 101 }, now);
await logMessage('error', 'This is an error message', { errorCode: 'E500' }, now);
})();
```
```plaintext
src/
βββ cli.js
βββ index.js
βββ database.js
βββ server.js
config.json
package.json
```
To start logging, run your main file:
```bash
node your_main_file.js
```
```json
{"level":"info","message":"This is an info message","metadata":{"key":"value"},"timestamp":"2024-10-30T19:04:31.758Z"}
```
β οΈ Database Connection Issues: Make sure MySQL is running, and credentials in config.json are correct.
β οΈ Date Format Errors: Use YYYY-MM-DD HH:MM:SS format for timestamp to avoid issues when saving logs to the database.
This project is licensed under the MIT License.
Contributions are welcome! For major changes, open an issue first to discuss what youβd like to modify.