CodeSampleX

Sample

github.com/go-kit/log v0.1.0: SwapLogger

Verified sample for golang github.com/go-kit/log v0.1.0: SwapLogger. The contract ran on go 1.26 · linux debian/x64 · docker and passed.

sha256:4349a182255375551be794247f84c968568e8f947fd9ed9236e150a8a9820cb1

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment linux 24 · ubuntu · glibc 2.39 x64 go

Verification-run environments

Environment Contract Stages Run
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-03

Case

HOW
Goal
verify log.SwapLogger in pkg:golang/github.com/go-kit/log@v0.1.0
Packages
Symbols
  • log.SwapLogger
Created
2026-09-03T18:32:00Z

Contract

  1. log.SwapLogger zero value discards all log events without error
  2. log.SwapLogger Swap dynamically replaces the active delegate logger with new log implementations
  3. log.SwapLogger Swap with nil reverts to discarding log events without error
  4. log.SwapLogger safely handles concurrent Log and Swap calls across multiple goroutines

Files

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • swaplogger.go
  • test/contract.go

Download the source artifact (tar.gz)

Source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify log.SwapLogger in pkg:golang/github.com/go-kit/log@v0.1.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/go-kit/log@v0.1.0
Demonstrate these symbols/APIs:
  - log.SwapLogger

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:636a3cda0a29dc2e2672340472a801beb1dfd0abbeecc7d7ed1d02730c2da247","contract":["log.SwapLogger zero value discards all log events without error","log.SwapLogger Swap dynamically replaces the active delegate logger with new log implementations","log.SwapLogger Swap with nil reverts to discarding log events without error","log.SwapLogger safely handles concurrent Log and Swap calls across multiple goroutines"],"goal":"verify log.SwapLogger in pkg:golang/github.com/go-kit/log@v0.1.0","kind":"HOW","packages":["pkg:golang/github.com/go-kit/log@v0.1.0"],"schemaVersion":1,"symbols":["log.SwapLogger"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/go-kit/log@v0.1.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/go-kit/log@v0.1.0","symbols":["log.SwapLogger"],"verifierAdapter":"golang@1"}
go.mod
module example.com/swaploggersample

go 1.26.6

require github.com/go-kit/log v0.1.0

require github.com/go-logfmt/logfmt v0.5.0 // indirect
go.sum
github.com/go-kit/log v0.1.0 h1:DGJh0Sm43HbOeYDNnVZFl8BvcYVvjD5bqYJvp0REbwQ=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-logfmt/logfmt v0.5.0 h1:TrB8swr/68K7m9CcGut2g3UOihhbcbiMAYiuTXdEih4=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify log.SwapLogger in pkg:golang/github.com/go-kit/log@v0.1.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/go-kit/log@v0.1.0"
  ],
  "symbols": [
    "log.SwapLogger"
  ]
}
swaplogger.go
package swaplogger

import (
	"github.com/go-kit/log"
)

// DynamicLogger wraps a log.SwapLogger to manage dynamic logger replacement.
type DynamicLogger struct {
	swap log.SwapLogger
}

// New creates a new DynamicLogger with an optional initial logger.
func New(initial log.Logger) *DynamicLogger {
	d := &DynamicLogger{}
	if initial != nil {
		d.swap.Swap(initial)
	}
	return d
}

// Log forwards key-value pairs to the underlying SwapLogger.
func (d *DynamicLogger) Log(keyvals ...interface{}) error {
	return d.swap.Log(keyvals...)
}

// Swap safely updates the underlying active logger concurrently.
func (d *DynamicLogger) Swap(logger log.Logger) {
	d.swap.Swap(logger)
}

// Underlying returns the pointer to the wrapped log.SwapLogger.
func (d *DynamicLogger) Underlying() *log.SwapLogger {
	return &d.swap
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"
	"strings"
	"sync"

	swaplogger "example.com/swaploggersample"
	"github.com/go-kit/log"
)

