CodeSampleX

Sample

github.com/go-kit/log v0.2.1: NewLogfmtLogger, NewJSONLogger, With

Verified sample for golang github.com/go-kit/log v0.2.1: NewLogfmtLogger, NewJSONLogger, With. The contract ran on go 1.26 · linux alpine/x64 · docker and…

sha256:4c44763d14139340391901ade894b536afe1028886da217c9675f781c7872e4a

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
2
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 alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-23
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-07

Case

HOW
Goal
verify pkg:golang/github.com/go-kit/log@v0.2.1
Packages
Symbols
  • log.NewLogfmtLogger
  • log.NewJSONLogger
  • log.With
  • log.Timestamp
  • level.NewFilter
  • log.NewSyncLogger
Created
2026-08-23T17:34:20Z

Contract

  1. log.NewLogfmtLogger formats structured key-value pairs into logfmt strings
  2. log.NewJSONLogger formats structured key-value pairs as valid JSON lines
  3. log.With preserves contextual key-value pairs across log invocations
  4. log.Timestamp dynamically evaluates timestamp Valuers on each log call
  5. level.NewFilter squelches debug messages and allows info and error messages
  6. log.NewSyncLogger provides thread-safe structured logging across concurrent goroutines

Files

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • 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 pkg:golang/github.com/go-kit/log@v0.2.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/go-kit/log@v0.2.1
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:35bcd69f32978d1e89fe6fd252ad692332b98ffc06ae846dee85959326825eae","contract":["log.NewLogfmtLogger formats structured key-value pairs into logfmt strings","log.NewJSONLogger formats structured key-value pairs as valid JSON lines","log.With preserves contextual key-value pairs across log invocations","log.Timestamp dynamically evaluates timestamp Valuers on each log call","level.NewFilter squelches debug messages and allows info and error messages","log.NewSyncLogger provides thread-safe structured logging across concurrent goroutines"],"goal":"verify pkg:golang/github.com/go-kit/log@v0.2.1","kind":"HOW","packages":["pkg:golang/github.com/go-kit/log@v0.2.1"],"schemaVersion":1,"symbols":["log.NewLogfmtLogger","log.NewJSONLogger","log.With","log.Timestamp","level.NewFilter","log.NewSyncLogger"]},"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.2.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/go-kit/log@v0.2.1","symbols":["log.NewLogfmtLogger","log.NewJSONLogger","log.With","log.Timestamp","level.NewFilter","log.NewSyncLogger"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

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

require github.com/go-logfmt/logfmt v0.5.1 // indirect
go.sum
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/go-kit/log@v0.2.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/go-kit/log@v0.2.1"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.go
package main

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"strings"
	"sync"
	"time"

	"github.com/go-kit/log"
	"github.com/go-kit/log/level"
)

func main() {
	if err := runTests(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract test failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All contract tests passed successfully.")
}

func runTests() error {
	// 1. Test log.NewLogfmtLogger format encoding
	var buf bytes.Buffer
	logger := log.NewLogfmtLogger(&buf)
	if err := logger.Log("msg", "hello world", "status", 200, "active", true); err != nil {
		return fmt.Errorf("logfmt logger.Log failed: %w", err)
	}
	out := strings.TrimSpace(buf.String())
	if out != `msg="hello world" status=200 active=true` {
		return fmt.Errorf("unexpected logfmt output: %q", out)
	}

	// 2. Test log.NewJSONLogger format encoding
	buf.Reset()
	jsonLogger := log.NewJSONLogger(&buf)
	if err := jsonLogger.Log("msg", "task started", "count", 42); err != nil {
		return fmt.Errorf("JSON logger.Log failed: %w", err)
	}
	var data map[string]interface{}
	if err := json.Unmarshal(buf.Bytes(), &data); err != nil {
		return fmt.Errorf("JSON unmarshal failed: %w (output: %q)", err, buf.String())
	}
	if data["msg"] != "task started" || data["count"] != float64(42) {
		return fmt.Errorf("unexpected JSON output: %+v", data)
	}

	// 3. Test log.With context preservation
	buf.Reset()
	ctxLogger := log.With(logger, "service", "service-a", "env", "test")
	if err := ctxLogger.Log("event", "process", "amount", 99.5); err != nil {
		return fmt.Errorf("contextual logger.Log failed: %w", err)
	}
	out = strings.TrimSpace(buf.String())
	if out != `service=service-a env=test event=process amount=99.5` {
		return fmt.Errorf("unexpected contextual logfmt output: %q", out)
	}

	// 4. Test log.Valuer dynamic evaluation (e.g. Timestamp)
	buf.Reset()
	fixedTime := time.Date(2026, 8, 23, 12, 0, 0, 0, time.UTC)
	timeValuer := log.Timestamp(func() time.Time {
		return fixedTime
	})
	valLogger := log.With(logger, "ts", timeValuer)
	if err := valLogger.Log("msg", "timestamped"); err != nil {
		return fmt.Errorf("valuer logger.Log failed: %w", err)
	}
	out = strings.TrimSpace(buf.String())
	if out != `ts=2026-08-23T12:00:00Z msg=timestamped` {
		return fmt.Errorf("unexpected valuer output: %q", out)
	}

	// 5. Test level.NewFilter filtering behavior
	buf.Reset()
	filterLogger := level.NewFilter(logger, level.AllowInfo())
	
	// Debug should be squelched (no output)
	if err := level.Debug(filterLogger).Log("msg", "debug detail"); err != nil {
		return fmt.Errorf("level.Debug log failed: %w", err)
	}
	if buf.Len() != 0 {
		return fmt.Errorf("expected debug to be squelched, got: %q", buf.String())
	}

	// Info should be emitted
	if err := level.Info(filterLogger).Log("msg", "info detail"); err != nil {
		return fmt.Errorf("level.Info log failed: %w", err)
	}
	out = strings.TrimSpace(buf.String())
	if out != `level=info msg="info detail"` {
		return fmt.Errorf("unexpected level.Info output: %q", out)
	}

	// Error should be emitted
	buf.Reset()
	if err := level.Error(filterLogger).Log("msg", "error detail", "err", errors.New("operation failed")); err != nil {
		return fmt.Errorf("level.Error log failed: %w", err)
	}
	out = strings.TrimSpace(buf.String())
	if out != `level=error msg="error detail" err="operation failed"` {
		return fmt.Errorf("unexpected level.Error output: %q", out)
	}

	// 6. Test log.NewSyncLogger concurrency safety
	buf.Reset()
	syncLogger := log.NewSyncLogger(logger)
	var wg sync.WaitGroup
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func(idx int) {
			defer wg.Done()
			_ = syncLogger.Log("worker", idx, "status", "ok")
		}(i)
	}
	wg.Wait()
	lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
	if len(lines) != 10 {
		return fmt.Errorf("expected 10 log lines from concurrent workers, got %d", len(lines))
	}

	return nil
}

Origin Seeder

anonymous