# `nyno-log` — Simple JSON → PostgreSQL Logger for Nyno Workflows

![Nyno Log Screenshot](/h/f37d79435106d4decb25be5ab49d4d5a853922ba79d6e497f3f2255d5207d62b/screenshot-from-2025-10-15-18-12-08.webp)

## Overview

`nyno-log` is a lightweight [Nyno](https://github.com/empowerd-cms/nyno)  extension that writes JSON logs directly into PostgreSQL.
It’s built to be fast, minimal, and production-ready — simply drop it into your Nyno environment and start logging.

### 🔹 Key Features

* Writes structured JSON logs to PostgreSQL
* Auto-flushes logs from a fast queue table to a persistent store
* Plug-and-play with Nyno v2 (no code changes required)
* One-step setup with automatic DB creation

---

## ⚙️ Installation

### Requirements

* [PostgreSQL 18+](https://www.postgresql.org/download/)
* [Nyno](https://github.com/empowerd-cms/nyno)

### 1. Create the Extensions Folder

If your Nyno project doesn’t already have one:

```bash
mkdir -p extensions/
```

### 2. Clone or Copy `nyno-log` into `extensions/`

```bash
cd extensions/
git clone https://github.com/empowerd-cms/nyno-log
```

Your structure should look like:

```
nyno/
├── extensions/
│   └── nyno-log/
│       ├── command.js
│       ├── setup.sh
│       ├── LICENSE
│       └── template.yml
```

### 3. Run Setup

The setup script creates a dedicated Postgres database, user, and schema:

```bash
bash extensions/nyno-log/setup.sh
```

This will:

* Create the database and user in PostgreSQL
* Apply migrations for `queue_logs` and `persistent_logs` tables
* Save connection credentials to `envs/.nyno_log_db.env`

---

## 🚀 Usage in a Nyno Workflow

Add the extension to your workflow:

```yaml
nyno-log:
  args:
    - "${JSON}"
```

Example JSON payload:

```json
{"event": "user_signup", "user": "alice", "timestamp": "2025-10-15T18:00:00Z"}
```

Each call:

* Inserts the JSON into the `queue_logs` table
* Runs `flush_queue()` to move older entries (>10s) into `persistent_logs`
* Returns `{ "output": "ok" }`

---

## ⚡ Example via TCPMan

Quick test using `tcpman`:

```bash
time tcpman localhost:6001/test_nyno_log \
  'c{"apiKey":"changeme"}' \
  'q{"JSON":{"test":"log 1"}}' \
  'q{"JSON":{"test":"log 2"}}' \
  'q{"JSON":{"test":"log 3"}}'
```

---

## ⚙️ Configuration

Configuration file is `automatically generated by setup.sh` is handled via the generated `.env` file:

**`envs/.nyno_log_db.env`**

```
NYNO_DB_NAME=nyno_log_db_1697404820
NYNO_DB_USER=nyno_log_user_1697404820
NYNO_DB_PASS=<auto-generated>
NYNO_DB_HOST=localhost
NYNO_DB_PORT=5432
```


---

## 🧾 License

Licensed under the [Apache License 2.0](./LICENSE).

### Nyno-log is the first JavaScript extension for [Nyno](https://github.com/empowerd-cms/nyno)
