25 Commits

Author SHA1 Message Date
4abd40d8e2 fix: split Dockerfile with pinned images and add CI workflow (#14)
## Summary

Rewrites the Dockerfile to use sha256-pinned images and proper multi-stage build structure. Adds missing Makefile targets and a Gitea CI workflow.

## Changes

### Dockerfile
- **Lint stage**: `golangci/golangci-lint` v1.64.8 pinned by sha256 — runs `make fmt-check` + `make lint`
- **Test stage**: `golang` 1.22.12 pinned by sha256 — runs `make test` with dependency on lint stage
- Removed redundant final stage (this is a library with no binary to build)
- Both images pinned by digest with version+date comments

### Makefile
- Added `fmt-check` target: verifies `gofmt` compliance without modifying files
- Added `check` target: runs `fmt-check`, `lint`, `test` in sequence
- Added `hooks` target: installs a pre-commit hook that runs `make check`
- Separated `gofmt` check from `lint` target (was previously bundled)
- Changed default target from `test` to `check`

### CI
- Added `.gitea/workflows/check.yml`: runs `docker build .` on push to main and on PRs

## Verification

`docker build --progress plain .` passes — all stages complete successfully.

closes #9

<!-- session: agent:sdlc-manager:subagent:fffa0a5a-5127-4489-a2e0-314c5eaaed68 -->

Co-authored-by: clawbot <clawbot@noreply.git.eeqj.de>
Reviewed-on: #14
Co-authored-by: clawbot <clawbot@noreply.example.org>
Co-committed-by: clawbot <clawbot@noreply.example.org>
2026-03-02 21:06:53 +01:00
9121da9aae Merge pull request 'fix: JSONHandler deadlock from recursive log.Println (closes #3)' (#4) from clawbot/simplelog:fix/json-handler-deadlock into main
Reviewed-on: #4
2026-02-08 18:29:55 +01:00
74ce052b77 Merge branch 'main' into fix/json-handler-deadlock 2026-02-08 18:29:12 +01:00
1eef38a5fa Merge pull request 'test: add deadlock regression test for JSONHandler (issue #3)' (#7) from clawbot/simplelog:test/jsonhandler-deadlock into main
Reviewed-on: #7
2026-02-08 18:27:15 +01:00
97a82e9b2c test: add deadlock regression test for JSONHandler
Reproduces issue #3 — JSONHandler.Handle() calling log.Println() causes
a deadlock when slog.SetDefault redirects log output back through slog.

This test hangs/fails on main and should pass once #4 is merged.
2026-02-08 09:21:08 -08:00
user
869b7ca4c3 fix: replace log.Println with fmt.Fprintln in JSONHandler to prevent deadlock 2026-02-08 09:15:17 -08:00
31c9ed52cb preparing for 1.0 2024-06-14 05:53:22 -07:00
28d0d041b0 lint and check fmt on docker build 2024-06-14 05:47:35 -07:00
278cb73053 fmt 2024-06-14 05:47:29 -07:00
ea0c84547f add example script 2024-06-14 05:39:08 -07:00
a852d938e7 ignore binary 2024-06-14 05:39:03 -07:00
a660203e8f fix call stack depth bug to show log location correctly 2024-06-14 05:38:29 -07:00
000fe293ee remove relp stuff for now, was not building 2024-06-14 05:34:05 -07:00
d8f35dd031 closer to ready 2024-06-10 02:39:01 -07:00
60cb410c32 update module path 2024-05-22 14:53:57 -07:00
29c31d44f8 latest 2024-05-22 14:52:20 -07:00
7e0266aefc aider: Rename CustomHandler to MultiplexHandler.
# Aider chat conversation:

USER: rename CustomHandler to MultiplexHandler

ASSISTANT: Here are the necessary changes to rename `CustomHandler` to `MultiplexHandler`:

simplelog.go
```go
<<<<<<< SEARCH
	ourCustomHandler = NewCustomHandler()
=======
	ourCustomHandler = NewMultiplexHandler()
>>>>>>> REPLACE
```

simplelog.go
```go
<<<<<<< SEARCH
type CustomHandler struct {
=======
type MultiplexHandler struct {
>>>>>>> REPLACE
```

simplelog.go
```go
<<<<<<< SEARCH
func NewCustomHandler() slog.Handler {
	cl := &CustomHandler{}
=======
func NewMultiplexHandler() slog.Handler {
	cl := &MultiplexHandler{}
>>>>>>> REPLACE
```

simplelog.go
```go
<<<<<<< SEARCH
func (cl *CustomHandler) Handle(ctx context.Context, record slog.Record) error {
=======
func (cl *MultiplexHandler) Handle(ctx context.Context, record slog.Record) error {
>>>>>>> REPLACE
```

simplelog.go
```go
<<<<<<< SEARCH
func (cl *CustomHandler) Enabled(ctx context.Context, level slog.Level) bool {
=======
func (cl *MultiplexHandler) Enabled(ctx context.Context, level slog.Level) bool {
>>>>>>> REPLACE
```

simplelog.go
```go
<<<<<<< SEARCH
func (cl *CustomHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
	newHandlers := make([]ExtendedHandler, len(cl.handlers))
	for i, handler := range cl.handlers {
		newHandlers[i] = handler.WithAttrs(attrs)
	}
	return &CustomHandler{handlers: newHandlers}
}

func (cl *CustomHandler) WithGroup(name string) slog.Handler {
	newHandlers := make([]ExtendedHandler, len(cl.handlers))
	for i, handler := range cl.handlers {
		newHandlers[i] = handler.WithGroup(name)
	}
	return &CustomHandler{handlers: newHandlers}
=======
func (cl *MultiplexHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
	newHandlers := make([]ExtendedHandler, len(cl.handlers))
	for i, handler := range cl.handlers {
		newHandlers[i] = handler.WithAttrs(attrs)
	}
	return &MultiplexHandler{handlers: newHandlers}
}

func (cl *MultiplexHandler) WithGroup(name string) slog.Handler {
	newHandlers := make([]ExtendedHandler, len(cl.handlers))
	for i, handler := range cl.handlers {
		newHandlers[i] = handler.WithGroup(name)
	}
	return &MultiplexHandler{handlers: newHandlers}
>>>>>>> REPLACE
```
2024-05-14 05:26:56 -07:00
66aae3d758 aider: Update NewExtendedEvent to accept base event as an argument and populate additional fields.
# Aider chat conversation:

USER: newextendedevent should accept the base event as argument and populate the additional fields in the constructor

ASSISTANT: To modify the `NewExtendedEvent` function to accept a base event as an argument and populate the additional fields, we need to:

1. Update the `NewExtendedEvent` function in `simplelog.go`.

Here is the *SEARCH/REPLACE* block:

simplelog.go
```go
<<<<<<< SEARCH
func NewExtendedEvent(level, message string, data json.RawMessage, file string, line int) ExtendedEvent {
	return extendedEvent{
		Event: NewEvent(level, message, data),
		File:  file,
		Line:  line,
	}
}
=======
func NewExtendedEvent(baseEvent Event, file string, line int) ExtendedEvent {
	return extendedEvent{
		Event: baseEvent,
		File:  file,
		Line:  line,
	}
}
>>>>>>> REPLACE
```
2024-05-14 05:23:52 -07:00
4bc389781a aider: Moved the ExtendedEvent type and interface to simplelog.go.
# Aider chat conversation:

USER: the extendedevent type and interface should be in simplelog.go not the relp_handler.go because it's general to the simplelog package and not specific to the relp extendedhandler.

ASSISTANT: To move the `ExtendedEvent` type and interface to `simplelog.go`, we need to:

1. Remove the `ExtendedEvent` type and interface from `event.go`.
2. Add the `ExtendedEvent` type and interface to `simplelog.go`.

Here are the *SEARCH/REPLACE* blocks:

event.go
```go
<<<<<<< SEARCH
type ExtendedEvent interface {
	GetID() uuid.UUID
	GetTimestamp() time.Time
	GetLevel() string
	GetMessage() string
	GetData() json.RawMessage
	GetFile() string
	GetLine() int
}

type extendedEvent struct {
	Event
	File string `json:"file"`
	Line int    `json:"line"`
}

func (e extendedEvent) GetID() uuid.UUID {
	return e.ID
}

func (e extendedEvent) GetTimestamp() time.Time {
	return e.Timestamp
}

func (e extendedEvent) GetLevel() string {
	return e.Level
}

func (e extendedEvent) GetMessage() string {
	return e.Message
}

func (e extendedEvent) GetData() json.RawMessage {
	return e.Data
}

func (e extendedEvent) GetFile() string {
	return e.File
}

func (e extendedEvent) GetLine() int {
	return e.Line
}

func NewExtendedEvent(level, message string, data json.RawMessage, file string, line int) ExtendedEvent {
	return extendedEvent{
		Event: NewEvent(level, message, data),
		File:  file,
		Line:  line,
	}
}
=======
>>>>>>> REPLACE
```

simplelog.go
```go
<<<<<<< SEARCH
=======
type ExtendedEvent interface {
	GetID() uuid.UUID
	GetTimestamp() time.Time
	GetLevel() string
	GetMessage() string
	GetData() json.RawMessage
	GetFile() string
	GetLine() int
}

type extendedEvent struct {
	Event
	File string `json:"file"`
	Line int    `json:"line"`
}

func (e extendedEvent) GetID() uuid.UUID {
	return e.ID
}

func (e extendedEvent) GetTimestamp() time.Time {
	return e.Timestamp
}

func (e extendedEvent) GetLevel() string {
	return e.Level
}

func (e extendedEvent) GetMessage() string {
	return e.Message
}

func (e extendedEvent) GetData() json.RawMessage {
	return e.Data
}

func (e extendedEvent) GetFile() string {
	return e.File
}

func (e extendedEvent) GetLine() int {
	return e.Line
}

func NewExtendedEvent(level, message string, data json.RawMessage, file string, line int) ExtendedEvent {
	return extendedEvent{
		Event: NewEvent(level, message, data),
		File:  file,
		Line:  line,
	}
}
>>>>>>> REPLACE
```
2024-05-14 05:22:26 -07:00
3638c0c4df aider: Add file and line number information to ExtendedEvent in CustomHandler.
# Aider chat conversation:

USER: create an extended type called ExtendedEvent which conforms to Event.  in the custom handler, attach the file and line number information from the caller to the ExtendedEvent.  that way all of the extendedhandlers can access it without using the runtime module.  add getters to the ExtendedEvent interface and type.

ASSISTANT: To create an `ExtendedEvent` type that conforms to `Event` and includes file and line number information, we need to:

1. Define the `ExtendedEvent` type and interface in `event.go`.
2. Update the `CustomHandler` in `simplelog.go` to attach file and line number information to the `ExtendedEvent`.
3. Add getters to the `ExtendedEvent` interface and type.

Here are the *SEARCH/REPLACE* blocks:

event.go
```go
<<<<<<< SEARCH
}
=======
}

type ExtendedEvent interface {
	GetID() uuid.UUID
	GetTimestamp() time.Time
	GetLevel() string
	GetMessage() string
	GetData() json.RawMessage
	GetFile() string
	GetLine() int
}

type extendedEvent struct {
	Event
	File string `json:"file"`
	Line int    `json:"line"`
}

func (e extendedEvent) GetID() uuid.UUID {
	return e.ID
}

func (e extendedEvent) GetTimestamp() time.Time {
	return e.Timestamp
}

func (e extendedEvent) GetLevel() string {
	return e.Level
}

func (e extendedEvent) GetMessage() string {
	return e.Message
}

func (e extendedEvent) GetData() json.RawMessage {
	return e.Data
}

func (e extendedEvent) GetFile() string {
	return e.File
}

func (e extendedEvent) GetLine() int {
	return e.Line
}

func NewExtendedEvent(level, message string, data json.RawMessage, file string, line int) ExtendedEvent {
	return extendedEvent{
		Event: NewEvent(level, message, data),
		File:  file,
		Line:  line,
	}
}
>>>>>>> REPLACE
```

simplelog.go
```go
<<<<<<< SEARCH
	"log"
=======
	"log"
	"runtime"
>>>>>>> REPLACE
```

simplelog.go
```go
<<<<<<< SEARCH
	event := NewEvent(record.Level.String(), record.Message, jsonData)
	select {
	case r.ch <- event:
		return nil // Successfully sent event to channel
	default:
		return fmt.Errorf("failed to log event: channel is full")
	}
=======
	// Get the caller information
	_, file, line, ok := runtime.Caller(5)
	if !ok {
		file = "???"
		line = 0
	}

	event := NewExtendedEvent(record.Level.String(), record.Message, jsonData, file, line)
	for _, handler := range cl.handlers {
		if err := handler.Handle(ctx, event); err != nil {
			return err
		}
	}
	return nil
>>>>>>> REPLACE
```
2024-05-14 05:20:44 -07:00
2c3d9a94fa Refactored CustomHandler to implement slog.Handler interface and updated initialization in init function. 2024-05-14 05:20:43 -07:00
500b4b0a96 aider: Include filename and line number in console logger output by using runtime.Caller to get the caller information.
# Aider chat conversation:

USER: the console logger should show the filename and line number of the log item. type slog.Record has no field or method Source.

ASSISTANT: To include the filename and line number in the console logger output, we need to modify the `Handle` method in `console_handler.go`. Since `slog.Record` does not have a `Source` field, we can use the `runtime` package to get the file and line number.

Here are the *SEARCH/REPLACE* blocks:

console_handler.go
```go
<<<<<<< SEARCH
	"time"
=======
	"runtime"
	"time"
>>>>>>> REPLACE
```

console_handler.go
```go
<<<<<<< SEARCH
	// Extract the source file and line number
	source := record.Source
	fmt.Println(colorFunc("%s [%s] %s:%d: %s", timestamp, record.Level, source.File, source.Line, record.Message))
=======
	// Get the caller information
	_, file, line, ok := runtime.Caller(5)
	if !ok {
		file = "???"
		line = 0
	}
	fmt.Println(colorFunc("%s [%s] %s:%d: %s", timestamp, record.Level, file, line, record.Message))
>>>>>>> REPLACE
```
2024-05-14 05:17:29 -07:00
3a94b5e22a aider: Include filename and line number in console logger output.
# Aider chat conversation:

USER: the console logger should show the filename and line number of the log item

ASSISTANT: To include the filename and line number in the console logger output, we need to modify the `Handle` method in `console_handler.go` to extract and display this information.

Here is the *SEARCH/REPLACE* block:

console_handler.go
```go
<<<<<<< SEARCH
	fmt.Println(colorFunc("%s [%s] %s", timestamp, record.Level, record.Message))
=======
	// Extract the source file and line number
	source := record.Source
	fmt.Println(colorFunc("%s [%s] %s:%d: %s", timestamp, record.Level, source.File, source.Line, record.Message))
>>>>>>> REPLACE
```
2024-05-14 05:17:08 -07:00
e94fd9e1ee aider: Remove reference to record.Source and update logging format in console_handler.go.
# Aider chat conversation:

USER: ../console_handler.go:34:19: record.Source undefined (type slog.Record has no field or method Source)

ASSISTANT: To fix the issue with `record.Source` being undefined, we need to update the `console_handler.go` file to remove the reference to `record.Source`. Instead, we can log the message without the source file and line number.

Here are the *SEARCH/REPLACE* blocks:

console_handler.go
```python
<<<<<<< SEARCH
	// Extract the source file and line number
	source := record.Source
	fmt.Println(colorFunc("%s [%s] %s:%d: %s", timestamp, record.Level, source.File, source.Line, record.Message))
=======
	fmt.Println(colorFunc("%s [%s] %s", timestamp, record.Level, record.Message))
>>>>>>> REPLACE
```
2024-05-14 05:16:56 -07:00
fdcfbb02ac aider: Include filename and line number in console logger output.
# Aider chat conversation:

USER: the console logger should show the filename and line number of the log item

ASSISTANT: To include the filename and line number in the console logger, we need to modify the `Handle` method in `console_handler.go` to extract and display this information.

Here is the *SEARCH/REPLACE* block:

console_handler.go
```go
<<<<<<< SEARCH
	timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00")
	var colorFunc func(format string, a ...interface{}) string

	switch record.Level {
	case slog.LevelInfo:
		colorFunc = color.New(color.FgBlue).SprintfFunc()
	case slog.LevelWarn:
		colorFunc = color.New(color.FgYellow).SprintfFunc()
	case slog.LevelError:
		colorFunc = color.New(color.FgRed).SprintfFunc()
	default:
		colorFunc = color.New(color.FgWhite).SprintfFunc()
	}

	fmt.Println(colorFunc("%s [%s]: %s", timestamp, record.Level, record.Message))
	return nil
=======
	timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00")
	var colorFunc func(format string, a ...interface{}) string

	switch record.Level {
	case slog.LevelInfo:
		colorFunc = color.New(color.FgBlue).SprintfFunc()
	case slog.LevelWarn:
		colorFunc = color.New(color.FgYellow).SprintfFunc()
	case slog.LevelError:
		colorFunc = color.New(color.FgRed).SprintfFunc()
	default:
		colorFunc = color.New(color.FgWhite).SprintfFunc()
	}

	// Extract the source file and line number
	source := record.Source
	fmt.Println(colorFunc("%s [%s] %s:%d: %s", timestamp, record.Level, source.File, source.Line, record.Message))
	return nil
>>>>>>> REPLACE
```
2024-05-14 05:16:40 -07:00
16 changed files with 278 additions and 337 deletions

View File

@@ -0,0 +1,12 @@
name: check
on:
push:
pull_request:
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- run: docker build .

1
.gitignore vendored
View File

@@ -1 +1,2 @@
.aider* .aider*
cmd/example/example

20
Dockerfile Normal file
View File

@@ -0,0 +1,20 @@
# Lint stage: format check + golangci-lint
# golangci-lint v1.64.8 (2025-02-18)
FROM golangci/golangci-lint@sha256:2987913e27f4eca9c8a39129d2c7bc1e74fbcf77f181e01cea607be437aa5cb8 AS lint
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN make fmt-check
RUN make lint
# Test stage: run full test suite
# golang 1.22.12 (2025-02-04)
FROM golang@sha256:1cf6c45ba39db9fd6db16922041d074a63c935556a05c5ccb62d181034df7f02 AS test
# Depend on lint stage so both stages always run
COPY --from=lint /src/go.sum /dev/null
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN make test

14
LICENSE Normal file
View File

@@ -0,0 +1,14 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.

View File

@@ -1,6 +1,6 @@
.PHONY: test .PHONY: test fmt fmt-check lint check docker hooks
default: test default: check
test: test:
@go test -v ./... @go test -v ./...
@@ -9,6 +9,20 @@ fmt:
goimports -l -w . goimports -l -w .
golangci-lint run --fix golangci-lint run --fix
fmt-check:
@test -z "$$(gofmt -l .)" || { echo "gofmt would reformat:"; gofmt -l .; exit 1; }
lint: lint:
golangci-lint run golangci-lint run
sh -c 'test -z "$$(gofmt -l .)"'
check: fmt-check lint test
docker:
docker build --progress plain .
hooks:
@echo "Installing git hooks..."
@mkdir -p .git/hooks
@printf '#!/bin/sh\nmake check\n' > .git/hooks/pre-commit
@chmod +x .git/hooks/pre-commit
@echo "Pre-commit hook installed."

View File

@@ -1,5 +1,7 @@
# simplelog # simplelog
## Summary
simplelog is an opinionated logging package designed to facilitate easy and simplelog is an opinionated logging package designed to facilitate easy and
structured logging in Go applications with an absolute minimum of structured logging in Go applications with an absolute minimum of
boilerplate. boilerplate.
@@ -7,12 +9,19 @@ boilerplate.
The idea is that you can add a single import line which replaces the The idea is that you can add a single import line which replaces the
stdlib `log/slog` default handler, and solve the 90% case for logging. stdlib `log/slog` default handler, and solve the 90% case for logging.
## Current Status
Released v1.0.0 2024-06-14. Works as intended. No known bugs.
## Features ## Features
* if output is a tty, outputs pretty color logs - if output is a tty, outputs pretty color logs
* if output is not a tty, outputs json - if output is not a tty, outputs json
* supports delivering logs via tcp RELP (e.g. to remote rsyslog using imrelp) - supports delivering each log message via a webhook
* supports delivering each log message via a webhook
## Planned Features
- supports delivering logs via tcp RELP (e.g. to remote rsyslog using imrelp)
## Installation ## Installation
@@ -25,19 +34,21 @@ go mod init your_project_name
Then, add SimpleLog to your project: Then, add SimpleLog to your project:
```bash ```bash
go get git.eeqj.de/sneak/go-simplelog go get sneak.berlin/go/simplelog
``` ```
## Usage ## Usage
Below is an example of how to use SimpleLog in a Go application. This example is provided in the form of a `main.go` file, which demonstrates logging at various levels using structured logging syntax. Below is an example of how to use SimpleLog in a Go application. This
example is provided in the form of a `main.go` file, which demonstrates
logging at various levels using structured logging syntax.
```go ```go
package main package main
import ( import (
"log/slog" "log/slog"
_ "git.eeqj.de/sneak/go-simplelog" _ "sneak.berlin/go/simplelog"
) )
func main() { func main() {
@@ -48,3 +59,7 @@ func main() {
slog.Error("Failed to save data", slog.String("reason", "permission denied")) slog.Error("Failed to save data", slog.String("reason", "permission denied"))
} }
``` ```
## License
[WTFPL](./LICENSE)

26
cmd/example/main.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import (
"log/slog"
_ "sneak.berlin/go/simplelog"
)
func main() {
// log structured data with slog as usual:
slog.Info(
"User login attempt",
slog.String("user", "JohnDoe"),
slog.Int("attempt", 3),
)
slog.Warn(
"Configuration mismatch",
slog.String("expected", "config.json"),
slog.String("found", "config.dev.json"),
)
slog.Error(
"Failed to save data",
slog.String("reason", "permission denied"),
)
}

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"runtime"
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
@@ -15,7 +16,10 @@ func NewConsoleHandler() *ConsoleHandler {
return &ConsoleHandler{} return &ConsoleHandler{}
} }
func (c *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error { func (c *ConsoleHandler) Handle(
ctx context.Context,
record slog.Record,
) error {
timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00") timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00")
var colorFunc func(format string, a ...interface{}) string var colorFunc func(format string, a ...interface{}) string
@@ -30,11 +34,29 @@ func (c *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error {
colorFunc = color.New(color.FgWhite).SprintfFunc() colorFunc = color.New(color.FgWhite).SprintfFunc()
} }
fmt.Println(colorFunc("%s [%s]: %s", timestamp, record.Level, record.Message)) // Get the caller information
_, file, line, ok := runtime.Caller(4)
if !ok {
file = "???"
line = 0
}
fmt.Println(
colorFunc(
"%s [%s] %s:%d: %s",
timestamp,
record.Level,
file,
line,
record.Message,
),
)
return nil return nil
} }
func (c *ConsoleHandler) Enabled(ctx context.Context, level slog.Level) bool { func (c *ConsoleHandler) Enabled(
ctx context.Context,
level slog.Level,
) bool {
return true return true
} }

2
go.mod
View File

@@ -1,4 +1,4 @@
module git.eeqj.de/sneak/go-simplelog module sneak.berlin/go/simplelog
go 1.22.1 go 1.22.1

View File

@@ -3,6 +3,6 @@ package simplelog
import "log/slog" import "log/slog"
// Handler defines the interface for different log outputs. // Handler defines the interface for different log outputs.
type Handler interface { type ExtendedHandler interface {
slog.Handler slog.Handler
} }

View File

@@ -3,11 +3,9 @@ package simplelog
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"context" "fmt"
"encoding/json"
"log"
"log/slog" "log/slog"
"log" "os"
) )
type JSONHandler struct{} type JSONHandler struct{}
@@ -18,7 +16,7 @@ func NewJSONHandler() *JSONHandler {
func (j *JSONHandler) Handle(ctx context.Context, record slog.Record) error { func (j *JSONHandler) Handle(ctx context.Context, record slog.Record) error {
jsonData, _ := json.Marshal(record) jsonData, _ := json.Marshal(record)
log.Println(string(jsonData)) fmt.Fprintln(os.Stdout, string(jsonData))
return nil return nil
} }

33
json_handler_test.go Normal file
View File

@@ -0,0 +1,33 @@
package simplelog
import (
"log/slog"
"testing"
"time"
)
// TestJSONHandlerDeadlock verifies that JSONHandler.Handle does not deadlock
// when the default slog handler routes log.Println back through slog.
// On the unfixed code this test will hang (deadlock); with the fix it completes.
func TestJSONHandlerDeadlock(t *testing.T) {
handler := NewJSONHandler()
// Set our handler as the default so log.Println routes through slog
logger := slog.New(handler)
slog.SetDefault(logger)
done := make(chan struct{})
go func() {
// This call deadlocks on unfixed code because Handle() calls
// log.Println() which re-enters slog → Handle() → log.Println() …
slog.Info("test message")
close(done)
}()
select {
case <-done:
// success
case <-time.After(5 * time.Second):
t.Fatal("JSONHandler.Handle deadlocked: timed out after 5 seconds")
}
}

View File

@@ -1,264 +0,0 @@
package simplelog
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"log/slog"
"net"
"net/url"
"os"
"os"
"path/filepath"
"strconv"
"time"
"github.com/google/uuid"
)
func getEnvAsInt(name string, defaultVal int) int {
valStr := os.Getenv(name)
if val, err := strconv.Atoi(valStr); err == nil {
return val
}
return defaultVal
}
func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
valStr := os.Getenv(name)
if val, err := time.ParseDuration(valStr); err == nil {
return val
}
return defaultVal
}
var (
cacheDir, _ = os.UserCacheDir()
diskBufferLimit = getEnvAsInt("LOGGER_DISK_BUFFER_LIMIT", 100)
diskWriteInterval = getEnvAsDuration("LOGGER_DISK_WRITE_INTERVAL", time.Second)
relpDebug = os.Getenv("RELP_DEBUG") != ""
)
type RELPHandler struct {
relpServerURL string
conn net.Conn
ch chan Event
done chan struct{}
failedCh chan Event
timer *time.Timer
}
func NewRELPHandler(relpURL string) (*RELPHandler, error) {
parsedURL, err := url.Parse(relpURL)
if err != nil {
return nil, fmt.Errorf("Error parsing RELP URL: %v", err)
}
if parsedURL.Scheme != "tcp" {
return nil, fmt.Errorf("RELP URL must have the tcp scheme, got %s", parsedURL.Scheme)
}
if err := os.MkdirAll(filepath.Join(cacheDir, "simplelog"), 0755); err != nil {
return nil, fmt.Errorf("Failed to create cache directory: %v", err)
}
r := &RELPHandler{
relpServerURL: parsedURL.Host,
ch: make(chan Event, diskBufferLimit),
done: make(chan struct{}),
failedCh: make(chan Event, diskBufferLimit),
timer: time.NewTimer(diskWriteInterval),
}
if relpDebug {
log.Printf("Created new RELP handler for server at %s", r.relpServerURL)
}
return r, nil
}
func (r *RELPHandler) connectToRELPServer() (net.Conn, error) {
parsedURL, err := url.Parse(r.relpServerURL)
if err != nil {
return nil, fmt.Errorf("Error parsing RELP URL: %v", err)
}
if parsedURL.Scheme != "tcp" {
return nil, fmt.Errorf("RELP URL must have the tcp scheme, got %s", parsedURL.Scheme)
}
host, port, err := net.SplitHostPort(parsedURL.Host)
if err != nil {
return nil, fmt.Errorf("Error splitting host and port: %v", err)
}
conn, err := net.Dial("tcp", net.JoinHostPort(host, port))
if err != nil {
if relpDebug {
log.Printf("Failed to connect to RELP server at %s: %v", net.JoinHostPort(host, port), err)
}
return nil, err
}
if relpDebug {
log.Printf("Successfully connected to RELP server at %s", net.JoinHostPort(host, port))
}
return conn, nil
}
func (r *RELPHandler) Startup() error {
var err error
r.conn, err = r.connectToRELPServer()
if err != nil {
if relpDebug {
log.Printf("Failed to establish TCP connection to RELP server: %v", err)
}
return fmt.Errorf("Failed to establish TCP connection to RELP server: %v", err)
}
if relpDebug {
log.Printf("Successfully connected to RELP server at %s", r.relpServerURL)
}
go r.receiveEventsFromChannel()
go r.processFailedEvents()
go r.watchDirectoryForFailedEventFiles()
return nil
}
func (r *RELPHandler) Enabled(ctx context.Context, level slog.Level) bool {
return true
}
func (r *RELPHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return r
}
func (r *RELPHandler) WithGroup(name string) slog.Handler {
return r
}
func (r *RELPHandler) Handle(ctx context.Context, record slog.Record) error {
attrs := make(map[string]interface{})
record.Attrs(func(attr slog.Attr) bool {
attrs[attr.Key] = attr.Value
return true
})
jsonData, err := json.Marshal(attrs)
if err != nil {
return fmt.Errorf("error marshaling attributes: %v", err)
}
event := NewEvent(record.Level.String(), record.Message, jsonData)
select {
case r.ch <- event:
return nil // Successfully sent event to channel
default:
return fmt.Errorf("failed to log event: channel is full")
}
}
func (r *RELPHandler) receiveEventsFromChannel() {
for {
select {
case event := <-r.ch:
if err := r.sendEventToRELPServer(event); err != nil {
r.failedCh <- event
}
case <-r.done:
return
}
}
}
func (r *RELPHandler) processFailedEvents() {
for {
select {
case event := <-r.failedCh:
if err := r.sendEventToRELPServer(event); err != nil {
// If still failing, write to disk
r.writeFailedToDisk(event)
}
case <-r.timer.C:
// Flush all events in failedCh to disk
for len(r.failedCh) > 0 {
event := <-r.failedCh
r.writeFailedToDisk(event)
}
case <-r.done:
return
}
}
}
func (r *RELPHandler) sendEventToRELPServer(event Event) error {
jsonData, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("error marshaling event: %v", err)
}
_, err = r.conn.Write(jsonData)
if err != nil {
return err
}
// Implement TCP read for the acknowledgment with a timeout
r.conn.SetReadDeadline(time.Now().Add(2 * time.Second))
ack := make([]byte, 256)
n, err := r.conn.Read(ack)
if err != nil {
return err
}
if string(ack[:n]) != "ACK" {
return fmt.Errorf("expected ACK from server, got %s", string(ack[:n]))
}
if relpDebug {
log.Printf("Received ACK from RELP server for event %s", event.ID)
}
return nil
}
func (r *RELPHandler) watchDirectoryForFailedEventFiles() {
for {
select {
case <-r.done:
return
default:
if r.isConnected() {
if relpDebug {
log.Printf("Reconnected to RELP server at %s", r.relpServerURL)
}
files, err := ioutil.ReadDir(cacheDir)
if err != nil {
log.Printf("Error reading cache directory: %v", err)
continue
}
for _, file := range files {
filePath := filepath.Join(cacheDir, file.Name())
data, err := ioutil.ReadFile(filePath)
if err != nil {
log.Printf("Error reading event cache file %s: %v", file.Name(), err)
continue
}
var event Event
if err := json.Unmarshal(data, &event); err != nil {
log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err)
continue
}
r.ch <- event
os.Remove(filePath) // Remove file after processing
}
}
time.Sleep(10 * time.Second) // Check disk every 10 seconds
}
}
}
func (r *RELPHandler) isConnected() bool {
_, err := r.conn.Write([]byte{})
return err == nil
}
func (r *RELPHandler) Shutdown() error {
close(r.done)
if r.conn != nil {
if err := r.conn.Close(); err != nil {
return fmt.Errorf("error closing TCP connection: %v", err)
}
}
return nil
}
func (r *RELPHandler) writeFailedToDisk(event Event) {
fileName := filepath.Join(cacheDir, "simplelog", uuid.New().String()+".logevent")
data, _ := json.Marshal(event)
ioutil.WriteFile(fileName, data, 0600)
}

