mirror of
https://github.com/taigrr/log-socket
synced 2026-03-20 16:02:28 -07:00
Compare commits
9 Commits
v1.0.6
...
cd/quality
| Author | SHA1 | Date | |
|---|---|---|---|
| 9395e417c6 | |||
| 5cb1329155 | |||
| e725622696 | |||
| 47bfb5ed98 | |||
| 6a709d3963 | |||
| 0a78d1ce90 | |||
| 93dd230be0 | |||
| f86c2dbf46 | |||
|
|
9c571ca955
|
13
.github/workflows/ci.yaml
vendored
13
.github/workflows/ci.yaml
vendored
@@ -1,23 +1,20 @@
|
|||||||
name: Go package
|
name: Go package
|
||||||
|
|
||||||
on: [push]
|
on: [push, pull_request]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: "1.21"
|
go-version: "1.26"
|
||||||
|
|
||||||
- name: Install dependencies
|
|
||||||
run: go get .
|
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: go build -v ./...
|
run: go build -v ./...
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: go test -v ./...
|
run: go test -race -v ./...
|
||||||
|
|||||||
492
CRUSH.md
Normal file
492
CRUSH.md
Normal file
@@ -0,0 +1,492 @@
|
|||||||
|
# CRUSH.md - Log Socket v2 Development Guide
|
||||||
|
|
||||||
|
This document provides context and conventions for working on **log-socket v2** - a real-time log viewer with WebSocket support and namespace filtering, written in Go.
|
||||||
|
|
||||||
|
## Project Overview
|
||||||
|
|
||||||
|
Log Socket v2 is a Go library and standalone application that provides:
|
||||||
|
- Real-time log streaming via WebSocket
|
||||||
|
- **Namespace-based log organization** (NEW in v2)
|
||||||
|
- Web-based log viewer with namespace filtering
|
||||||
|
- Support for multiple log levels (TRACE, DEBUG, INFO, NOTICE, WARN, ERROR, PANIC, FATAL)
|
||||||
|
- Client architecture allowing multiple subscribers to filtered log streams
|
||||||
|
|
||||||
|
**Key insight**: This is both a library (importable Go package) and a standalone application. The main.go serves as an example implementation.
|
||||||
|
|
||||||
|
## Essential Commands
|
||||||
|
|
||||||
|
### Build
|
||||||
|
```bash
|
||||||
|
go build -v ./...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Run Server
|
||||||
|
```bash
|
||||||
|
# Default (runs on 0.0.0.0:8080)
|
||||||
|
go run main.go
|
||||||
|
|
||||||
|
# Custom address
|
||||||
|
go run main.go -addr localhost:8080
|
||||||
|
```
|
||||||
|
|
||||||
|
Once running, open browser to `http://localhost:8080` to view logs.
|
||||||
|
|
||||||
|
### Test
|
||||||
|
```bash
|
||||||
|
go test -v ./...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Install
|
||||||
|
```bash
|
||||||
|
go install github.com/taigrr/log-socket/v2@latest
|
||||||
|
```
|
||||||
|
|
||||||
|
### Dependencies
|
||||||
|
```bash
|
||||||
|
go get .
|
||||||
|
```
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
.
|
||||||
|
├── main.go # Example server with multiple namespaces
|
||||||
|
├── log/ # Core logging package
|
||||||
|
│ ├── log.go # Package-level logging functions + namespace tracking
|
||||||
|
│ ├── logger.go # Logger type with namespace support
|
||||||
|
│ ├── types.go # Type definitions (includes Namespace fields)
|
||||||
|
│ └── log_test.go # Tests
|
||||||
|
├── ws/ # WebSocket server
|
||||||
|
│ ├── server.go # LogSocketHandler with namespace filtering
|
||||||
|
│ └── namespaces.go # HTTP handler for namespace list API
|
||||||
|
└── browser/ # Web UI
|
||||||
|
├── browser.go # HTTP handler serving embedded HTML
|
||||||
|
└── viewer.html # Embedded web interface with namespace filter
|
||||||
|
```
|
||||||
|
|
||||||
|
## Major Changes in v2
|
||||||
|
|
||||||
|
### Module Path
|
||||||
|
- **v1**: `github.com/taigrr/log-socket`
|
||||||
|
- **v2**: `github.com/taigrr/log-socket/v2`
|
||||||
|
|
||||||
|
### Namespace Support
|
||||||
|
|
||||||
|
**Core concept**: Namespaces allow organizing logs by component, service, or domain (e.g., "api", "database", "auth").
|
||||||
|
|
||||||
|
#### Types Changes
|
||||||
|
|
||||||
|
**Entry** now includes:
|
||||||
|
```go
|
||||||
|
type Entry struct {
|
||||||
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
Output string `json:"output"`
|
||||||
|
File string `json:"file"`
|
||||||
|
Level string `json:"level"`
|
||||||
|
Namespace string `json:"namespace"` // NEW
|
||||||
|
level Level
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Client** now uses a slice for filtering:
|
||||||
|
```go
|
||||||
|
type Client struct {
|
||||||
|
LogLevel Level `json:"level"`
|
||||||
|
Namespaces []string `json:"namespaces"` // Empty = all namespaces
|
||||||
|
writer LogWriter
|
||||||
|
initialized bool
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Logger** has namespace field:
|
||||||
|
```go
|
||||||
|
type Logger struct {
|
||||||
|
FileInfoDepth int
|
||||||
|
Namespace string // NEW
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### API Changes
|
||||||
|
|
||||||
|
**CreateClient** now variadic:
|
||||||
|
```go
|
||||||
|
// v1
|
||||||
|
func CreateClient() *Client
|
||||||
|
|
||||||
|
// v2
|
||||||
|
func CreateClient(namespaces ...string) *Client
|
||||||
|
|
||||||
|
// Examples:
|
||||||
|
client := log.CreateClient() // All namespaces
|
||||||
|
client := log.CreateClient("api") // Single namespace
|
||||||
|
client := log.CreateClient("api", "database") // Multiple namespaces
|
||||||
|
```
|
||||||
|
|
||||||
|
**NewLogger** constructor added:
|
||||||
|
```go
|
||||||
|
func NewLogger(namespace string) *Logger
|
||||||
|
|
||||||
|
// Example:
|
||||||
|
apiLogger := log.NewLogger("api")
|
||||||
|
apiLogger.Info("API request received")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Namespace Tracking
|
||||||
|
|
||||||
|
Global namespace registry tracks all used namespaces:
|
||||||
|
```go
|
||||||
|
var (
|
||||||
|
namespaces map[string]bool
|
||||||
|
namespacesMux sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetNamespaces() []string
|
||||||
|
```
|
||||||
|
|
||||||
|
Namespaces are automatically registered when logs are created.
|
||||||
|
|
||||||
|
### WebSocket Changes
|
||||||
|
|
||||||
|
**Query parameter for filtering**:
|
||||||
|
```
|
||||||
|
ws://localhost:8080/ws?namespaces=api,database
|
||||||
|
```
|
||||||
|
|
||||||
|
The handler parses comma-separated namespace list and creates a filtered client.
|
||||||
|
|
||||||
|
### Web UI Changes
|
||||||
|
|
||||||
|
- Namespace filter input field added to controls
|
||||||
|
- Namespace column added to log table
|
||||||
|
- Reconnect button to apply namespace filter
|
||||||
|
- WebSocket URL includes namespace query parameter
|
||||||
|
|
||||||
|
## Code Organization & Architecture
|
||||||
|
|
||||||
|
### Log Package (`log/`)
|
||||||
|
|
||||||
|
Dual API remains, but with namespace support:
|
||||||
|
|
||||||
|
1. **Package-level functions**: Use "default" namespace
|
||||||
|
- `log.Info()`, `log.Debug()`, etc.
|
||||||
|
- All entry creations include `Namespace: DefaultNamespace`
|
||||||
|
|
||||||
|
2. **Logger instances**: Use custom namespace
|
||||||
|
- Create with `log.NewLogger(namespace)`
|
||||||
|
- All entry creations include `Namespace: l.Namespace`
|
||||||
|
|
||||||
|
### Client Architecture (Updated)
|
||||||
|
|
||||||
|
**Client filtering by namespace**:
|
||||||
|
|
||||||
|
1. **Empty Namespaces slice**: Receives all logs regardless of namespace
|
||||||
|
2. **Non-empty Namespaces**: Only receives logs matching one of the specified namespaces
|
||||||
|
|
||||||
|
**matchesNamespace helper**:
|
||||||
|
```go
|
||||||
|
func (c *Client) matchesNamespace(namespace string) bool {
|
||||||
|
// Empty Namespaces slice means match all
|
||||||
|
if len(c.Namespaces) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
for _, ns := range c.Namespaces {
|
||||||
|
if ns == namespace {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Entry flow with namespace filtering**:
|
||||||
|
1. Log function called with namespace
|
||||||
|
2. `Entry` created with namespace field
|
||||||
|
3. Namespace registered in global map
|
||||||
|
4. `createLog()` sends to all clients
|
||||||
|
5. Each client checks `matchesNamespace()`
|
||||||
|
6. Only matching clients receive the entry
|
||||||
|
|
||||||
|
### WebSocket Handler (`ws/`)
|
||||||
|
|
||||||
|
**Namespace parameter parsing**:
|
||||||
|
```go
|
||||||
|
namespacesParam := r.URL.Query().Get("namespaces")
|
||||||
|
var namespaces []string
|
||||||
|
if namespacesParam != "" {
|
||||||
|
namespaces = strings.Split(namespacesParam, ",")
|
||||||
|
}
|
||||||
|
lc := logger.CreateClient(namespaces...)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Namespaces API handler** (`ws/namespaces.go`):
|
||||||
|
```go
|
||||||
|
func NamespacesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
namespaces := logger.GetNamespaces()
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||||
|
"namespaces": namespaces,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Browser Package (`browser/`)
|
||||||
|
|
||||||
|
Still embeds viewer.html, but HTML now includes:
|
||||||
|
- Namespace filter input
|
||||||
|
- Namespace column in grid (5 columns instead of 4)
|
||||||
|
- `reconnectWithNamespace()` method
|
||||||
|
- WebSocket URL construction with query parameter
|
||||||
|
|
||||||
|
## Go Version & Dependencies
|
||||||
|
|
||||||
|
- **Go version**: 1.26.1 (specified in go.mod)
|
||||||
|
- **Only external dependency**: `github.com/gorilla/websocket v1.5.3`
|
||||||
|
|
||||||
|
## Naming Conventions & Style
|
||||||
|
|
||||||
|
### Namespaces
|
||||||
|
- Use lowercase strings: `"api"`, `"database"`, `"auth"`, `"cache"`
|
||||||
|
- Default constant: `DefaultNamespace = "default"`
|
||||||
|
- Comma-separated in query params: `?namespaces=api,database`
|
||||||
|
|
||||||
|
### Log Levels
|
||||||
|
Unchanged from v1 - still use uppercase strings and iota constants.
|
||||||
|
|
||||||
|
### Variable Names
|
||||||
|
- Use descriptive names (`apiLogger`, `dbLogger`, `namespaces`)
|
||||||
|
- Exception: Loop variables, short-lived scopes
|
||||||
|
|
||||||
|
## Important Patterns & Gotchas
|
||||||
|
|
||||||
|
### 1. Namespace Tracking is Automatic
|
||||||
|
When any log is created, its namespace is automatically added to the global registry:
|
||||||
|
```go
|
||||||
|
func createLog(e Entry) {
|
||||||
|
// Track namespace
|
||||||
|
namespacesMux.Lock()
|
||||||
|
namespaces[e.Namespace] = true
|
||||||
|
namespacesMux.Unlock()
|
||||||
|
// ... rest of function
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**No manual registration needed** - just log and the namespace appears.
|
||||||
|
|
||||||
|
### 2. Empty Namespace List = All Logs
|
||||||
|
Both for clients and WebSocket connections:
|
||||||
|
```go
|
||||||
|
client := log.CreateClient() // Gets ALL logs
|
||||||
|
ws://localhost:8080/ws // Gets ALL logs
|
||||||
|
```
|
||||||
|
|
||||||
|
This is the default behavior to maintain backward compatibility.
|
||||||
|
|
||||||
|
### 3. Client Namespace Filtering is Inclusive (OR)
|
||||||
|
If a client has multiple namespaces, it receives logs matching ANY of them:
|
||||||
|
```go
|
||||||
|
client := log.CreateClient("api", "database")
|
||||||
|
// Receives logs from "api" OR "database", not "auth"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Namespace Field Always Set
|
||||||
|
All logging functions set namespace:
|
||||||
|
- Package functions: `DefaultNamespace`
|
||||||
|
- Logger methods: `l.Namespace`
|
||||||
|
|
||||||
|
**Never nil or empty** - there's always a namespace.
|
||||||
|
|
||||||
|
### 5. WebSocket Namespace Reconnection
|
||||||
|
Changing namespace filter requires reconnecting WebSocket:
|
||||||
|
```javascript
|
||||||
|
reconnectWithNamespace() {
|
||||||
|
if (this.ws) {
|
||||||
|
this.ws.onclose = null; // Prevent auto-reconnect
|
||||||
|
this.ws.close();
|
||||||
|
this.ws = null;
|
||||||
|
}
|
||||||
|
this.reconnectAttempts = 0;
|
||||||
|
this.connectWebSocket(); // Creates new connection with new filter
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
UI provides "Reconnect" button for this purpose.
|
||||||
|
|
||||||
|
### 6. Stderr Client Uses All Namespaces
|
||||||
|
The built-in stderr client (created in `init()`) listens to all namespaces:
|
||||||
|
```go
|
||||||
|
stderrClient = CreateClient() // No args = all namespaces
|
||||||
|
```
|
||||||
|
|
||||||
|
It prints logs matching its level and namespace filter in `logStdErr()`:
|
||||||
|
```go
|
||||||
|
if e.level >= c.LogLevel && c.matchesNamespace(e.Namespace) {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s\t%s\t[%s]\t%s\t%s\n", ...)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Since `CreateClient()` is called with no arguments, the Namespaces slice is empty, which means it matches all namespaces.
|
||||||
|
|
||||||
|
### 7. Grid Layout Updated
|
||||||
|
The log viewer grid changed from 4 to 5 columns:
|
||||||
|
```css
|
||||||
|
/* v1 */
|
||||||
|
grid-template-columns: 180px 80px 1fr 120px;
|
||||||
|
|
||||||
|
/* v2 */
|
||||||
|
grid-template-columns: 180px 80px 100px 1fr 120px;
|
||||||
|
```
|
||||||
|
|
||||||
|
Order: Timestamp, Level, Namespace, Message, Source
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
### Test Updates for v2
|
||||||
|
|
||||||
|
All `CreateClient()` calls in tests now pass namespace:
|
||||||
|
```go
|
||||||
|
// v1
|
||||||
|
c := CreateClient()
|
||||||
|
|
||||||
|
// v2
|
||||||
|
c := CreateClient("test")
|
||||||
|
c := CreateClient(DefaultNamespace)
|
||||||
|
```
|
||||||
|
|
||||||
|
Tests verify namespace appears in output (see stderr format).
|
||||||
|
|
||||||
|
### Running Tests
|
||||||
|
```bash
|
||||||
|
go test -v ./...
|
||||||
|
```
|
||||||
|
|
||||||
|
All existing tests pass with namespace support added.
|
||||||
|
|
||||||
|
## CI/CD
|
||||||
|
|
||||||
|
GitHub Actions workflow (`.github/workflows/ci.yaml`):
|
||||||
|
- Uses Go 1.26, actions/checkout@v4, actions/setup-go@v5
|
||||||
|
- Runs tests with `-race` flag
|
||||||
|
- Triggers on push and pull_request
|
||||||
|
|
||||||
|
## Common Tasks
|
||||||
|
|
||||||
|
### Adding a New Namespace
|
||||||
|
|
||||||
|
No code changes needed! Just create a logger:
|
||||||
|
```go
|
||||||
|
cacheLogger := log.NewLogger("cache")
|
||||||
|
cacheLogger.Info("Cache initialized")
|
||||||
|
```
|
||||||
|
|
||||||
|
Namespace automatically tracked and available via API.
|
||||||
|
|
||||||
|
### Creating Namespace-Specific Client
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Subscribe only to API logs
|
||||||
|
apiClient := log.CreateClient("api")
|
||||||
|
defer apiClient.Destroy()
|
||||||
|
|
||||||
|
for {
|
||||||
|
entry := apiClient.Get()
|
||||||
|
// Only receives logs from "api" namespace
|
||||||
|
processAPILog(entry)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Filtering WebSocket by Namespace
|
||||||
|
|
||||||
|
Frontend:
|
||||||
|
```javascript
|
||||||
|
// Set namespace filter
|
||||||
|
document.getElementById('namespaceFilter').value = 'api,database';
|
||||||
|
// Click reconnect button or call:
|
||||||
|
logViewer.reconnectWithNamespace();
|
||||||
|
```
|
||||||
|
|
||||||
|
Backend automatically creates filtered client based on query param.
|
||||||
|
|
||||||
|
### Getting All Active Namespaces
|
||||||
|
|
||||||
|
```go
|
||||||
|
namespaces := log.GetNamespaces()
|
||||||
|
// Returns: ["default", "api", "database", "auth", ...]
|
||||||
|
```
|
||||||
|
|
||||||
|
Or via HTTP:
|
||||||
|
```bash
|
||||||
|
GET /api/namespaces
|
||||||
|
```
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"namespaces": ["default", "api", "database", "auth"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Migration from v1 to v2
|
||||||
|
|
||||||
|
### Import Paths
|
||||||
|
```go
|
||||||
|
// v1
|
||||||
|
import "github.com/taigrr/log-socket/log"
|
||||||
|
import "github.com/taigrr/log-socket/ws"
|
||||||
|
import "github.com/taigrr/log-socket/browser"
|
||||||
|
|
||||||
|
// v2
|
||||||
|
import "github.com/taigrr/log-socket/v2/log"
|
||||||
|
import "github.com/taigrr/log-socket/v2/ws"
|
||||||
|
import "github.com/taigrr/log-socket/v2/browser"
|
||||||
|
```
|
||||||
|
|
||||||
|
### CreateClient Calls
|
||||||
|
```go
|
||||||
|
// v1
|
||||||
|
client := log.CreateClient()
|
||||||
|
|
||||||
|
// v2 - same behavior
|
||||||
|
client := log.CreateClient() // Empty = all namespaces
|
||||||
|
|
||||||
|
// v2 - new filtering capability
|
||||||
|
client := log.CreateClient("api")
|
||||||
|
client := log.CreateClient("api", "database")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Default() Logger
|
||||||
|
```go
|
||||||
|
// v1
|
||||||
|
logger := log.Default()
|
||||||
|
|
||||||
|
// v2 - uses default namespace
|
||||||
|
logger := log.Default()
|
||||||
|
|
||||||
|
// v2 - new namespaced option
|
||||||
|
logger := log.NewLogger("api")
|
||||||
|
```
|
||||||
|
|
||||||
|
### WebSocket URL
|
||||||
|
```
|
||||||
|
v1: ws://host/ws
|
||||||
|
v2: ws://host/ws # All namespaces (backward compatible)
|
||||||
|
v2: ws://host/ws?namespaces=api # Filtered
|
||||||
|
v2: ws://host/ws?namespaces=api,database # Multiple
|
||||||
|
```
|
||||||
|
|
||||||
|
## Repository Context
|
||||||
|
|
||||||
|
- **License**: Check LICENSE file
|
||||||
|
- **Funding**: GitHub sponsors (.github/FUNDING.yml)
|
||||||
|
- **Open Source**: github.com/taigrr/log-socket
|
||||||
|
- **Version**: v2.x.x (major version bump for breaking changes)
|
||||||
|
|
||||||
|
## Future Agent Notes
|
||||||
|
|
||||||
|
- **v2 is a breaking change**: Major version bump follows Go modules convention
|
||||||
|
- Namespace support is the primary new feature
|
||||||
|
- Backward compatible behavior: empty namespace list = all logs
|
||||||
|
- Namespace tracking is automatic via global registry
|
||||||
|
- Web UI has been significantly updated for namespace support
|
||||||
|
- Possible bug: stderr client might need to use `CreateClient()` instead of `CreateClient(DefaultNamespace)` to see all logs
|
||||||
|
- All tests updated and passing
|
||||||
|
- Example in main.go demonstrates multiple namespaces
|
||||||
259
README.md
259
README.md
@@ -1,11 +1,27 @@
|
|||||||
# Log Socket
|
# Log Socket v2
|
||||||
|
|
||||||
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.
|
A real-time log viewer with WebSocket support and namespace filtering, written in Go.
|
||||||
|
|
||||||
|
## What's New in v2
|
||||||
|
|
||||||
|
**Breaking Changes:**
|
||||||
|
- Module path changed to `github.com/taigrr/log-socket/v2`
|
||||||
|
- `CreateClient()` now accepts variadic `namespaces ...string` parameter
|
||||||
|
- `Logger` type now includes `Namespace` field
|
||||||
|
- New `NewLogger(namespace string)` constructor for namespaced loggers
|
||||||
|
|
||||||
|
**New Features:**
|
||||||
|
- **Namespace support**: Organize logs by namespace (api, database, auth, etc.)
|
||||||
|
- **Namespace filtering**: Subscribe to specific namespaces via WebSocket
|
||||||
|
- **Frontend namespace selector**: Filter logs by namespace in the web UI
|
||||||
|
- **Namespace API**: GET `/api/namespaces` to list all active namespaces
|
||||||
|
- **Colored terminal output**: Log levels are color-coded in terminal (no external packages)
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Real-time log streaming via WebSocket
|
- Real-time log streaming via WebSocket
|
||||||
- Web-based log viewer with filtering capabilities
|
- Web-based log viewer with filtering capabilities
|
||||||
|
- **Namespace-based log organization**
|
||||||
- Support for multiple log levels (TRACE, DEBUG, INFO, WARN, ERROR, PANIC, FATAL)
|
- Support for multiple log levels (TRACE, DEBUG, INFO, WARN, ERROR, PANIC, FATAL)
|
||||||
- Color-coded log levels for better visibility
|
- Color-coded log levels for better visibility
|
||||||
- Auto-scrolling with toggle option
|
- Auto-scrolling with toggle option
|
||||||
@@ -16,47 +32,228 @@ A real-time log viewer with WebSocket support, written in Go. This tool provides
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go install github.com/taigrr/log-socket@latest
|
go install github.com/taigrr/log-socket/v2@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example Preview
|
## Quick Start
|
||||||
|
|
||||||
1. Start the server:
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
```bash
|
import (
|
||||||
log-socket
|
"net/http"
|
||||||
```
|
logger "github.com/taigrr/log-socket/v2/log"
|
||||||
|
"github.com/taigrr/log-socket/v2/ws"
|
||||||
|
"github.com/taigrr/log-socket/v2/browser"
|
||||||
|
)
|
||||||
|
|
||||||
By default, the server runs on `0.0.0.0:8080`. You can specify a different address using the `-addr` flag:
|
func main() {
|
||||||
|
defer logger.Flush()
|
||||||
|
|
||||||
|
// Set up HTTP handlers
|
||||||
|
http.HandleFunc("/ws", ws.LogSocketHandler)
|
||||||
|
http.HandleFunc("/api/namespaces", ws.NamespacesHandler)
|
||||||
|
http.HandleFunc("/", browser.LogSocketViewHandler)
|
||||||
|
|
||||||
|
// Use default namespace
|
||||||
|
logger.Info("Application started")
|
||||||
|
|
||||||
|
// Create namespaced loggers
|
||||||
|
apiLogger := logger.NewLogger("api")
|
||||||
|
dbLogger := logger.NewLogger("database")
|
||||||
|
|
||||||
|
apiLogger.Info("API server ready")
|
||||||
|
dbLogger.Debug("Database connected")
|
||||||
|
|
||||||
|
logger.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
```bash
|
## Usage
|
||||||
log-socket -addr localhost:8080
|
|
||||||
```
|
|
||||||
|
|
||||||
2. Open your browser and navigate to `http://localhost:8080`
|
### Starting the Server
|
||||||
|
|
||||||

|
```bash
|
||||||
|
log-socket
|
||||||
|
```
|
||||||
|
|
||||||
## Logging Interface
|
By default, the server runs on `0.0.0.0:8080`. Specify a different address:
|
||||||
|
|
||||||
The package provides a comprehensive logging interface with the following methods:
|
```bash
|
||||||
|
log-socket -addr localhost:8080
|
||||||
|
```
|
||||||
|
|
||||||
- `Trace/Tracef/Traceln`: For trace-level logging
|
### Web Interface
|
||||||
- `Debug/Debugf/Debugln`: For debug-level logging
|
|
||||||
- `Info/Infof/Infoln`: For info-level logging
|
Open your browser and navigate to `http://localhost:8080`
|
||||||
- `Notice/Noticef/Noticeln`: For notice-level logging
|
|
||||||
- `Warn/Warnf/Warnln`: For warning-level logging
|
**Namespace Filtering:**
|
||||||
- `Error/Errorf/Errorln`: For error-level logging
|
- The namespace dropdown is automatically populated from `/api/namespaces`
|
||||||
- `Panic/Panicf/Panicln`: For panic-level logging
|
- Select one or more namespaces to filter (hold Ctrl/Cmd to multi-select)
|
||||||
- `Fatal/Fatalf/Fatalln`: For fatal-level logging
|
- Default is "All Namespaces" (shows everything)
|
||||||
|
- Click "Reconnect" to apply the filter
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### Logging Interface
|
||||||
|
|
||||||
|
The package provides two ways to log:
|
||||||
|
|
||||||
|
#### 1. Package-level functions (default namespace)
|
||||||
|
|
||||||
|
```go
|
||||||
|
logger.Trace("trace message")
|
||||||
|
logger.Debug("debug message")
|
||||||
|
logger.Info("info message")
|
||||||
|
logger.Notice("notice message")
|
||||||
|
logger.Warn("warning message")
|
||||||
|
logger.Error("error message")
|
||||||
|
logger.Panic("panic message") // Logs and panics
|
||||||
|
logger.Fatal("fatal message") // Logs and exits
|
||||||
|
```
|
||||||
|
|
||||||
|
Each has formatted (`f`) and line (`ln`) variants:
|
||||||
|
```go
|
||||||
|
logger.Infof("User %s logged in", username)
|
||||||
|
logger.Infoln("This adds a newline")
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Namespaced loggers
|
||||||
|
|
||||||
|
```go
|
||||||
|
apiLogger := logger.NewLogger("api")
|
||||||
|
apiLogger.Info("Request received")
|
||||||
|
|
||||||
|
dbLogger := logger.NewLogger("database")
|
||||||
|
dbLogger.Warn("Slow query detected")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Creating Clients with Namespace Filters
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Listen to all namespaces
|
||||||
|
client := logger.CreateClient()
|
||||||
|
|
||||||
|
// Listen to specific namespace
|
||||||
|
client := logger.CreateClient("api")
|
||||||
|
|
||||||
|
// Listen to multiple namespaces
|
||||||
|
client := logger.CreateClient("api", "database", "auth")
|
||||||
|
```
|
||||||
|
|
||||||
|
### WebSocket API
|
||||||
|
|
||||||
|
#### Log Stream Endpoint
|
||||||
|
|
||||||
|
**URL:** `ws://localhost:8080/ws`
|
||||||
|
|
||||||
|
**Query Parameters:**
|
||||||
|
- `namespaces` (optional): Comma-separated list of namespaces to filter
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
```
|
||||||
|
ws://localhost:8080/ws # All namespaces
|
||||||
|
ws://localhost:8080/ws?namespaces=api # Only "api" namespace
|
||||||
|
ws://localhost:8080/ws?namespaces=api,database # Multiple namespaces
|
||||||
|
```
|
||||||
|
|
||||||
|
**Message Format:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"timestamp": "2024-11-10T15:42:49.777298-05:00",
|
||||||
|
"output": "API request received",
|
||||||
|
"file": "main.go:42",
|
||||||
|
"level": "INFO",
|
||||||
|
"namespace": "api"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Namespaces List Endpoint
|
||||||
|
|
||||||
|
**URL:** `GET http://localhost:8080/api/namespaces`
|
||||||
|
|
||||||
|
**Response:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"namespaces": ["default", "api", "database", "auth"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Web Interface Features
|
## Web Interface Features
|
||||||
|
|
||||||
- **Filtering**: Type in the search box to filter logs
|
- **Namespace Dropdown**: Dynamically populated from `/api/namespaces`, multi-select support
|
||||||
- **Auto-scroll**: Toggle auto-scrolling with the checkbox
|
- **Text Search**: Filter logs by content, level, namespace, or source file
|
||||||
|
- **Auto-scroll**: Toggle auto-scrolling with checkbox
|
||||||
- **Download**: Save all logs as a JSON file
|
- **Download**: Save all logs as a JSON file
|
||||||
- **Clear**: Remove all logs from the viewer
|
- **Clear**: Remove all logs from the viewer
|
||||||
- **Color Coding**: Different log levels are color-coded for easy identification
|
- **Color Coding**: Different log levels are color-coded
|
||||||
|
- **Reconnect**: Reconnect WebSocket with new namespace filter
|
||||||
|
|
||||||
|
## Terminal Colors
|
||||||
|
|
||||||
|
Log output to stderr is automatically colorized when writing to a terminal. Colors are disabled when output is piped or redirected to a file.
|
||||||
|
|
||||||
|
### Color Scheme
|
||||||
|
|
||||||
|
| Level | Color |
|
||||||
|
|--------|--------------|
|
||||||
|
| TRACE | Gray |
|
||||||
|
| DEBUG | Cyan |
|
||||||
|
| INFO | Green |
|
||||||
|
| NOTICE | Blue |
|
||||||
|
| WARN | Yellow |
|
||||||
|
| ERROR | Red |
|
||||||
|
| PANIC | Bold Red |
|
||||||
|
| FATAL | Bold Red |
|
||||||
|
|
||||||
|
### Controlling Colors
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Disable colors (e.g., for CI/CD or file output)
|
||||||
|
logger.SetColorEnabled(false)
|
||||||
|
|
||||||
|
// Enable colors explicitly
|
||||||
|
logger.SetColorEnabled(true)
|
||||||
|
|
||||||
|
// Check current state
|
||||||
|
if logger.ColorEnabled() {
|
||||||
|
// colors are on
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Colors are implemented using standard ANSI escape codes with no external dependencies.
|
||||||
|
|
||||||
|
## Migration from v1
|
||||||
|
|
||||||
|
### Import Path
|
||||||
|
|
||||||
|
```go
|
||||||
|
// v1
|
||||||
|
import "github.com/taigrr/log-socket/log"
|
||||||
|
|
||||||
|
// v2
|
||||||
|
import "github.com/taigrr/log-socket/v2/log"
|
||||||
|
```
|
||||||
|
|
||||||
|
### CreateClient Changes
|
||||||
|
|
||||||
|
```go
|
||||||
|
// v1
|
||||||
|
client := log.CreateClient()
|
||||||
|
|
||||||
|
// v2 - specify namespace(s) or leave empty for all
|
||||||
|
client := log.CreateClient() // All namespaces
|
||||||
|
client := log.CreateClient("api") // Single namespace
|
||||||
|
client := log.CreateClient("api", "db") // Multiple namespaces
|
||||||
|
```
|
||||||
|
|
||||||
|
### New Logger Constructor
|
||||||
|
|
||||||
|
```go
|
||||||
|
// v2 only - create namespaced logger
|
||||||
|
apiLogger := log.NewLogger("api")
|
||||||
|
apiLogger.Info("Message in api namespace")
|
||||||
|
```
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
@@ -64,6 +261,8 @@ The package provides a comprehensive logging interface with the following method
|
|||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
The web interface is not meant to be used as-is.
|
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.
|
||||||
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.
|
|
||||||
It's mostly here to provide an example of how to consume the websocket data and display it.
|
## License
|
||||||
|
|
||||||
|
See LICENSE file for details.
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 196 KiB After Width: | Height: | Size: 117 KiB |
@@ -126,7 +126,7 @@
|
|||||||
|
|
||||||
.log-row {
|
.log-row {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: 180px 80px 1fr 120px;
|
grid-template-columns: 180px 80px 100px 1fr 120px;
|
||||||
gap: 15px;
|
gap: 15px;
|
||||||
padding: 10px 15px;
|
padding: 10px 15px;
|
||||||
border-bottom: 1px solid var(--border-color);
|
border-bottom: 1px solid var(--border-color);
|
||||||
@@ -321,6 +321,17 @@
|
|||||||
<header class="header">
|
<header class="header">
|
||||||
<h1>📊 Log Viewer</h1>
|
<h1>📊 Log Viewer</h1>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
|
<div class="search-container">
|
||||||
|
<select
|
||||||
|
id="namespaceFilter"
|
||||||
|
class="search-input"
|
||||||
|
multiple
|
||||||
|
size="1"
|
||||||
|
aria-label="Filter by namespace"
|
||||||
|
>
|
||||||
|
<option value="">All Namespaces (loading...)</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
<div class="search-container">
|
<div class="search-container">
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
@@ -334,6 +345,9 @@
|
|||||||
<input type="checkbox" id="shouldScroll" checked>
|
<input type="checkbox" id="shouldScroll" checked>
|
||||||
<label for="shouldScroll">Auto-scroll</label>
|
<label for="shouldScroll">Auto-scroll</label>
|
||||||
</div>
|
</div>
|
||||||
|
<button id="reconnectBtn" class="btn btn-primary">
|
||||||
|
🔄 Reconnect
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
@@ -342,6 +356,7 @@
|
|||||||
<div class="log-row">
|
<div class="log-row">
|
||||||
<div class="log-cell">Timestamp</div>
|
<div class="log-cell">Timestamp</div>
|
||||||
<div class="log-cell">Level</div>
|
<div class="log-cell">Level</div>
|
||||||
|
<div class="log-cell">Namespace</div>
|
||||||
<div class="log-cell">Message</div>
|
<div class="log-cell">Message</div>
|
||||||
<div class="log-cell">Source</div>
|
<div class="log-cell">Source</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -385,6 +400,7 @@
|
|||||||
|
|
||||||
this.initializeElements();
|
this.initializeElements();
|
||||||
this.attachEventListeners();
|
this.attachEventListeners();
|
||||||
|
this.fetchNamespaces();
|
||||||
this.connectWebSocket();
|
this.connectWebSocket();
|
||||||
this.startAutoScroll();
|
this.startAutoScroll();
|
||||||
}
|
}
|
||||||
@@ -392,10 +408,12 @@
|
|||||||
initializeElements() {
|
initializeElements() {
|
||||||
this.logViewer = document.getElementById('logViewer');
|
this.logViewer = document.getElementById('logViewer');
|
||||||
this.emptyState = document.getElementById('emptyState');
|
this.emptyState = document.getElementById('emptyState');
|
||||||
|
this.namespaceFilter = document.getElementById('namespaceFilter');
|
||||||
this.searchInput = document.getElementById('search');
|
this.searchInput = document.getElementById('search');
|
||||||
this.scrollCheckbox = document.getElementById('shouldScroll');
|
this.scrollCheckbox = document.getElementById('shouldScroll');
|
||||||
this.downloadBtn = document.getElementById('downloadBtn');
|
this.downloadBtn = document.getElementById('downloadBtn');
|
||||||
this.clearBtn = document.getElementById('clearBtn');
|
this.clearBtn = document.getElementById('clearBtn');
|
||||||
|
this.reconnectBtn = document.getElementById('reconnectBtn');
|
||||||
this.statusIndicator = document.getElementById('statusIndicator');
|
this.statusIndicator = document.getElementById('statusIndicator');
|
||||||
this.connectionStatus = document.getElementById('connectionStatus');
|
this.connectionStatus = document.getElementById('connectionStatus');
|
||||||
this.logCount = document.getElementById('logCount');
|
this.logCount = document.getElementById('logCount');
|
||||||
@@ -403,15 +421,61 @@
|
|||||||
|
|
||||||
attachEventListeners() {
|
attachEventListeners() {
|
||||||
this.searchInput.addEventListener('input', this.debounce(() => this.filterLogs(), 300));
|
this.searchInput.addEventListener('input', this.debounce(() => this.filterLogs(), 300));
|
||||||
|
this.namespaceFilter.addEventListener('change', () => this.reconnectWithNamespace());
|
||||||
this.downloadBtn.addEventListener('click', () => this.downloadLogs());
|
this.downloadBtn.addEventListener('click', () => this.downloadLogs());
|
||||||
this.clearBtn.addEventListener('click', () => this.clearLogs());
|
this.clearBtn.addEventListener('click', () => this.clearLogs());
|
||||||
|
this.reconnectBtn.addEventListener('click', () => this.reconnectWithNamespace());
|
||||||
|
}
|
||||||
|
|
||||||
|
async fetchNamespaces() {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/namespaces');
|
||||||
|
const data = await response.json();
|
||||||
|
this.updateNamespaceFilter(data.namespaces || []);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch namespaces:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
updateNamespaceFilter(namespaces) {
|
||||||
|
// Clear existing options
|
||||||
|
this.namespaceFilter.innerHTML = '';
|
||||||
|
|
||||||
|
// Add "All" option
|
||||||
|
const allOption = document.createElement('option');
|
||||||
|
allOption.value = '';
|
||||||
|
allOption.textContent = 'All Namespaces';
|
||||||
|
allOption.selected = true;
|
||||||
|
this.namespaceFilter.appendChild(allOption);
|
||||||
|
|
||||||
|
// Add namespace options
|
||||||
|
namespaces.sort().forEach(ns => {
|
||||||
|
const option = document.createElement('option');
|
||||||
|
option.value = ns;
|
||||||
|
option.textContent = ns;
|
||||||
|
this.namespaceFilter.appendChild(option);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
connectWebSocket() {
|
connectWebSocket() {
|
||||||
if (this.ws) return;
|
if (this.ws) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.ws = new WebSocket("{{.}}");
|
let wsUrl = "{{.}}";
|
||||||
|
|
||||||
|
// Get selected namespace options from multi-select
|
||||||
|
const selectedOptions = Array.from(this.namespaceFilter.selectedOptions)
|
||||||
|
.map(opt => opt.value)
|
||||||
|
.filter(val => val !== ''); // Remove empty "All" option
|
||||||
|
|
||||||
|
// Add namespace filter if specific namespaces selected
|
||||||
|
if (selectedOptions.length > 0) {
|
||||||
|
const namespaces = selectedOptions.join(',');
|
||||||
|
const separator = wsUrl.includes('?') ? '&' : '?';
|
||||||
|
wsUrl += `${separator}namespaces=${encodeURIComponent(namespaces)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.ws = new WebSocket(wsUrl);
|
||||||
this.updateConnectionStatus('Connecting...', false);
|
this.updateConnectionStatus('Connecting...', false);
|
||||||
|
|
||||||
this.ws.onopen = () => {
|
this.ws.onopen = () => {
|
||||||
@@ -448,6 +512,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reconnectWithNamespace() {
|
||||||
|
if (this.ws) {
|
||||||
|
this.ws.onclose = null; // Prevent auto-reconnect
|
||||||
|
this.ws.close();
|
||||||
|
this.ws = null;
|
||||||
|
}
|
||||||
|
this.reconnectAttempts = 0;
|
||||||
|
this.connectWebSocket();
|
||||||
|
}
|
||||||
|
|
||||||
scheduleReconnect() {
|
scheduleReconnect() {
|
||||||
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
||||||
this.reconnectAttempts++;
|
this.reconnectAttempts++;
|
||||||
@@ -480,6 +554,7 @@
|
|||||||
logRow.innerHTML = `
|
logRow.innerHTML = `
|
||||||
<div class="log-cell timestamp">${this.formatTimestamp(entry.timestamp)}</div>
|
<div class="log-cell timestamp">${this.formatTimestamp(entry.timestamp)}</div>
|
||||||
<div class="log-cell level">${entry.level}</div>
|
<div class="log-cell level">${entry.level}</div>
|
||||||
|
<div class="log-cell namespace">${this.escapeHtml(entry.namespace || 'default')}</div>
|
||||||
<div class="log-cell output">${this.escapeHtml(entry.output)}</div>
|
<div class="log-cell output">${this.escapeHtml(entry.output)}</div>
|
||||||
<div class="log-cell source">${this.escapeHtml(entry.file || 'N/A')}</div>
|
<div class="log-cell source">${this.escapeHtml(entry.file || 'N/A')}</div>
|
||||||
`;
|
`;
|
||||||
@@ -536,6 +611,7 @@
|
|||||||
return (
|
return (
|
||||||
entry.output.toLowerCase().includes(query) ||
|
entry.output.toLowerCase().includes(query) ||
|
||||||
entry.level.toLowerCase().includes(query) ||
|
entry.level.toLowerCase().includes(query) ||
|
||||||
|
(entry.namespace && entry.namespace.toLowerCase().includes(query)) ||
|
||||||
(entry.file && entry.file.toLowerCase().includes(query)) ||
|
(entry.file && entry.file.toLowerCase().includes(query)) ||
|
||||||
entry.timestamp.toLowerCase().includes(query)
|
entry.timestamp.toLowerCase().includes(query)
|
||||||
);
|
);
|
||||||
|
|||||||
36
examples/basic/main.go
Normal file
36
examples/basic/main.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
// Example: basic usage of log-socket as a drop-in logger.
|
||||||
|
//
|
||||||
|
// This demonstrates using the package-level logging functions,
|
||||||
|
// which work similarly to the standard library's log package.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/taigrr/log-socket/v2/browser"
|
||||||
|
logger "github.com/taigrr/log-socket/v2/log"
|
||||||
|
"github.com/taigrr/log-socket/v2/ws"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer logger.Flush()
|
||||||
|
|
||||||
|
// Set the minimum log level (default is LTrace, showing everything)
|
||||||
|
logger.SetLogLevel(logger.LDebug)
|
||||||
|
|
||||||
|
// Package-level functions log to the "default" namespace
|
||||||
|
logger.Info("Application starting up")
|
||||||
|
logger.Debug("Debug mode enabled")
|
||||||
|
logger.Warnf("Config file not found at %s, using defaults", "/etc/app/config.yaml")
|
||||||
|
logger.Errorf("Failed to connect to database: %s", "connection refused")
|
||||||
|
|
||||||
|
// Print/Printf/Println are aliases for Info
|
||||||
|
logger.Println("This is equivalent to Infoln")
|
||||||
|
|
||||||
|
// Start the web UI so you can view logs at http://localhost:8080
|
||||||
|
http.HandleFunc("/ws", ws.LogSocketHandler)
|
||||||
|
http.HandleFunc("/", browser.LogSocketViewHandler)
|
||||||
|
fmt.Println("Log viewer available at http://localhost:8080")
|
||||||
|
logger.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
|
||||||
|
}
|
||||||
63
examples/client/main.go
Normal file
63
examples/client/main.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
// Example: programmatic log client with namespace filtering.
|
||||||
|
//
|
||||||
|
// This shows how to create a Client that receives log entries
|
||||||
|
// programmatically, optionally filtered to specific namespaces.
|
||||||
|
// Useful for building custom log processors, alerting, or forwarding.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
logger "github.com/taigrr/log-socket/v2/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer logger.Flush()
|
||||||
|
|
||||||
|
// Create a client that receives ALL log entries
|
||||||
|
allLogs := logger.CreateClient()
|
||||||
|
allLogs.SetLogLevel(logger.LInfo)
|
||||||
|
|
||||||
|
// Create a client that only receives "database" and "auth" logs
|
||||||
|
securityLogs := logger.CreateClient("database", "auth")
|
||||||
|
securityLogs.SetLogLevel(logger.LWarn) // Only warnings and above
|
||||||
|
|
||||||
|
dbLog := logger.NewLogger("database")
|
||||||
|
authLog := logger.NewLogger("auth")
|
||||||
|
apiLog := logger.NewLogger("api")
|
||||||
|
|
||||||
|
// Process all logs
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
entry := allLogs.Get()
|
||||||
|
fmt.Printf("[ALL] %s [%s] %s: %s\n",
|
||||||
|
entry.Timestamp.Format(time.TimeOnly),
|
||||||
|
entry.Namespace, entry.Level, entry.Output)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Process only security-relevant warnings/errors
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
entry := securityLogs.Get()
|
||||||
|
if entry.Level == "ERROR" || entry.Level == "WARN" {
|
||||||
|
fmt.Printf("🚨 SECURITY ALERT [%s] %s: %s\n",
|
||||||
|
entry.Namespace, entry.Level, entry.Output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Generate some logs
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
apiLog.Info("API request processed")
|
||||||
|
dbLog.Info("Query executed successfully")
|
||||||
|
dbLog.Warn("Connection pool running low")
|
||||||
|
authLog.Error("Brute force attempt detected")
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up clients when done
|
||||||
|
allLogs.Destroy()
|
||||||
|
securityLogs.Destroy()
|
||||||
|
}
|
||||||
48
examples/log-levels/main.go
Normal file
48
examples/log-levels/main.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
// Example: log level filtering and all available levels.
|
||||||
|
//
|
||||||
|
// log-socket supports 8 log levels from TRACE (most verbose)
|
||||||
|
// to FATAL (least verbose). Setting a log level filters out
|
||||||
|
// everything below it.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
logger "github.com/taigrr/log-socket/v2/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer logger.Flush()
|
||||||
|
|
||||||
|
fmt.Println("=== All log levels (TRACE and above) ===")
|
||||||
|
logger.SetLogLevel(logger.LTrace)
|
||||||
|
|
||||||
|
logger.Trace("Detailed execution trace — variable x = 42")
|
||||||
|
logger.Debug("Processing request for user_id=123")
|
||||||
|
logger.Info("Server started on :8080")
|
||||||
|
logger.Notice("Configuration reloaded")
|
||||||
|
logger.Warn("Disk usage at 85%")
|
||||||
|
logger.Error("Failed to send email: SMTP timeout")
|
||||||
|
// logger.Panic("...") — would panic
|
||||||
|
// logger.Fatal("...") — would os.Exit(1)
|
||||||
|
|
||||||
|
fmt.Println("\n=== Formatted variants ===")
|
||||||
|
logger.Infof("Request took %dms", 42)
|
||||||
|
logger.Warnf("Retrying in %d seconds (attempt %d/%d)", 5, 2, 3)
|
||||||
|
logger.Errorf("HTTP %d: %s", 503, "Service Unavailable")
|
||||||
|
|
||||||
|
fmt.Println("\n=== Only WARN and above ===")
|
||||||
|
logger.SetLogLevel(logger.LWarn)
|
||||||
|
|
||||||
|
logger.Debug("This will NOT appear")
|
||||||
|
logger.Info("This will NOT appear either")
|
||||||
|
logger.Warn("This WILL appear")
|
||||||
|
logger.Error("This WILL appear too")
|
||||||
|
|
||||||
|
fmt.Println("\n=== Per-logger levels via namespaced loggers ===")
|
||||||
|
logger.SetLogLevel(logger.LTrace) // Reset global
|
||||||
|
|
||||||
|
appLog := logger.NewLogger("app")
|
||||||
|
appLog.Info("Namespaced loggers inherit the global output but tag entries")
|
||||||
|
appLog.Warnf("Something needs attention in the %s subsystem", "app")
|
||||||
|
}
|
||||||
73
examples/namespaces/main.go
Normal file
73
examples/namespaces/main.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
// Example: namespace-based logging for organizing logs by component.
|
||||||
|
//
|
||||||
|
// Namespaces let you tag log entries by subsystem (api, database, auth, etc.)
|
||||||
|
// and filter them in the web UI or via programmatic clients.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/taigrr/log-socket/v2/browser"
|
||||||
|
logger "github.com/taigrr/log-socket/v2/log"
|
||||||
|
"github.com/taigrr/log-socket/v2/ws"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer logger.Flush()
|
||||||
|
|
||||||
|
// Create loggers for different subsystems
|
||||||
|
apiLog := logger.NewLogger("api")
|
||||||
|
dbLog := logger.NewLogger("database")
|
||||||
|
authLog := logger.NewLogger("auth")
|
||||||
|
cacheLog := logger.NewLogger("cache")
|
||||||
|
|
||||||
|
// Simulate application activity
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
apiLog.Infof("GET /api/users — 200 OK (%dms)", rand.Intn(200))
|
||||||
|
apiLog.Debugf("Request headers: Accept=application/json")
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
dbLog.Infof("SELECT * FROM users — %d rows", rand.Intn(100))
|
||||||
|
if rand.Float64() < 0.3 {
|
||||||
|
dbLog.Warn("Slow query detected (>500ms)")
|
||||||
|
}
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
if rand.Float64() < 0.7 {
|
||||||
|
authLog.Info("User login successful")
|
||||||
|
} else {
|
||||||
|
authLog.Error("Failed login attempt from 192.168.1.42")
|
||||||
|
}
|
||||||
|
time.Sleep(3 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
cacheLog.Tracef("Cache hit ratio: %.1f%%", rand.Float64()*100)
|
||||||
|
if rand.Float64() < 0.1 {
|
||||||
|
cacheLog.Warn("Cache eviction triggered")
|
||||||
|
}
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// The /api/namespaces endpoint lists all active namespaces
|
||||||
|
http.HandleFunc("/ws", ws.LogSocketHandler)
|
||||||
|
http.HandleFunc("/api/namespaces", ws.NamespacesHandler)
|
||||||
|
http.HandleFunc("/", browser.LogSocketViewHandler)
|
||||||
|
fmt.Println("Log viewer with namespace filtering at http://localhost:8080")
|
||||||
|
logger.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
|
||||||
|
}
|
||||||
4
go.mod
4
go.mod
@@ -1,5 +1,5 @@
|
|||||||
module github.com/taigrr/log-socket
|
module github.com/taigrr/log-socket/v2
|
||||||
|
|
||||||
go 1.24.4
|
go 1.26.1
|
||||||
|
|
||||||
require github.com/gorilla/websocket v1.5.3
|
require github.com/gorilla/websocket v1.5.3
|
||||||
|
|||||||
171
log/bench_test.go
Normal file
171
log/bench_test.go
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const benchNS = "bench-isolated-ns"
|
||||||
|
|
||||||
|
// benchClient creates a client with a continuous drain goroutine.
|
||||||
|
// Returns the client and a stop function to call after b.StopTimer().
|
||||||
|
func benchClient(ns string) (*Client, func()) {
|
||||||
|
c := CreateClient(ns)
|
||||||
|
c.SetLogLevel(LTrace)
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
case <-c.writer:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return c, func() {
|
||||||
|
close(done)
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkCreateClient measures client creation overhead.
|
||||||
|
func BenchmarkCreateClient(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
c := CreateClient("bench")
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkTrace benchmarks Logger.Trace.
|
||||||
|
func BenchmarkTrace(b *testing.B) {
|
||||||
|
l := NewLogger(benchNS)
|
||||||
|
_, stop := benchClient(benchNS)
|
||||||
|
defer stop()
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
l.Trace("benchmark trace message")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkTracef benchmarks formatted Logger.Tracef.
|
||||||
|
func BenchmarkTracef(b *testing.B) {
|
||||||
|
l := NewLogger(benchNS)
|
||||||
|
_, stop := benchClient(benchNS)
|
||||||
|
defer stop()
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
l.Tracef("benchmark trace message %d", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkDebug benchmarks Logger.Debug.
|
||||||
|
func BenchmarkDebug(b *testing.B) {
|
||||||
|
l := NewLogger(benchNS)
|
||||||
|
_, stop := benchClient(benchNS)
|
||||||
|
defer stop()
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
l.Debug("benchmark debug message")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkInfo benchmarks Logger.Info.
|
||||||
|
func BenchmarkInfo(b *testing.B) {
|
||||||
|
l := NewLogger(benchNS)
|
||||||
|
_, stop := benchClient(benchNS)
|
||||||
|
defer stop()
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
l.Info("benchmark info message")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkInfof benchmarks Logger.Infof with formatting.
|
||||||
|
func BenchmarkInfof(b *testing.B) {
|
||||||
|
l := NewLogger(benchNS)
|
||||||
|
_, stop := benchClient(benchNS)
|
||||||
|
defer stop()
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
l.Infof("user %s performed action %d", "testuser", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkWarn benchmarks Logger.Warn.
|
||||||
|
func BenchmarkWarn(b *testing.B) {
|
||||||
|
l := NewLogger(benchNS)
|
||||||
|
_, stop := benchClient(benchNS)
|
||||||
|
defer stop()
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
l.Warn("benchmark warn message")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkError benchmarks Logger.Error.
|
||||||
|
func BenchmarkError(b *testing.B) {
|
||||||
|
l := NewLogger(benchNS)
|
||||||
|
_, stop := benchClient(benchNS)
|
||||||
|
defer stop()
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
l.Error("benchmark error message")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkMultipleClients measures fan-out to multiple consumers.
|
||||||
|
func BenchmarkMultipleClients(b *testing.B) {
|
||||||
|
const numClients = 5
|
||||||
|
l := NewLogger(benchNS)
|
||||||
|
stops := make([]func(), numClients)
|
||||||
|
for i := range stops {
|
||||||
|
_, stops[i] = benchClient(benchNS)
|
||||||
|
}
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
l.Info("benchmark multi-client")
|
||||||
|
}
|
||||||
|
b.StopTimer()
|
||||||
|
for _, stop := range stops {
|
||||||
|
stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkParallelInfo benchmarks concurrent Info calls from multiple goroutines.
|
||||||
|
func BenchmarkParallelInfo(b *testing.B) {
|
||||||
|
l := NewLogger(benchNS)
|
||||||
|
_, stop := benchClient(benchNS)
|
||||||
|
defer stop()
|
||||||
|
b.ResetTimer()
|
||||||
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
|
for pb.Next() {
|
||||||
|
l.Info("parallel benchmark info")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkNamespaceFiltering benchmarks logging when the consumer
|
||||||
|
// filters by a different namespace (messages are not delivered).
|
||||||
|
func BenchmarkNamespaceFiltering(b *testing.B) {
|
||||||
|
l := NewLogger(benchNS)
|
||||||
|
_, stop := benchClient("completely-different-ns")
|
||||||
|
defer stop()
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
l.Info("filtered out message")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkFileInfo measures the cost of runtime.Caller for file info.
|
||||||
|
func BenchmarkFileInfo(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
fileInfo(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkEntryCreation measures raw fmt.Sprint overhead (baseline).
|
||||||
|
func BenchmarkEntryCreation(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = fmt.Sprint("benchmark message ", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
99
log/color.go
Normal file
99
log/color.go
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ANSI color codes for terminal output
|
||||||
|
const (
|
||||||
|
colorReset = "\033[0m"
|
||||||
|
colorRed = "\033[31m"
|
||||||
|
colorGreen = "\033[32m"
|
||||||
|
colorYellow = "\033[33m"
|
||||||
|
colorBlue = "\033[34m"
|
||||||
|
colorPurple = "\033[35m"
|
||||||
|
colorCyan = "\033[36m"
|
||||||
|
colorWhite = "\033[37m"
|
||||||
|
colorGray = "\033[90m"
|
||||||
|
|
||||||
|
// Bold variants
|
||||||
|
colorBoldRed = "\033[1;31m"
|
||||||
|
colorBoldYellow = "\033[1;33m"
|
||||||
|
colorBoldWhite = "\033[1;37m"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
colorEnabled = true
|
||||||
|
colorEnabledOnce sync.Once
|
||||||
|
colorMux sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
|
// SetColorEnabled enables or disables colored output for stderr logging.
|
||||||
|
// By default, color is enabled when stderr is a terminal.
|
||||||
|
func SetColorEnabled(enabled bool) {
|
||||||
|
colorMux.Lock()
|
||||||
|
colorEnabled = enabled
|
||||||
|
colorMux.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ColorEnabled returns whether colored output is currently enabled.
|
||||||
|
func ColorEnabled() bool {
|
||||||
|
colorMux.RLock()
|
||||||
|
defer colorMux.RUnlock()
|
||||||
|
return colorEnabled
|
||||||
|
}
|
||||||
|
|
||||||
|
// isTerminal checks if the given file descriptor is a terminal.
|
||||||
|
// This is a simple heuristic that works on Unix-like systems.
|
||||||
|
func isTerminal(f *os.File) bool {
|
||||||
|
stat, err := f.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return (stat.Mode() & os.ModeCharDevice) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// initColorEnabled sets the default color state based on whether stderr is a terminal.
|
||||||
|
func initColorEnabled() {
|
||||||
|
colorEnabledOnce.Do(func() {
|
||||||
|
colorEnabled = isTerminal(os.Stderr)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// levelColor returns the ANSI color code for a given log level.
|
||||||
|
func levelColor(level Level) string {
|
||||||
|
switch level {
|
||||||
|
case LTrace:
|
||||||
|
return colorGray
|
||||||
|
case LDebug:
|
||||||
|
return colorCyan
|
||||||
|
case LInfo:
|
||||||
|
return colorGreen
|
||||||
|
case LNotice:
|
||||||
|
return colorBlue
|
||||||
|
case LWarn:
|
||||||
|
return colorYellow
|
||||||
|
case LError:
|
||||||
|
return colorRed
|
||||||
|
case LPanic:
|
||||||
|
return colorBoldRed
|
||||||
|
case LFatal:
|
||||||
|
return colorBoldRed
|
||||||
|
default:
|
||||||
|
return colorReset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// colorize wraps text with ANSI color codes if color is enabled.
|
||||||
|
func colorize(text string, color string) string {
|
||||||
|
if !ColorEnabled() {
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
return color + text + colorReset
|
||||||
|
}
|
||||||
|
|
||||||
|
// colorizeLevelText returns the level string with appropriate color.
|
||||||
|
func colorizeLevelText(level string, lvl Level) string {
|
||||||
|
return colorize(level, levelColor(lvl))
|
||||||
|
}
|
||||||
121
log/log.go
121
log/log.go
@@ -16,9 +16,13 @@ var (
|
|||||||
stderrClient *Client
|
stderrClient *Client
|
||||||
cleanup sync.Once
|
cleanup sync.Once
|
||||||
stderrFinished chan bool
|
stderrFinished chan bool
|
||||||
|
namespaces map[string]bool
|
||||||
|
namespacesMux sync.RWMutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
namespaces = make(map[string]bool)
|
||||||
|
initColorEnabled()
|
||||||
stderrClient = CreateClient()
|
stderrClient = CreateClient()
|
||||||
stderrClient.SetLogLevel(LTrace)
|
stderrClient.SetLogLevel(LTrace)
|
||||||
stderrFinished = make(chan bool, 1)
|
stderrFinished = make(chan bool, 1)
|
||||||
@@ -27,16 +31,33 @@ func init() {
|
|||||||
|
|
||||||
func (c *Client) logStdErr() {
|
func (c *Client) logStdErr() {
|
||||||
for e := range c.writer {
|
for e := range c.writer {
|
||||||
if e.level >= c.LogLevel {
|
if e.level >= c.LogLevel && c.matchesNamespace(e.Namespace) {
|
||||||
fmt.Fprintf(os.Stderr, "%s\t%s\t%s\t%s\n", e.Timestamp.String(), e.Level, e.Output, e.File)
|
levelStr := colorizeLevelText(e.Level, e.level)
|
||||||
|
nsStr := colorize("["+e.Namespace+"]", colorPurple)
|
||||||
|
fileStr := colorize(e.File, colorGray)
|
||||||
|
fmt.Fprintf(os.Stderr, "%s\t%s\t%s\t%s\t%s\n", e.Timestamp.String(), levelStr, nsStr, e.Output, fileStr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stderrFinished <- true
|
stderrFinished <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateClient() *Client {
|
func (c *Client) matchesNamespace(namespace string) bool {
|
||||||
|
// Empty Namespaces slice means match all
|
||||||
|
if len(c.Namespaces) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
for _, ns := range c.Namespaces {
|
||||||
|
if ns == namespace {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateClient(namespaces ...string) *Client {
|
||||||
var client Client
|
var client Client
|
||||||
client.initialized = true
|
client.initialized = true
|
||||||
|
client.Namespaces = namespaces
|
||||||
client.writer = make(LogWriter, 1000)
|
client.writer = make(LogWriter, 1000)
|
||||||
sliceTex.Lock()
|
sliceTex.Lock()
|
||||||
clients = append(clients, &client)
|
clients = append(clients, &client)
|
||||||
@@ -53,19 +74,19 @@ func Flush() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Destroy() error {
|
func (c *Client) Destroy() error {
|
||||||
var otherClients []*Client
|
|
||||||
if !c.initialized {
|
if !c.initialized {
|
||||||
panic(errors.New("cannot delete uninitialized client, did you use CreateClient?"))
|
panic(errors.New("cannot delete uninitialized client, did you use CreateClient?"))
|
||||||
}
|
}
|
||||||
sliceTex.Lock()
|
sliceTex.Lock()
|
||||||
c.writer = nil
|
|
||||||
c.initialized = false
|
c.initialized = false
|
||||||
|
var otherClients []*Client
|
||||||
for _, x := range clients {
|
for _, x := range clients {
|
||||||
if x.initialized {
|
if x != c && x.initialized {
|
||||||
otherClients = append(otherClients, x)
|
otherClients = append(otherClients, x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
clients = otherClients
|
clients = otherClients
|
||||||
|
c.writer = nil
|
||||||
sliceTex.Unlock()
|
sliceTex.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -78,9 +99,21 @@ func (c *Client) GetLogLevel() Level {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createLog(e Entry) {
|
func createLog(e Entry) {
|
||||||
|
// Track namespace
|
||||||
|
namespacesMux.Lock()
|
||||||
|
namespaces[e.Namespace] = true
|
||||||
|
namespacesMux.Unlock()
|
||||||
|
|
||||||
sliceTex.Lock()
|
sliceTex.Lock()
|
||||||
for _, c := range clients {
|
for _, c := range clients {
|
||||||
func(c *Client, e Entry) {
|
func(c *Client, e Entry) {
|
||||||
|
if c.writer == nil || !c.initialized {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Filter by namespace if client has filters specified
|
||||||
|
if !c.matchesNamespace(e.Namespace) {
|
||||||
|
return
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case c.writer <- e:
|
case c.writer <- e:
|
||||||
// try to clear out one of the older entries
|
// try to clear out one of the older entries
|
||||||
@@ -96,6 +129,18 @@ func createLog(e Entry) {
|
|||||||
sliceTex.Unlock()
|
sliceTex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNamespaces returns a list of all namespaces that have been used
|
||||||
|
func GetNamespaces() []string {
|
||||||
|
namespacesMux.RLock()
|
||||||
|
defer namespacesMux.RUnlock()
|
||||||
|
|
||||||
|
result := make([]string, 0, len(namespaces))
|
||||||
|
for ns := range namespaces {
|
||||||
|
result = append(result, ns)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
func SetLogLevel(level Level) {
|
func SetLogLevel(level Level) {
|
||||||
stderrClient.LogLevel = level
|
stderrClient.LogLevel = level
|
||||||
}
|
}
|
||||||
@@ -123,6 +168,7 @@ func Trace(args ...any) {
|
|||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "TRACE",
|
Level: "TRACE",
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
level: LTrace,
|
level: LTrace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
@@ -137,6 +183,7 @@ func Tracef(format string, args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "TRACE",
|
Level: "TRACE",
|
||||||
level: LTrace,
|
level: LTrace,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -150,6 +197,7 @@ func Traceln(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "TRACE",
|
Level: "TRACE",
|
||||||
level: LTrace,
|
level: LTrace,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -163,6 +211,7 @@ func Debug(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "DEBUG",
|
Level: "DEBUG",
|
||||||
level: LDebug,
|
level: LDebug,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -176,6 +225,7 @@ func Debugf(format string, args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "DEBUG",
|
Level: "DEBUG",
|
||||||
level: LDebug,
|
level: LDebug,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -189,6 +239,7 @@ func Debugln(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "DEBUG",
|
Level: "DEBUG",
|
||||||
level: LDebug,
|
level: LDebug,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -202,6 +253,7 @@ func Info(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "INFO",
|
Level: "INFO",
|
||||||
level: LInfo,
|
level: LInfo,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -215,6 +267,7 @@ func Infof(format string, args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "INFO",
|
Level: "INFO",
|
||||||
level: LInfo,
|
level: LInfo,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -228,6 +281,7 @@ func Infoln(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "INFO",
|
Level: "INFO",
|
||||||
level: LInfo,
|
level: LInfo,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -241,6 +295,7 @@ func Notice(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "NOTICE",
|
Level: "NOTICE",
|
||||||
level: LNotice,
|
level: LNotice,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -254,6 +309,7 @@ func Noticef(format string, args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "NOTICE",
|
Level: "NOTICE",
|
||||||
level: LNotice,
|
level: LNotice,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -267,6 +323,7 @@ func Noticeln(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "NOTICE",
|
Level: "NOTICE",
|
||||||
level: LNotice,
|
level: LNotice,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -280,6 +337,7 @@ func Warn(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "WARN",
|
Level: "WARN",
|
||||||
level: LWarn,
|
level: LWarn,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -293,6 +351,7 @@ func Warnf(format string, args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "WARN",
|
Level: "WARN",
|
||||||
level: LWarn,
|
level: LWarn,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -306,6 +365,7 @@ func Warnln(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "WARN",
|
Level: "WARN",
|
||||||
level: LWarn,
|
level: LWarn,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -319,6 +379,7 @@ func Error(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "ERROR",
|
Level: "ERROR",
|
||||||
level: LError,
|
level: LError,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -332,6 +393,7 @@ func Errorf(format string, args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "ERROR",
|
Level: "ERROR",
|
||||||
level: LError,
|
level: LError,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -345,6 +407,7 @@ func Errorln(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "ERROR",
|
Level: "ERROR",
|
||||||
level: LError,
|
level: LError,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -358,9 +421,10 @@ func Panic(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "PANIC",
|
Level: "PANIC",
|
||||||
level: LPanic,
|
level: LPanic,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
if len(args) >= 0 {
|
if len(args) > 0 {
|
||||||
switch args[0].(type) {
|
switch args[0].(type) {
|
||||||
case error:
|
case error:
|
||||||
panic(args[0])
|
panic(args[0])
|
||||||
@@ -381,9 +445,10 @@ func Panicf(format string, args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "PANIC",
|
Level: "PANIC",
|
||||||
level: LPanic,
|
level: LPanic,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
if len(args) >= 0 {
|
if len(args) > 0 {
|
||||||
switch args[0].(type) {
|
switch args[0].(type) {
|
||||||
case error:
|
case error:
|
||||||
panic(args[0])
|
panic(args[0])
|
||||||
@@ -403,9 +468,10 @@ func Panicln(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "PANIC",
|
Level: "PANIC",
|
||||||
level: LPanic,
|
level: LPanic,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
if len(args) >= 0 {
|
if len(args) > 0 {
|
||||||
switch args[0].(type) {
|
switch args[0].(type) {
|
||||||
case error:
|
case error:
|
||||||
panic(args[0])
|
panic(args[0])
|
||||||
@@ -426,6 +492,7 @@ func Fatal(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "FATAL",
|
Level: "FATAL",
|
||||||
level: LFatal,
|
level: LFatal,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
Flush()
|
Flush()
|
||||||
@@ -441,6 +508,7 @@ func Fatalf(format string, args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "FATAL",
|
Level: "FATAL",
|
||||||
level: LFatal,
|
level: LFatal,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
Flush()
|
Flush()
|
||||||
@@ -455,6 +523,7 @@ func Fatalln(args ...any) {
|
|||||||
File: fileInfo(2),
|
File: fileInfo(2),
|
||||||
Level: "FATAL",
|
Level: "FATAL",
|
||||||
level: LFatal,
|
level: LFatal,
|
||||||
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
Flush()
|
Flush()
|
||||||
@@ -487,3 +556,37 @@ func fileInfo(skip int) string {
|
|||||||
}
|
}
|
||||||
return fmt.Sprintf("%s:%d", file, line)
|
return fmt.Sprintf("%s:%d", file, line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Broadcast sends an [Entry] to all registered clients. This is the public
|
||||||
|
// entry point used by adapter packages (such as the slog handler) that
|
||||||
|
// construct entries themselves. The unexported level field is inferred from
|
||||||
|
// [Entry.Level] when not already set.
|
||||||
|
func Broadcast(e Entry) {
|
||||||
|
if e.level == 0 && e.Level != "" && e.Level != "TRACE" {
|
||||||
|
e.level = parseLevelString(e.Level)
|
||||||
|
}
|
||||||
|
createLog(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseLevelString(s string) Level {
|
||||||
|
switch s {
|
||||||
|
case "TRACE":
|
||||||
|
return LTrace
|
||||||
|
case "DEBUG":
|
||||||
|
return LDebug
|
||||||
|
case "INFO":
|
||||||
|
return LInfo
|
||||||
|
case "NOTICE":
|
||||||
|
return LNotice
|
||||||
|
case "WARN":
|
||||||
|
return LWarn
|
||||||
|
case "ERROR":
|
||||||
|
return LError
|
||||||
|
case "PANIC":
|
||||||
|
return LPanic
|
||||||
|
case "FATAL":
|
||||||
|
return LFatal
|
||||||
|
default:
|
||||||
|
return LInfo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
516
log/log_test.go
516
log/log_test.go
@@ -4,8 +4,21 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// getEntry reads from a client with a timeout to avoid hanging tests.
|
||||||
|
func getEntry(c *Client, timeout time.Duration) (Entry, bool) {
|
||||||
|
ch := make(chan Entry, 1)
|
||||||
|
go func() { ch <- c.Get() }()
|
||||||
|
select {
|
||||||
|
case e := <-ch:
|
||||||
|
return e, true
|
||||||
|
case <-time.After(timeout):
|
||||||
|
return Entry{}, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Test CreateClient() and Client.Destroy()
|
// Test CreateClient() and Client.Destroy()
|
||||||
func TestCreateDestroy(t *testing.T) {
|
func TestCreateDestroy(t *testing.T) {
|
||||||
// Ensure only stderr exists at the beginning
|
// Ensure only stderr exists at the beginning
|
||||||
@@ -13,7 +26,7 @@ func TestCreateDestroy(t *testing.T) {
|
|||||||
t.Errorf("Expected 1 client, but found %d", len(clients))
|
t.Errorf("Expected 1 client, but found %d", len(clients))
|
||||||
}
|
}
|
||||||
// Create a new client, ensure it's added
|
// Create a new client, ensure it's added
|
||||||
c := CreateClient()
|
c := CreateClient("test")
|
||||||
if len(clients) != 2 {
|
if len(clients) != 2 {
|
||||||
t.Errorf("Expected 2 clients, but found %d", len(clients))
|
t.Errorf("Expected 2 clients, but found %d", len(clients))
|
||||||
}
|
}
|
||||||
@@ -27,7 +40,7 @@ func TestCreateDestroy(t *testing.T) {
|
|||||||
// SetLogLevel set log level of logger
|
// SetLogLevel set log level of logger
|
||||||
func TestSetLogLevel(t *testing.T) {
|
func TestSetLogLevel(t *testing.T) {
|
||||||
logLevels := [...]Level{LTrace, LDebug, LInfo, LWarn, LError, LPanic, LFatal}
|
logLevels := [...]Level{LTrace, LDebug, LInfo, LWarn, LError, LPanic, LFatal}
|
||||||
c := CreateClient()
|
c := CreateClient("test")
|
||||||
for _, x := range logLevels {
|
for _, x := range logLevels {
|
||||||
c.SetLogLevel(x)
|
c.SetLogLevel(x)
|
||||||
if c.GetLogLevel() != x {
|
if c.GetLogLevel() != x {
|
||||||
@@ -38,7 +51,7 @@ func TestSetLogLevel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkDebugSerial(b *testing.B) {
|
func BenchmarkDebugSerial(b *testing.B) {
|
||||||
c := CreateClient()
|
c := CreateClient("test")
|
||||||
var x sync.WaitGroup
|
var x sync.WaitGroup
|
||||||
x.Add(b.N)
|
x.Add(b.N)
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
@@ -55,7 +68,7 @@ func BenchmarkDebugSerial(b *testing.B) {
|
|||||||
// Trace ensure logs come out in the right order
|
// Trace ensure logs come out in the right order
|
||||||
func TestOrder(t *testing.T) {
|
func TestOrder(t *testing.T) {
|
||||||
testString := "Testing trace: "
|
testString := "Testing trace: "
|
||||||
c := CreateClient()
|
c := CreateClient(DefaultNamespace)
|
||||||
c.SetLogLevel(LTrace)
|
c.SetLogLevel(LTrace)
|
||||||
|
|
||||||
for i := 0; i < 5000; i++ {
|
for i := 0; i < 5000; i++ {
|
||||||
@@ -64,70 +77,477 @@ func TestOrder(t *testing.T) {
|
|||||||
t.Error("Trace input doesn't match output")
|
t.Error("Trace input doesn't match output")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug prints out logs on debug level
|
|
||||||
func TestDebug(t *testing.T) {
|
func TestDebug(t *testing.T) {
|
||||||
Debug("Test of Debug")
|
c := CreateClient(DefaultNamespace)
|
||||||
// if logLevel >= LDebug {
|
c.SetLogLevel(LDebug)
|
||||||
// entry := logger.WithFields(logrus.Fields{})
|
|
||||||
// entry.Data["file"] = fileInfo(2)
|
Debug("debug message")
|
||||||
// entry.Debug(args...)
|
e, ok := getEntry(c, time.Second)
|
||||||
// }
|
if !ok {
|
||||||
|
t.Fatal("timed out waiting for debug entry")
|
||||||
|
}
|
||||||
|
if e.Level != "DEBUG" {
|
||||||
|
t.Errorf("level = %q, want DEBUG", e.Level)
|
||||||
|
}
|
||||||
|
if e.Output != "debug message" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "debug message")
|
||||||
|
}
|
||||||
|
if e.Namespace != DefaultNamespace {
|
||||||
|
t.Errorf("namespace = %q, want %q", e.Namespace, DefaultNamespace)
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDebugf(t *testing.T) {
|
||||||
|
c := CreateClient(DefaultNamespace)
|
||||||
|
c.SetLogLevel(LDebug)
|
||||||
|
|
||||||
|
Debugf("hello %s %d", "world", 42)
|
||||||
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
if e.Output != "hello world 42" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "hello world 42")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info prints out logs on info level
|
|
||||||
func TestInfo(t *testing.T) {
|
func TestInfo(t *testing.T) {
|
||||||
// if logLevel >= LInfo {
|
c := CreateClient(DefaultNamespace)
|
||||||
// entry := logger.WithFields(logrus.Fields{})
|
c.SetLogLevel(LInfo)
|
||||||
// entry.Data["file"] = fileInfo(2)
|
|
||||||
// entry.Info(args...)
|
Info("info message")
|
||||||
// }
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out waiting for info entry")
|
||||||
|
}
|
||||||
|
if e.Level != "INFO" {
|
||||||
|
t.Errorf("level = %q, want INFO", e.Level)
|
||||||
|
}
|
||||||
|
if e.Output != "info message" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "info message")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInfof(t *testing.T) {
|
||||||
|
c := CreateClient(DefaultNamespace)
|
||||||
|
c.SetLogLevel(LInfo)
|
||||||
|
|
||||||
|
Infof("count: %d", 99)
|
||||||
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
if e.Output != "count: 99" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "count: 99")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print prints out logs on info level
|
|
||||||
func TestPrint(t *testing.T) {
|
func TestPrint(t *testing.T) {
|
||||||
// if logLevel >= LInfo {
|
c := CreateClient(DefaultNamespace)
|
||||||
// entry := logger.WithFields(logrus.Fields{})
|
c.SetLogLevel(LInfo)
|
||||||
// entry.Data["file"] = fileInfo(2)
|
|
||||||
// entry.Info(args...)
|
Print("print message")
|
||||||
// }
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
// Print is an alias for Info
|
||||||
|
if e.Level != "INFO" {
|
||||||
|
t.Errorf("level = %q, want INFO", e.Level)
|
||||||
|
}
|
||||||
|
if e.Output != "print message" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "print message")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintf(t *testing.T) {
|
||||||
|
c := CreateClient(DefaultNamespace)
|
||||||
|
c.SetLogLevel(LInfo)
|
||||||
|
|
||||||
|
Printf("formatted %s", "print")
|
||||||
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
if e.Output != "formatted print" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "formatted print")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNotice(t *testing.T) {
|
||||||
|
c := CreateClient(DefaultNamespace)
|
||||||
|
c.SetLogLevel(LNotice)
|
||||||
|
|
||||||
|
Notice("notice message")
|
||||||
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
if e.Level != "NOTICE" {
|
||||||
|
t.Errorf("level = %q, want NOTICE", e.Level)
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn prints out logs on warn level
|
|
||||||
func TestWarn(t *testing.T) {
|
func TestWarn(t *testing.T) {
|
||||||
// if logLevel >= LWarn {
|
c := CreateClient(DefaultNamespace)
|
||||||
// entry := logger.WithFields(logrus.Fields{})
|
c.SetLogLevel(LWarn)
|
||||||
// entry.Data["file"] = fileInfo(2)
|
|
||||||
// entry.Warn(args...)
|
Warn("warning message")
|
||||||
// }
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out waiting for warn entry")
|
||||||
|
}
|
||||||
|
if e.Level != "WARN" {
|
||||||
|
t.Errorf("level = %q, want WARN", e.Level)
|
||||||
|
}
|
||||||
|
if e.Output != "warning message" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "warning message")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWarnf(t *testing.T) {
|
||||||
|
c := CreateClient(DefaultNamespace)
|
||||||
|
c.SetLogLevel(LWarn)
|
||||||
|
|
||||||
|
Warnf("warn %d", 1)
|
||||||
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
if e.Output != "warn 1" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "warn 1")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error prints out logs on error level
|
|
||||||
func TestError(t *testing.T) {
|
func TestError(t *testing.T) {
|
||||||
// if logLevel >= LError {
|
c := CreateClient(DefaultNamespace)
|
||||||
// entry := logger.WithFields(logrus.Fields{})
|
c.SetLogLevel(LError)
|
||||||
// entry.Data["file"] = fileInfo(2)
|
|
||||||
// entry.Error(args...)
|
Error("error message")
|
||||||
// }
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out waiting for error entry")
|
||||||
|
}
|
||||||
|
if e.Level != "ERROR" {
|
||||||
|
t.Errorf("level = %q, want ERROR", e.Level)
|
||||||
|
}
|
||||||
|
if e.Output != "error message" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "error message")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal prints out logs on fatal level
|
func TestErrorf(t *testing.T) {
|
||||||
func TestFatal(t *testing.T) {
|
c := CreateClient(DefaultNamespace)
|
||||||
// if logLevel >= LFatal {
|
c.SetLogLevel(LError)
|
||||||
// entry := logger.WithFields(logrus.Fields{})
|
|
||||||
// entry.Data["file"] = fileInfo(2)
|
Errorf("err: %s", "something broke")
|
||||||
// entry.Fatal(args...)
|
e, ok := getEntry(c, time.Second)
|
||||||
// }
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
if e.Output != "err: something broke" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "err: something broke")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panic prints out logs on panic level
|
|
||||||
func TestPanic(t *testing.T) {
|
func TestPanic(t *testing.T) {
|
||||||
// if logLevel >= LPanic {
|
c := CreateClient(DefaultNamespace)
|
||||||
// entry := logger.WithFields(logrus.Fields{})
|
c.SetLogLevel(LPanic)
|
||||||
// entry.Data["file"] = fileInfo(2)
|
|
||||||
// entry.Panic(args...)
|
defer func() {
|
||||||
// }
|
r := recover()
|
||||||
|
if r == nil {
|
||||||
|
t.Error("expected panic, got nil")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}()
|
||||||
|
|
||||||
|
Panic("panic message")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPanicf(t *testing.T) {
|
||||||
|
c := CreateClient(DefaultNamespace)
|
||||||
|
c.SetLogLevel(LPanic)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
r := recover()
|
||||||
|
if r == nil {
|
||||||
|
t.Error("expected panic, got nil")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}()
|
||||||
|
|
||||||
|
Panicf("panic %d", 42)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPanicln(t *testing.T) {
|
||||||
|
c := CreateClient(DefaultNamespace)
|
||||||
|
c.SetLogLevel(LPanic)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
r := recover()
|
||||||
|
if r == nil {
|
||||||
|
t.Error("expected panic, got nil")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}()
|
||||||
|
|
||||||
|
Panicln("panic line")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestLogLevelFiltering verifies that the client's log level is stored correctly.
|
||||||
|
// Note: level filtering only applies to stderr output, not to client channels.
|
||||||
|
// All entries matching the namespace are delivered to the client channel regardless of level.
|
||||||
|
func TestLogLevelFiltering(t *testing.T) {
|
||||||
|
c := CreateClient(DefaultNamespace)
|
||||||
|
c.SetLogLevel(LWarn)
|
||||||
|
|
||||||
|
if c.GetLogLevel() != LWarn {
|
||||||
|
t.Errorf("expected log level LWarn, got %d", c.GetLogLevel())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Both entries arrive at the client channel (level filtering is stderr-only)
|
||||||
|
Info("info message")
|
||||||
|
Warn("warn message")
|
||||||
|
|
||||||
|
e1, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out waiting for first entry")
|
||||||
|
}
|
||||||
|
if e1.Output != "info message" {
|
||||||
|
t.Errorf("expected 'info message', got %q", e1.Output)
|
||||||
|
}
|
||||||
|
|
||||||
|
e2, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out waiting for second entry")
|
||||||
|
}
|
||||||
|
if e2.Output != "warn message" {
|
||||||
|
t.Errorf("expected 'warn message', got %q", e2.Output)
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestNamespaceFiltering verifies clients only receive matching namespaces.
|
||||||
|
func TestNamespaceFiltering(t *testing.T) {
|
||||||
|
c := CreateClient("api")
|
||||||
|
c.SetLogLevel(LTrace)
|
||||||
|
|
||||||
|
apiLogger := NewLogger("api")
|
||||||
|
dbLogger := NewLogger("database")
|
||||||
|
|
||||||
|
// Log to database namespace — should not arrive at "api" client
|
||||||
|
dbLogger.Info("db message")
|
||||||
|
|
||||||
|
// Log to api namespace — should arrive
|
||||||
|
apiLogger.Info("api message")
|
||||||
|
|
||||||
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out waiting for api entry")
|
||||||
|
}
|
||||||
|
if e.Output != "api message" {
|
||||||
|
t.Errorf("expected 'api message', got %q", e.Output)
|
||||||
|
}
|
||||||
|
if e.Namespace != "api" {
|
||||||
|
t.Errorf("namespace = %q, want 'api'", e.Namespace)
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMultiNamespaceClient verifies a client subscribed to multiple namespaces.
|
||||||
|
func TestMultiNamespaceClient(t *testing.T) {
|
||||||
|
c := CreateClient("api", "auth")
|
||||||
|
c.SetLogLevel(LTrace)
|
||||||
|
|
||||||
|
apiLogger := NewLogger("api")
|
||||||
|
authLogger := NewLogger("auth")
|
||||||
|
dbLogger := NewLogger("database")
|
||||||
|
|
||||||
|
dbLogger.Info("db message") // filtered out
|
||||||
|
apiLogger.Info("api message") // should arrive
|
||||||
|
authLogger.Info("auth message") // should arrive
|
||||||
|
|
||||||
|
e1, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out waiting for first entry")
|
||||||
|
}
|
||||||
|
if e1.Output != "api message" {
|
||||||
|
t.Errorf("first entry = %q, want 'api message'", e1.Output)
|
||||||
|
}
|
||||||
|
|
||||||
|
e2, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out waiting for second entry")
|
||||||
|
}
|
||||||
|
if e2.Output != "auth message" {
|
||||||
|
t.Errorf("second entry = %q, want 'auth message'", e2.Output)
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestGetNamespaces verifies the namespace registry.
|
||||||
|
func TestGetNamespaces(t *testing.T) {
|
||||||
|
l := NewLogger("test-ns-registry")
|
||||||
|
l.Info("register this namespace")
|
||||||
|
|
||||||
|
nss := GetNamespaces()
|
||||||
|
found := false
|
||||||
|
for _, ns := range nss {
|
||||||
|
if ns == "test-ns-registry" {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Errorf("expected 'test-ns-registry' in GetNamespaces(), got %v", nss)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestLoggerDebugln verifies the Debugln method on Logger.
|
||||||
|
func TestLoggerDebugln(t *testing.T) {
|
||||||
|
c := CreateClient("debugln-test")
|
||||||
|
c.SetLogLevel(LDebug)
|
||||||
|
|
||||||
|
l := NewLogger("debugln-test")
|
||||||
|
l.Debugln("debugln message")
|
||||||
|
|
||||||
|
e, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
if e.Level != "DEBUG" {
|
||||||
|
t.Errorf("level = %q, want DEBUG", e.Level)
|
||||||
|
}
|
||||||
|
// Sprintln appends a newline
|
||||||
|
if e.Output != "debugln message\n" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "debugln message\n")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestNewLoggerEmptyNamespace verifies empty namespace defaults to DefaultNamespace.
|
||||||
|
func TestNewLoggerEmptyNamespace(t *testing.T) {
|
||||||
|
l := NewLogger("")
|
||||||
|
if l.Namespace != DefaultNamespace {
|
||||||
|
t.Errorf("namespace = %q, want %q", l.Namespace, DefaultNamespace)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestFileInfo verifies fileInfo returns a non-empty file:line string.
|
||||||
|
func TestFileInfo(t *testing.T) {
|
||||||
|
fi := fileInfo(1)
|
||||||
|
if fi == "" || fi == "<???>:1" {
|
||||||
|
t.Errorf("fileInfo returned unexpected value: %q", fi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestColorize verifies color wrapping.
|
||||||
|
func TestColorize(t *testing.T) {
|
||||||
|
SetColorEnabled(true)
|
||||||
|
result := colorize("hello", colorRed)
|
||||||
|
expected := colorRed + "hello" + colorReset
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("colorize with color enabled: got %q, want %q", result, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
SetColorEnabled(false)
|
||||||
|
result = colorize("hello", colorRed)
|
||||||
|
if result != "hello" {
|
||||||
|
t.Errorf("colorize with color disabled: got %q, want %q", result, "hello")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore default
|
||||||
|
SetColorEnabled(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestParseLevelString verifies level string parsing.
|
||||||
|
func TestParseLevelString(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
want Level
|
||||||
|
}{
|
||||||
|
{"TRACE", LTrace},
|
||||||
|
{"DEBUG", LDebug},
|
||||||
|
{"INFO", LInfo},
|
||||||
|
{"NOTICE", LNotice},
|
||||||
|
{"WARN", LWarn},
|
||||||
|
{"ERROR", LError},
|
||||||
|
{"PANIC", LPanic},
|
||||||
|
{"FATAL", LFatal},
|
||||||
|
{"UNKNOWN", LInfo}, // default
|
||||||
|
{"", LInfo}, // default
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
got := parseLevelString(tt.input)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("parseLevelString(%q) = %d, want %d", tt.input, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestBroadcast verifies the public Broadcast function.
|
||||||
|
func TestBroadcast(t *testing.T) {
|
||||||
|
c := CreateClient("broadcast-ns")
|
||||||
|
c.SetLogLevel(LTrace)
|
||||||
|
|
||||||
|
e := Entry{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Output: "broadcast test",
|
||||||
|
File: "test.go:1",
|
||||||
|
Level: "WARN",
|
||||||
|
Namespace: "broadcast-ns",
|
||||||
|
}
|
||||||
|
Broadcast(e)
|
||||||
|
|
||||||
|
got, ok := getEntry(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
if got.Output != "broadcast test" {
|
||||||
|
t.Errorf("output = %q, want %q", got.Output, "broadcast test")
|
||||||
|
}
|
||||||
|
if got.Level != "WARN" {
|
||||||
|
t.Errorf("level = %q, want WARN", got.Level)
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMatchesNamespace verifies the namespace matching helper.
|
||||||
|
func TestMatchesNamespace(t *testing.T) {
|
||||||
|
// Client with no namespace filter matches everything
|
||||||
|
c := CreateClient()
|
||||||
|
if !c.matchesNamespace("anything") {
|
||||||
|
t.Error("empty Namespaces should match all")
|
||||||
|
}
|
||||||
|
c.Destroy()
|
||||||
|
|
||||||
|
// Client with specific namespaces
|
||||||
|
c2 := CreateClient("api", "auth")
|
||||||
|
if !c2.matchesNamespace("api") {
|
||||||
|
t.Error("should match 'api'")
|
||||||
|
}
|
||||||
|
if !c2.matchesNamespace("auth") {
|
||||||
|
t.Error("should match 'auth'")
|
||||||
|
}
|
||||||
|
if c2.matchesNamespace("database") {
|
||||||
|
t.Error("should not match 'database'")
|
||||||
|
}
|
||||||
|
c2.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFlush(t *testing.T) {
|
func TestFlush(t *testing.T) {
|
||||||
|
|||||||
@@ -8,7 +8,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Default() *Logger {
|
func Default() *Logger {
|
||||||
return &Logger{FileInfoDepth: 0}
|
return &Logger{FileInfoDepth: 0, Namespace: DefaultNamespace}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLogger(namespace string) *Logger {
|
||||||
|
if namespace == "" {
|
||||||
|
namespace = DefaultNamespace
|
||||||
|
}
|
||||||
|
return &Logger{FileInfoDepth: 0, Namespace: namespace}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) SetInfoDepth(depth int) {
|
func (l *Logger) SetInfoDepth(depth int) {
|
||||||
@@ -21,9 +28,10 @@ func (l Logger) Trace(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "TRACE",
|
Level: "TRACE",
|
||||||
level: LTrace,
|
level: LTrace,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -34,9 +42,10 @@ func (l Logger) Tracef(format string, args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "TRACE",
|
Level: "TRACE",
|
||||||
level: LTrace,
|
level: LTrace,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -47,9 +56,10 @@ func (l Logger) Traceln(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "TRACE",
|
Level: "TRACE",
|
||||||
level: LTrace,
|
level: LTrace,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -60,9 +70,10 @@ func (l Logger) Debug(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "DEBUG",
|
Level: "DEBUG",
|
||||||
level: LDebug,
|
level: LDebug,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -73,9 +84,24 @@ func (l Logger) Debugf(format string, args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "DEBUG",
|
Level: "DEBUG",
|
||||||
level: LDebug,
|
level: LDebug,
|
||||||
|
Namespace: l.Namespace,
|
||||||
|
}
|
||||||
|
createLog(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debugln prints out logs on debug level with a newline
|
||||||
|
func (l Logger) Debugln(args ...any) {
|
||||||
|
output := fmt.Sprintln(args...)
|
||||||
|
e := Entry{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Output: output,
|
||||||
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
|
Level: "DEBUG",
|
||||||
|
level: LDebug,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -86,9 +112,10 @@ func (l Logger) Info(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "INFO",
|
Level: "INFO",
|
||||||
level: LInfo,
|
level: LInfo,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -99,9 +126,10 @@ func (l Logger) Infof(format string, args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "INFO",
|
Level: "INFO",
|
||||||
level: LInfo,
|
level: LInfo,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -112,9 +140,10 @@ func (l Logger) Infoln(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "INFO",
|
Level: "INFO",
|
||||||
level: LInfo,
|
level: LInfo,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -125,9 +154,10 @@ func (l Logger) Notice(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "NOTICE",
|
Level: "NOTICE",
|
||||||
level: LNotice,
|
level: LNotice,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -138,9 +168,10 @@ func (l Logger) Noticef(format string, args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "NOTICE",
|
Level: "NOTICE",
|
||||||
level: LNotice,
|
level: LNotice,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -151,9 +182,10 @@ func (l Logger) Noticeln(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "NOTICE",
|
Level: "NOTICE",
|
||||||
level: LNotice,
|
level: LNotice,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -164,9 +196,10 @@ func (l Logger) Warn(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "WARN",
|
Level: "WARN",
|
||||||
level: LWarn,
|
level: LWarn,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -177,9 +210,10 @@ func (l Logger) Warnf(format string, args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "WARN",
|
Level: "WARN",
|
||||||
level: LWarn,
|
level: LWarn,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -190,9 +224,10 @@ func (l Logger) Warnln(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "WARN",
|
Level: "WARN",
|
||||||
level: LWarn,
|
level: LWarn,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -203,9 +238,10 @@ func (l Logger) Error(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "ERROR",
|
Level: "ERROR",
|
||||||
level: LError,
|
level: LError,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -216,9 +252,10 @@ func (l Logger) Errorf(format string, args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "ERROR",
|
Level: "ERROR",
|
||||||
level: LError,
|
level: LError,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -229,9 +266,10 @@ func (l Logger) Errorln(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "ERROR",
|
Level: "ERROR",
|
||||||
level: LError,
|
level: LError,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
@@ -242,12 +280,13 @@ func (l Logger) Panic(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "PANIC",
|
Level: "PANIC",
|
||||||
level: LPanic,
|
level: LPanic,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
if len(args) >= 0 {
|
if len(args) > 0 {
|
||||||
switch args[0].(type) {
|
switch args[0].(type) {
|
||||||
case error:
|
case error:
|
||||||
panic(args[0])
|
panic(args[0])
|
||||||
@@ -265,12 +304,13 @@ func (l Logger) Panicf(format string, args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "PANIC",
|
Level: "PANIC",
|
||||||
level: LPanic,
|
level: LPanic,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
if len(args) >= 0 {
|
if len(args) > 0 {
|
||||||
switch args[0].(type) {
|
switch args[0].(type) {
|
||||||
case error:
|
case error:
|
||||||
panic(args[0])
|
panic(args[0])
|
||||||
@@ -288,12 +328,13 @@ func (l Logger) Panicln(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "PANIC",
|
Level: "PANIC",
|
||||||
level: LPanic,
|
level: LPanic,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
if len(args) >= 0 {
|
if len(args) > 0 {
|
||||||
switch args[0].(type) {
|
switch args[0].(type) {
|
||||||
case error:
|
case error:
|
||||||
panic(args[0])
|
panic(args[0])
|
||||||
@@ -311,9 +352,10 @@ func (l Logger) Fatal(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "FATAL",
|
Level: "FATAL",
|
||||||
level: LFatal,
|
level: LFatal,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
Flush()
|
Flush()
|
||||||
@@ -326,9 +368,10 @@ func (l Logger) Fatalf(format string, args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "FATAL",
|
Level: "FATAL",
|
||||||
level: LFatal,
|
level: LFatal,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
Flush()
|
Flush()
|
||||||
@@ -341,9 +384,10 @@ func (l Logger) Fatalln(args ...any) {
|
|||||||
e := Entry{
|
e := Entry{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Output: output,
|
Output: output,
|
||||||
File: fileInfo(l.FileInfoDepth),
|
File: fileInfo(2 + l.FileInfoDepth),
|
||||||
Level: "FATAL",
|
Level: "FATAL",
|
||||||
level: LFatal,
|
level: LFatal,
|
||||||
|
Namespace: l.Namespace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
Flush()
|
Flush()
|
||||||
|
|||||||
@@ -13,12 +13,15 @@ const (
|
|||||||
LFatal
|
LFatal
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const DefaultNamespace = "default"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
LogWriter chan Entry
|
LogWriter chan Entry
|
||||||
Level int
|
Level int
|
||||||
|
|
||||||
Client struct {
|
Client struct {
|
||||||
LogLevel Level `json:"level"`
|
LogLevel Level `json:"level"`
|
||||||
|
Namespaces []string `json:"namespaces"` // Empty slice means all namespaces
|
||||||
writer LogWriter
|
writer LogWriter
|
||||||
initialized bool
|
initialized bool
|
||||||
}
|
}
|
||||||
@@ -27,9 +30,11 @@ type (
|
|||||||
Output string `json:"output"`
|
Output string `json:"output"`
|
||||||
File string `json:"file"`
|
File string `json:"file"`
|
||||||
Level string `json:"level"`
|
Level string `json:"level"`
|
||||||
|
Namespace string `json:"namespace"`
|
||||||
level Level
|
level Level
|
||||||
}
|
}
|
||||||
Logger struct {
|
Logger struct {
|
||||||
FileInfoDepth int
|
FileInfoDepth int
|
||||||
|
Namespace string
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
30
main.go
30
main.go
@@ -5,20 +5,33 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/taigrr/log-socket/browser"
|
"github.com/taigrr/log-socket/v2/browser"
|
||||||
logger "github.com/taigrr/log-socket/log"
|
logger "github.com/taigrr/log-socket/v2/log"
|
||||||
"github.com/taigrr/log-socket/ws"
|
"github.com/taigrr/log-socket/v2/ws"
|
||||||
)
|
)
|
||||||
|
|
||||||
var addr = flag.String("addr", "0.0.0.0:8080", "http service address")
|
var addr = flag.String("addr", "0.0.0.0:8080", "http service address")
|
||||||
|
|
||||||
func generateLogs() {
|
func generateLogs() {
|
||||||
|
// Create loggers for different namespaces
|
||||||
|
apiLogger := logger.NewLogger("api")
|
||||||
|
dbLogger := logger.NewLogger("database")
|
||||||
|
authLogger := logger.NewLogger("auth")
|
||||||
|
|
||||||
for {
|
for {
|
||||||
logger.Info("This is an info log!")
|
logger.Info("This is a default namespace log!")
|
||||||
logger.Trace("This is a trace log!")
|
apiLogger.Info("API request received")
|
||||||
logger.Debug("This is a debug log!")
|
apiLogger.Debug("Processing API call")
|
||||||
logger.Warn("This is a warn log!")
|
|
||||||
logger.Error("This is an error log!")
|
dbLogger.Info("Database query executed")
|
||||||
|
dbLogger.Warn("Slow query detected")
|
||||||
|
|
||||||
|
authLogger.Info("User authentication successful")
|
||||||
|
authLogger.Error("Failed login attempt detected")
|
||||||
|
|
||||||
|
logger.Trace("This is a trace log in default namespace!")
|
||||||
|
logger.Warn("This is a warning in default namespace!")
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,6 +40,7 @@ func main() {
|
|||||||
defer logger.Flush()
|
defer logger.Flush()
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
http.HandleFunc("/ws", ws.LogSocketHandler)
|
http.HandleFunc("/ws", ws.LogSocketHandler)
|
||||||
|
http.HandleFunc("/api/namespaces", ws.NamespacesHandler)
|
||||||
http.HandleFunc("/", browser.LogSocketViewHandler)
|
http.HandleFunc("/", browser.LogSocketViewHandler)
|
||||||
go generateLogs()
|
go generateLogs()
|
||||||
logger.Fatal(http.ListenAndServe(*addr, nil))
|
logger.Fatal(http.ListenAndServe(*addr, nil))
|
||||||
|
|||||||
165
slog/handler.go
Normal file
165
slog/handler.go
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
// Package slog provides a [log/slog.Handler] that routes structured log
|
||||||
|
// records into the log-socket broadcasting system, giving every slog-based
|
||||||
|
// caller free WebSocket streaming and the browser viewer UI.
|
||||||
|
package slog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/taigrr/log-socket/v2/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Handler implements [slog.Handler] by converting each [slog.Record] into a
|
||||||
|
// log-socket [log.Entry] and feeding it through the normal broadcast path.
|
||||||
|
//
|
||||||
|
// Attributes accumulated via [Handler.WithAttrs] are prepended to the log
|
||||||
|
// message as key=value pairs. Groups set via [Handler.WithGroup] prefix
|
||||||
|
// attribute keys with "group.".
|
||||||
|
type Handler struct {
|
||||||
|
namespace string
|
||||||
|
level slog.Level
|
||||||
|
attrs []slog.Attr
|
||||||
|
groups []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure interface compliance at compile time.
|
||||||
|
var _ slog.Handler = (*Handler)(nil)
|
||||||
|
|
||||||
|
// Option configures a [Handler].
|
||||||
|
type Option func(*Handler)
|
||||||
|
|
||||||
|
// WithNamespace sets the log-socket namespace for entries produced by this
|
||||||
|
// handler. If empty, [log.DefaultNamespace] is used.
|
||||||
|
func WithNamespace(ns string) Option {
|
||||||
|
return func(h *Handler) {
|
||||||
|
h.namespace = ns
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithLevel sets the minimum slog level the handler will accept.
|
||||||
|
func WithLevel(l slog.Level) Option {
|
||||||
|
return func(h *Handler) {
|
||||||
|
h.level = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHandler returns a new [Handler] that writes to the log-socket broadcast
|
||||||
|
// system. Options may be used to set the namespace and minimum level.
|
||||||
|
func NewHandler(opts ...Option) *Handler {
|
||||||
|
h := &Handler{
|
||||||
|
namespace: log.DefaultNamespace,
|
||||||
|
level: slog.LevelDebug,
|
||||||
|
}
|
||||||
|
for _, o := range opts {
|
||||||
|
o(h)
|
||||||
|
}
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enabled reports whether the handler is configured to process records at
|
||||||
|
// the given level.
|
||||||
|
func (h *Handler) Enabled(_ context.Context, level slog.Level) bool {
|
||||||
|
return level >= h.level
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle converts r into a log-socket Entry and broadcasts it.
|
||||||
|
func (h *Handler) Handle(_ context.Context, r slog.Record) error {
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString(r.Message)
|
||||||
|
|
||||||
|
// Append pre-collected attrs.
|
||||||
|
for _, a := range h.attrs {
|
||||||
|
writeAttr(&b, h.groups, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append record-level attrs.
|
||||||
|
r.Attrs(func(a slog.Attr) bool {
|
||||||
|
writeAttr(&b, h.groups, a)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
file := "???"
|
||||||
|
if r.PC != 0 {
|
||||||
|
fs := runtime.CallersFrames([]uintptr{r.PC})
|
||||||
|
f, _ := fs.Next()
|
||||||
|
if f.File != "" {
|
||||||
|
short := f.File
|
||||||
|
if idx := strings.LastIndex(short, "/"); idx >= 0 {
|
||||||
|
short = short[idx+1:]
|
||||||
|
}
|
||||||
|
file = fmt.Sprintf("%s:%d", short, f.Line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
e := log.Entry{
|
||||||
|
Timestamp: r.Time,
|
||||||
|
Output: b.String(),
|
||||||
|
File: file,
|
||||||
|
Level: slogLevelToString(r.Level),
|
||||||
|
Namespace: h.namespace,
|
||||||
|
}
|
||||||
|
log.Broadcast(e)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithAttrs returns a new Handler with the given attributes appended.
|
||||||
|
func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||||
|
h2 := h.clone()
|
||||||
|
h2.attrs = append(h2.attrs, attrs...)
|
||||||
|
return h2
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithGroup returns a new Handler where subsequent attributes are nested
|
||||||
|
// under the given group name.
|
||||||
|
func (h *Handler) WithGroup(name string) slog.Handler {
|
||||||
|
if name == "" {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
h2 := h.clone()
|
||||||
|
h2.groups = append(h2.groups, name)
|
||||||
|
return h2
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) clone() *Handler {
|
||||||
|
h2 := &Handler{
|
||||||
|
namespace: h.namespace,
|
||||||
|
level: h.level,
|
||||||
|
attrs: make([]slog.Attr, len(h.attrs)),
|
||||||
|
groups: make([]string, len(h.groups)),
|
||||||
|
}
|
||||||
|
copy(h2.attrs, h.attrs)
|
||||||
|
copy(h2.groups, h.groups)
|
||||||
|
return h2
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeAttr(b *strings.Builder, groups []string, a slog.Attr) {
|
||||||
|
a.Value = a.Value.Resolve()
|
||||||
|
if a.Equal(slog.Attr{}) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.WriteByte(' ')
|
||||||
|
for _, g := range groups {
|
||||||
|
b.WriteString(g)
|
||||||
|
b.WriteByte('.')
|
||||||
|
}
|
||||||
|
b.WriteString(a.Key)
|
||||||
|
b.WriteByte('=')
|
||||||
|
b.WriteString(a.Value.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func slogLevelToString(l slog.Level) string {
|
||||||
|
switch {
|
||||||
|
case l >= slog.LevelError:
|
||||||
|
return "ERROR"
|
||||||
|
case l >= slog.LevelWarn:
|
||||||
|
return "WARN"
|
||||||
|
case l >= slog.LevelInfo:
|
||||||
|
return "INFO"
|
||||||
|
default:
|
||||||
|
return "DEBUG"
|
||||||
|
}
|
||||||
|
}
|
||||||
118
slog/handler_test.go
Normal file
118
slog/handler_test.go
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package slog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/taigrr/log-socket/v2/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// getWithTimeout reads from a log client with a timeout to avoid hanging tests.
|
||||||
|
func getWithTimeout(c *log.Client, timeout time.Duration) (log.Entry, bool) {
|
||||||
|
ch := make(chan log.Entry, 1)
|
||||||
|
go func() { ch <- c.Get() }()
|
||||||
|
select {
|
||||||
|
case e := <-ch:
|
||||||
|
return e, true
|
||||||
|
case <-time.After(timeout):
|
||||||
|
return log.Entry{}, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandler_Enabled(t *testing.T) {
|
||||||
|
h := NewHandler(WithLevel(slog.LevelWarn))
|
||||||
|
if h.Enabled(context.Background(), slog.LevelInfo) {
|
||||||
|
t.Error("expected Info to be disabled when min level is Warn")
|
||||||
|
}
|
||||||
|
if !h.Enabled(context.Background(), slog.LevelWarn) {
|
||||||
|
t.Error("expected Warn to be enabled")
|
||||||
|
}
|
||||||
|
if !h.Enabled(context.Background(), slog.LevelError) {
|
||||||
|
t.Error("expected Error to be enabled")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandler_Handle(t *testing.T) {
|
||||||
|
c := log.CreateClient()
|
||||||
|
defer c.Destroy()
|
||||||
|
c.SetLogLevel(log.LTrace)
|
||||||
|
|
||||||
|
h := NewHandler(WithNamespace("test-ns"))
|
||||||
|
logger := slog.New(h)
|
||||||
|
|
||||||
|
logger.Info("hello world", "key", "value")
|
||||||
|
|
||||||
|
e, ok := getWithTimeout(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out waiting for log entry")
|
||||||
|
}
|
||||||
|
if e.Namespace != "test-ns" {
|
||||||
|
t.Errorf("namespace = %q, want %q", e.Namespace, "test-ns")
|
||||||
|
}
|
||||||
|
if e.Level != "INFO" {
|
||||||
|
t.Errorf("level = %q, want INFO", e.Level)
|
||||||
|
}
|
||||||
|
if e.Output == "" {
|
||||||
|
t.Error("output should not be empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandler_WithAttrs(t *testing.T) {
|
||||||
|
c := log.CreateClient()
|
||||||
|
defer c.Destroy()
|
||||||
|
c.SetLogLevel(log.LTrace)
|
||||||
|
|
||||||
|
h := NewHandler()
|
||||||
|
h2 := h.WithAttrs([]slog.Attr{slog.String("service", "api")})
|
||||||
|
logger := slog.New(h2)
|
||||||
|
|
||||||
|
logger.Info("request")
|
||||||
|
|
||||||
|
e, ok := getWithTimeout(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
if e.Output != "request service=api" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "request service=api")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandler_WithGroup(t *testing.T) {
|
||||||
|
c := log.CreateClient()
|
||||||
|
defer c.Destroy()
|
||||||
|
c.SetLogLevel(log.LTrace)
|
||||||
|
|
||||||
|
h := NewHandler()
|
||||||
|
h2 := h.WithGroup("http").WithAttrs([]slog.Attr{slog.Int("status", 200)})
|
||||||
|
logger := slog.New(h2)
|
||||||
|
|
||||||
|
logger.Info("done")
|
||||||
|
|
||||||
|
e, ok := getWithTimeout(c, time.Second)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("timed out")
|
||||||
|
}
|
||||||
|
if e.Output != "done http.status=200" {
|
||||||
|
t.Errorf("output = %q, want %q", e.Output, "done http.status=200")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSlogLevelMapping(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
level slog.Level
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{slog.LevelDebug, "DEBUG"},
|
||||||
|
{slog.LevelInfo, "INFO"},
|
||||||
|
{slog.LevelWarn, "WARN"},
|
||||||
|
{slog.LevelError, "ERROR"},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
got := slogLevelToString(tt.level)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("slogLevelToString(%v) = %q, want %q", tt.level, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
ws/namespaces.go
Normal file
17
ws/namespaces.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package ws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
logger "github.com/taigrr/log-socket/v2/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NamespacesHandler returns a JSON list of all namespaces that have been used
|
||||||
|
func NamespacesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
namespaces := logger.GetNamespaces()
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||||
|
"namespaces": namespaces,
|
||||||
|
})
|
||||||
|
}
|
||||||
13
ws/server.go
13
ws/server.go
@@ -3,9 +3,10 @@ package ws
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
logger "github.com/taigrr/log-socket/log"
|
logger "github.com/taigrr/log-socket/v2/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{} // use default options
|
var upgrader = websocket.Upgrader{} // use default options
|
||||||
@@ -15,13 +16,21 @@ func SetUpgrader(u websocket.Upgrader) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LogSocketHandler(w http.ResponseWriter, r *http.Request) {
|
func LogSocketHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Get namespaces from query parameter, comma-separated
|
||||||
|
// Empty or missing means all namespaces
|
||||||
|
namespacesParam := r.URL.Query().Get("namespaces")
|
||||||
|
var namespaces []string
|
||||||
|
if namespacesParam != "" {
|
||||||
|
namespaces = strings.Split(namespacesParam, ",")
|
||||||
|
}
|
||||||
|
|
||||||
c, err := upgrader.Upgrade(w, r, nil)
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("upgrade:", err)
|
logger.Error("upgrade:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
lc := logger.CreateClient()
|
lc := logger.CreateClient(namespaces...)
|
||||||
defer lc.Destroy()
|
defer lc.Destroy()
|
||||||
lc.SetLogLevel(logger.LTrace)
|
lc.SetLogLevel(logger.LTrace)
|
||||||
logger.Info("Websocket client attached.")
|
logger.Info("Websocket client attached.")
|
||||||
|
|||||||
Reference in New Issue
Block a user