Skip to content

Commit f86c2db

Browse files
committed
enable namespaced logging
1 parent 9c571ca commit f86c2db

11 files changed

Lines changed: 932 additions & 54 deletions

File tree

CRUSH.md

Lines changed: 501 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 194 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
# Log Socket
1+
# Log Socket v2
22

3-
A real-time log viewer with WebSocket support, written in Go. This tool provides a web-based interface for viewing and filtering logs in real-time.
3+
A real-time log viewer with WebSocket support and namespace filtering, written in Go.
4+
5+
## What's New in v2
6+
7+
**Breaking Changes:**
8+
- Module path changed to `github.com/taigrr/log-socket/v2`
9+
- `CreateClient()` now accepts variadic `namespaces ...string` parameter
10+
- `Logger` type now includes `Namespace` field
11+
- New `NewLogger(namespace string)` constructor for namespaced loggers
12+
13+
**New Features:**
14+
- **Namespace support**: Organize logs by namespace (api, database, auth, etc.)
15+
- **Namespace filtering**: Subscribe to specific namespaces via WebSocket
16+
- **Frontend namespace selector**: Filter logs by namespace in the web UI
17+
- **Namespace API**: GET `/api/namespaces` to list all active namespaces
418

519
## Features
620

721
- Real-time log streaming via WebSocket
822
- Web-based log viewer with filtering capabilities
23+
- **Namespace-based log organization**
924
- Support for multiple log levels (TRACE, DEBUG, INFO, WARN, ERROR, PANIC, FATAL)
1025
- Color-coded log levels for better visibility
1126
- Auto-scrolling with toggle option
@@ -16,54 +31,203 @@ A real-time log viewer with WebSocket support, written in Go. This tool provides
1631
## Installation
1732

1833
```bash
19-
go install github.com/taigrr/log-socket@latest
34+
go install github.com/taigrr/log-socket/v2@latest
35+
```
36+
37+
## Quick Start
38+
39+
```go
40+
package main
41+
42+
import (
43+
"net/http"
44+
logger "github.com/taigrr/log-socket/v2/log"
45+
"github.com/taigrr/log-socket/v2/ws"
46+
"github.com/taigrr/log-socket/v2/browser"
47+
)
48+
49+
func main() {
50+
defer logger.Flush()
51+
52+
// Set up HTTP handlers
53+
http.HandleFunc("/ws", ws.LogSocketHandler)
54+
http.HandleFunc("/api/namespaces", ws.NamespacesHandler)
55+
http.HandleFunc("/", browser.LogSocketViewHandler)
56+
57+
// Use default namespace
58+
logger.Info("Application started")
59+
60+
// Create namespaced loggers
61+
apiLogger := logger.NewLogger("api")
62+
dbLogger := logger.NewLogger("database")
63+
64+
apiLogger.Info("API server ready")
65+
dbLogger.Debug("Database connected")
66+
67+
logger.Fatal(http.ListenAndServe(":8080", nil))
68+
}
69+
```
70+
71+
## Usage
72+
73+
### Starting the Server
74+
75+
```bash
76+
log-socket
77+
```
78+
79+
By default, the server runs on `0.0.0.0:8080`. Specify a different address:
80+
81+
```bash
82+
log-socket -addr localhost:8080
2083
```
2184

22-
## Example Preview
85+
### Web Interface
86+
87+
Open your browser and navigate to `http://localhost:8080`
88+
89+
**Namespace Filtering:**
90+
- The namespace dropdown is automatically populated from `/api/namespaces`
91+
- Select one or more namespaces to filter (hold Ctrl/Cmd to multi-select)
92+
- Default is "All Namespaces" (shows everything)
93+
- Click "Reconnect" to apply the filter
94+
95+
## API
2396

24-
1. Start the server:
97+
### Logging Interface
2598

26-
```bash
27-
log-socket
28-
```
99+
The package provides two ways to log:
29100

30-
By default, the server runs on `0.0.0.0:8080`. You can specify a different address using the `-addr` flag:
101+
#### 1. Package-level functions (default namespace)
31102

32-
```bash
33-
log-socket -addr localhost:8080
34-
```
103+
```go
104+
logger.Trace("trace message")
105+
logger.Debug("debug message")
106+
logger.Info("info message")
107+
logger.Notice("notice message")
108+
logger.Warn("warning message")
109+
logger.Error("error message")
110+
logger.Panic("panic message") // Logs and panics
111+
logger.Fatal("fatal message") // Logs and exits
112+
```
113+
114+
Each has formatted (`f`) and line (`ln`) variants:
115+
```go
116+
logger.Infof("User %s logged in", username)
117+
logger.Infoln("This adds a newline")
118+
```
35119

36-
2. Open your browser and navigate to `http://localhost:8080`
120+
#### 2. Namespaced loggers
37121