func main() {
	// Assertion 1: Zero value log.SwapLogger discards all log events without error
	var zeroLogger log.SwapLogger
	if err := zeroLogger.Log("key", "value", "count", 42); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: zero value SwapLogger returned error: %v\n", err)
		os.Exit(1)
	}

	// Assertion 2: Swap dynamically delegates to a JSON logger
	bufJSON := &bytes.Buffer{}
	jsonLogger := log.NewJSONLogger(bufJSON)
	zeroLogger.Swap(jsonLogger)

	if err := zeroLogger.Log("msg", "hello", "code", 200); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: JSON SwapLogger returned error: %v\n", err)
		os.Exit(1)
	}
	gotJSON := bufJSON.String()
	if !strings.Contains(gotJSON, `"msg":"hello"`) || !strings.Contains(gotJSON, `"code":200`) {
		fmt.Fprintf(os.Stderr, "FAIL: unexpected JSON output: %q\n", gotJSON)
		os.Exit(1)
	}

	// Assertion 3: Swap dynamically delegates to a logfmt logger
	bufFmt := &bytes.Buffer{}
	fmtLogger := log.NewLogfmtLogger(bufFmt)
	zeroLogger.Swap(fmtLogger)

	if err := zeroLogger.Log("msg", "swapped", "status", "ok"); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Logfmt SwapLogger returned error: %v\n", err)
		os.Exit(1)
	}
	gotFmt := bufFmt.String()
	if !strings.Contains(gotFmt, "msg=swapped") || !strings.Contains(gotFmt, "status=ok") {
		fmt.Fprintf(os.Stderr, "FAIL: unexpected Logfmt output: %q\n", gotFmt)
		os.Exit(1)
	}

	// Assertion 4: Swap with nil reverts to discarding log events without error
	zeroLogger.Swap(nil)
	if err := zeroLogger.Log("ignored", "yes"); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Swap(nil) returned error: %v\n", err)
		os.Exit(1)
	}

	// Assertion 5: Safe concurrent Log and Swap calls across multiple goroutines
	var concurrentLogger log.SwapLogger
	bufSync1 := &bytes.Buffer{}
	bufSync2 := &bytes.Buffer{}
	l1 := log.NewSyncLogger(log.NewLogfmtLogger(bufSync1))
	l2 := log.NewSyncLogger(log.NewLogfmtLogger(bufSync2))
	concurrentLogger.Swap(l1)

	var wg sync.WaitGroup
	startCh := make(chan struct{})

	// Log workers
	for i := 0; i < 8; i++ {
		wg.Add(1)
		go func(workerID int) {
			defer wg.Done()
			<-startCh
			for j := 0; j < 500; j++ {
				if err := concurrentLogger.Log("worker", workerID, "iter", j); err != nil {
					fmt.Fprintf(os.Stderr, "FAIL: concurrent Log returned error: %v\n", err)
					os.Exit(1)
				}
			}
		}(i)
	}

	// Swap workers
	for i := 0; i < 4; i++ {
		wg.Add(1)
		go func(swapID int) {
			defer wg.Done()
			<-startCh
			for j := 0; j < 100; j++ {
				if j%2 == 0 {
					concurrentLogger.Swap(l2)
				} else {
					concurrentLogger.Swap(l1)
				}
			}
		}(i)
	}

	close(startCh)
	wg.Wait()

	// Assertion 6: DynamicLogger wrapper delegating through SwapLogger
	dynBuf := &bytes.Buffer{}
	dyn := swaplogger.New(log.NewLogfmtLogger(dynBuf))
	if err := dyn.Log("layer", "dynamic", "state", "active"); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: DynamicLogger Log error: %v\n", err)
		os.Exit(1)
	}
	if !strings.Contains(dynBuf.String(), "layer=dynamic") || !strings.Contains(dynBuf.String(), "state=active") {
		fmt.Fprintf(os.Stderr, "FAIL: DynamicLogger output mismatch: %q\n", dynBuf.String())
		os.Exit(1)
	}

	dynBuf2 := &bytes.Buffer{}
	dyn.Swap(log.NewJSONLogger(dynBuf2))
	if err := dyn.Log("swapped", true); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: DynamicLogger post-swap Log error: %v\n", err)
		os.Exit(1)
	}
	if !strings.Contains(dynBuf2.String(), `"swapped":true`) {
		fmt.Fprintf(os.Stderr, "FAIL: DynamicLogger post-swap JSON mismatch: %q\n", dynBuf2.String())
		os.Exit(1)
	}

	fmt.Println("PASS: log.SwapLogger contract passed")
}

Origin Seeder

anonymous