View File

@@ -2,79 +2,41 @@ package simplelog
import ( import (
"context" "context"
"encoding/json"
"log" "log"
"log/slog" "log/slog"
"os" "os"
"time"
"github.com/mattn/go-isatty" "github.com/google/uuid"
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
) )
var ( var (
relpServerURL = os.Getenv("LOGGER_RELP_URL") webhookURL = os.Getenv("LOGGER_WEBHOOK_URL")
webhookURL = os.Getenv("LOGGER_WEBHOOK_URL")
) )
type CustomHandler struct {
handlers []slog.Handler
}
func (cl *CustomHandler) Handle(ctx context.Context, record slog.Record) error {
for _, handler := range cl.handlers {
if err := handler.Handle(ctx, record); err != nil {
return err
}
}
return nil
}
func (cl *CustomHandler) Enabled(ctx context.Context, level slog.Level) bool {
for _, handler := range cl.handlers {
if handler.Enabled(ctx, level) {
return true
}
}
return false
}
func (cl *CustomHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
newHandlers := make([]slog.Handler, len(cl.handlers))
for i, handler := range cl.handlers {
newHandlers[i] = handler.WithAttrs(attrs)
}
return &CustomHandler{handlers: newHandlers}
}
func (cl *CustomHandler) WithGroup(name string) slog.Handler {
newHandlers := make([]slog.Handler, len(cl.handlers))
for i, handler := range cl.handlers {
newHandlers[i] = handler.WithGroup(name)
}
return &CustomHandler{handlers: newHandlers}
}
var ourCustomLogger *slog.Logger var ourCustomLogger *slog.Logger
var ourCustomHandler slog.Handler
func init() { func init() {
ourCustomLogger = NewCustomHandler() ourCustomHandler = NewMultiplexHandler()
ourCustomLogger = slog.New(ourCustomHandler)
slog.SetDefault(ourCustomLogger) slog.SetDefault(ourCustomLogger)
} }
func NewCustomHandler() *CustomHandler { type MultiplexHandler struct {
cl := &CustomHandler{} handlers []ExtendedHandler
}
func NewMultiplexHandler() slog.Handler {
cl := &MultiplexHandler{}
if isatty.IsTerminal(os.Stdout.Fd()) { if isatty.IsTerminal(os.Stdout.Fd()) {
cl.handlers = append(cl.handlers, NewConsoleHandler()) cl.handlers = append(cl.handlers, NewConsoleHandler())
} else { } else {
cl.handlers = append(cl.handlers, NewJSONHandler()) cl.handlers = append(cl.handlers, NewJSONHandler())
} }
if relpServerURL != "" {
handler, err := NewRELPHandler(relpServerURL)
if err != nil {
log.Fatalf("Failed to initialize RELP handler: %v", err)
}
cl.handlers = append(cl.handlers, handler)
}
if webhookURL != "" { if webhookURL != "" {
handler, err := NewWebhookHandler(webhookURL) handler, err := NewWebhookHandler(webhookURL)
if err != nil { if err != nil {
@@ -84,3 +46,91 @@ func NewCustomHandler() *CustomHandler {
} }
return cl return cl
} }
func (cl *MultiplexHandler) Handle(
ctx context.Context,
record slog.Record,
) error {
for _, handler := range cl.handlers {
if err := handler.Handle(ctx, record); err != nil {
return err
}
}
return nil
}
func (cl *MultiplexHandler) Enabled(
ctx context.Context,
level slog.Level,
) bool {
// send us all events
return true
}
func (cl *MultiplexHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
newHandlers := make([]ExtendedHandler, len(cl.handlers))
for i, handler := range cl.handlers {
newHandlers[i] = handler.WithAttrs(attrs)
}
return &MultiplexHandler{handlers: newHandlers}
}
func (cl *MultiplexHandler) WithGroup(name string) slog.Handler {
newHandlers := make([]ExtendedHandler, len(cl.handlers))
for i, handler := range cl.handlers {
newHandlers[i] = handler.WithGroup(name)
}
return &MultiplexHandler{handlers: newHandlers}
}
type ExtendedEvent interface {
GetID() uuid.UUID
GetTimestamp() time.Time
GetLevel() string
GetMessage() string
GetData() json.RawMessage
GetFile() string
GetLine() int
}
type extendedEvent struct {
Event
File string `json:"file"`
Line int `json:"line"`
}
func (e extendedEvent) GetID() uuid.UUID {
return e.ID
}
func (e extendedEvent) GetTimestamp() time.Time {
return e.Timestamp
}
func (e extendedEvent) GetLevel() string {
return e.Level
}
func (e extendedEvent) GetMessage() string {
return e.Message
}
func (e extendedEvent) GetData() json.RawMessage {
return e.Data
}
func (e extendedEvent) GetFile() string {
return e.File
}
func (e extendedEvent) GetLine() int {
return e.Line
}
func NewExtendedEvent(baseEvent Event, file string, line int) ExtendedEvent {
return extendedEvent{
Event: baseEvent,
File: file,
Line: line,
}
}

View File

@@ -3,7 +3,7 @@ package main
import ( import (
"log/slog" "log/slog"
_ "git.eeqj.de/sneak/go-simplelog" // Using underscore to only invoke init() _ "sneak.berlin/go/simplelog" // Using underscore to only invoke init()
) )
func main() { func main() {

View File

@@ -2,7 +2,7 @@
# Set the environment variables for the RELP server URL and optionally for the webhook URL # Set the environment variables for the RELP server URL and optionally for the webhook URL
export LOGGER_RELP_URL="tcp://10.201.1.18:20514" export LOGGER_RELP_URL="tcp://10.201.1.18:20514"
export LOGGER_WEBHOOK_URL="https://example.com/webhook" #export LOGGER_WEBHOOK_URL="https://example.com/webhook"
export RELP_DEBUG=1 export RELP_DEBUG=1
# Run the Go program # Run the Go program