38-
![Log Socket Web Interface](browser/screenshot.png)
122+
```go
123+
apiLogger := logger.NewLogger("api")
124+
apiLogger.Info("Request received")
39125

40-
## Logging Interface
126+
dbLogger := logger.NewLogger("database")
127+
dbLogger.Warn("Slow query detected")
128+
```
129+
130+
### Creating Clients with Namespace Filters
131+
132+
```go
133+
// Listen to all namespaces
134+
client := logger.CreateClient()
135+
136+
// Listen to specific namespace
137+
client := logger.CreateClient("api")
138+
139+
// Listen to multiple namespaces
140+
client := logger.CreateClient("api", "database", "auth")
141+
```
41142

42-
The package provides a comprehensive logging interface with the following methods:
143+
### WebSocket API
43144

44-
- `Trace/Tracef/Traceln`: For trace-level logging
45-
- `Debug/Debugf/Debugln`: For debug-level logging
46-
- `Info/Infof/Infoln`: For info-level logging
47-
- `Notice/Noticef/Noticeln`: For notice-level logging
48-
- `Warn/Warnf/Warnln`: For warning-level logging
49-
- `Error/Errorf/Errorln`: For error-level logging
50-
- `Panic/Panicf/Panicln`: For panic-level logging
51-
- `Fatal/Fatalf/Fatalln`: For fatal-level logging
145+
#### Log Stream Endpoint
146+
147+
**URL:** `ws://localhost:8080/ws`
148+
149+
**Query Parameters:**
150+
- `namespaces` (optional): Comma-separated list of namespaces to filter
151+
152+
**Examples:**
153+
```
154+
ws://localhost:8080/ws # All namespaces
155+
ws://localhost:8080/ws?namespaces=api # Only "api" namespace
156+
ws://localhost:8080/ws?namespaces=api,database # Multiple namespaces
157+
```
158+
159+
**Message Format:**
160+
```json
161+
{
162+
"timestamp": "2024-11-10T15:42:49.777298-05:00",
163+
"output": "API request received",
164+
"file": "main.go:42",
165+
"level": "INFO",
166+
"namespace": "api"
167+
}
168+
```
169+
170+
#### Namespaces List Endpoint
171+
172+
**URL:** `GET http://localhost:8080/api/namespaces`
173+
174+
**Response:**
175+
```json
176+
{
177+
"namespaces": ["default", "api", "database", "auth"]
178+
}
179+
```
52180

53181
## Web Interface Features
54182

55-
- **Filtering**: Type in the search box to filter logs
56-
- **Auto-scroll**: Toggle auto-scrolling with the checkbox
183+
- **Namespace Dropdown**: Dynamically populated from `/api/namespaces`, multi-select support
184+
- **Text Search**: Filter logs by content, level, namespace, or source file
185+
- **Auto-scroll**: Toggle auto-scrolling with checkbox
57186
- **Download**: Save all logs as a JSON file
58187
- **Clear**: Remove all logs from the viewer
59-
- **Color Coding**: Different log levels are color-coded for easy identification
188+
- **Color Coding**: Different log levels are color-coded
189+
- **Reconnect**: Reconnect WebSocket with new namespace filter
190+
191+
## Migration from v1
192+
193+
### Import Path
194+
195+
```go
196+
// v1
197+
import "github.com/taigrr/log-socket/log"
198+
199+
// v2
200+
import "github.com/taigrr/log-socket/v2/log"
201+
```
202+
203+
### CreateClient Changes
204+
205+
```go
206+
// v1
207+
client := log.CreateClient()
208+
209+
// v2 - specify namespace(s) or leave empty for all
210+
client := log.CreateClient() // All namespaces
211+
client := log.CreateClient("api") // Single namespace
212+
client := log.CreateClient("api", "db") // Multiple namespaces
213+
```
214+
215+
### New Logger Constructor
216+
217+
```go
218+
// v2 only - create namespaced logger
219+
apiLogger := log.NewLogger("api")
220+
apiLogger.Info("Message in api namespace")
221+
```
60222

61223
## Dependencies
62224

63225
- [gorilla/websocket](https://github.com/gorilla/websocket) for WebSocket support
64226

65227
## Notes
66228

67-
The web interface is not meant to be used as-is.
68-
It functions perfectly well for some scenarios, but it is broken out into a different package intentionally, such that users can add their own as they see fit.
69-
It's mostly here to provide an example of how to consume the websocket data and display it.
229+
The web interface is provided as an example implementation. Users are encouraged to customize it for their specific needs. The WebSocket endpoint (`/ws`) can be consumed by any WebSocket client.
230+
231+
## License
232+
233+
See LICENSE file for details.

0 commit comments

Comments
 (0)