A simple, dependency-light solution for monitoring server logs in real-time directly through a web browser. It uses Node.js and WebSockets to mimic the functionality of tail -f without requiring external client tools.
root/
├── index.html
├── server.js
├── test.log
└── README.md
- Node.js (v14 or later recommended)
- The
wspackage (WebSocket library).
Install the required WebSocket package:
npm install wsRun the main backend file:
node server.jsOpen the web browser and navigate to:
http://localhost:8080
- Client Connects: The browser loads
index.htmland initiates a WebSocket connection to the server. - Initial Data:
server.jsreads the last 15 lines oftest.logand sends them to the client. - File Watcher: The
fs.watchmodule monitorstest.logfor changes. - Live Stream: When data is appended, the server reads only the new content and broadcasts it to all clients via the WebSocket connection.
All configuration constants are located at the top of server.js:
| Constant | Default Value | Description |
|---|---|---|
PORT |
8080 |
The port for the HTTP and WebSocket server. |
LOG_FILE_PATH |
./test.log |
The path to the log file to monitor. |
INITIAL_LINES |
15 |
The number of log lines to show on initial client connect. |
To monitor a real log file instead of the mock one, comment out the setInterval call near the end of server.js:
// Comment out this line to disable the mock generator:
// setInterval(() => { ... }, LOG_GENERATION_INTERVAL);- Attempting to open
index.htmldirectly (usingfile://) will not work, as WebSockets require a running HTTP server context. - The target log file (
test.logby default) is created automatically if it doesn't exist.