CodeSampleX

Sample

go.uber.org/zap v1.26.0: RedirectStdLog

Verified sample for golang go.uber.org/zap v1.26.0: RedirectStdLog. The contract ran on go 1.26 · linux debian/x64 · docker and passed.

sha256:15dcd41c8f9035434dd7a223e31899c86c341e907cff1e4cb64a22fe305b6a41

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 zap.RedirectStdLog in pkg:golang/go.uber.org/zap@v1.26.0
Packages
Symbols
  • zap.RedirectStdLog
Created
2026-09-03T06:51:10Z

Contract

  1. zap.RedirectStdLog redirects standard library log output to the zap logger at InfoLevel
  2. invoking the returned restore function reinstates original log routing
  3. zap.RedirectStdLog output is filtered when the zap logger level threshold is configured above InfoLevel

Files

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • redirect.go
  • 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 zap.RedirectStdLog in pkg:golang/go.uber.org/zap@v1.26.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/go.uber.org/zap@v1.26.0
Demonstrate these symbols/APIs:
  - zap.RedirectStdLog

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:4aa970a5732730a19335cc9fcb62dbd9a6536d74c08bfcf6f14ce978a6157c17","contract":["zap.RedirectStdLog redirects standard library log output to the zap logger at InfoLevel","invoking the returned restore function reinstates original log routing","zap.RedirectStdLog output is filtered when the zap logger level threshold is configured above InfoLevel"],"goal":"verify zap.RedirectStdLog in pkg:golang/go.uber.org/zap@v1.26.0","kind":"HOW","packages":["pkg:golang/go.uber.org/zap@v1.26.0"],"schemaVersion":1,"symbols":["zap.RedirectStdLog"]},"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/go.uber.org/zap@v1.26.0"],"schemaVersion":1,"subject":"pkg:golang/go.uber.org/zap@v1.26.0","symbols":["zap.RedirectStdLog"],"verifierAdapter":"golang@1"}
go.mod
module example.com/zapredirect

go 1.22.0

require go.uber.org/zap v1.26.0

require go.uber.org/multierr v1.10.0 // indirect
go.sum
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
redirect.go
package zapredirect

import (
	"go.uber.org/zap"
)

// RedirectStandardLog redirects standard library log output to the provided zap logger at InfoLevel.
// It returns a restore function that reinstates original standard library log settings.
func RedirectStandardLog(logger *zap.Logger) func() {
	return zap.RedirectStdLog(logger)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify zap.RedirectStdLog in pkg:golang/go.uber.org/zap@v1.26.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.uber.org/zap@v1.26.0"
  ],
  "symbols": [
    "zap.RedirectStdLog"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"os"
	"strings"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"

	zapredirect "example.com/zapredirect"
)

type syncer struct {
	buf bytes.Buffer
}

func (s *syncer) Write(p []byte) (n int, err error) {
	return s.buf.Write(p)
}

func (s *syncer) Sync() error {
	return nil
}

func (s *syncer) Reset() {
	s.buf.Reset()
}

func (s *syncer) Lines() []string {
	raw := strings.TrimSpace(s.buf.String())
	if raw == "" {
		return nil
	}
	return strings.Split(raw, "\n")
}

func newLogger(level zapcore.LevelEnabler) (*zap.Logger, *syncer) {
	sink := &syncer{}
	encCfg := zapcore.EncoderConfig{
		MessageKey:     "msg",
		LevelKey:       "level",
		EncodeLevel:    zapcore.LowercaseLevelEncoder,
		EncodeDuration: zapcore.StringDurationEncoder,
	}
	core := zapcore.NewCore(
		zapcore.NewJSONEncoder(encCfg),
		sink,
		level,
	)
	return zap.New(core), sink
}

func parseLine(raw string) (map[string]interface{}, error) {
	var entry map[string]interface{}
	if err := json.Unmarshal([]byte(raw), &entry); err != nil {
		return nil, fmt.Errorf("failed to parse JSON log %q: %w", raw, err)
	}
	return entry, nil
}

func main() {
	// Assertion 1: zap.RedirectStdLog redirects standard library log output to the zap logger at InfoLevel
	logger, sink := newLogger(zapcore.DebugLevel)
	restore := zapredirect.RedirectStandardLog(logger)
	if restore == nil {
		fmt.Fprintln(os.Stderr, "FAIL: expected non-nil restore func from RedirectStdLog")
		os.Exit(1)
	}

	log.Print("contract test redirected message")

	lines := sink.Lines()
	if len(lines) != 1 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 1 log line in sink, got %d\n", len(lines))
		os.Exit(1)
	}
	entry, err := parseLine(lines[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: parsing log entry: %v\n", err)
		os.Exit(1)
	}
	if entry["level"] != "info" {
		fmt.Fprintf(os.Stderr, "FAIL: expected level 'info', got %v\n", entry["level"])
		os.Exit(1)
	}
	msg, _ := entry["msg"].(string)
	if !strings.Contains(msg, "contract test redirected message") {
		fmt.Fprintf(os.Stderr, "FAIL: expected msg to contain 'contract test redirected message', got %q\n", msg)
		os.Exit(1)
	}

	// Assertion 2: invoking the returned restore function reinstates original log routing
	restore()
	sink.Reset()

	var customBuf bytes.Buffer
	log.SetOutput(&customBuf)
	log.Print("message after restoration")

	if len(sink.Lines()) != 0 {
		fmt.Fprintf(os.Stderr, "FAIL: zap sink received log after restore: %s\n", sink.buf.String())
		os.Exit(1)
	}
	if !strings.Contains(customBuf.String(), "message after restoration") {
		fmt.Fprintf(os.Stderr, "FAIL: customBuf did not receive standard log output: %s\n", customBuf.String())
		os.Exit(1)
	}

	// Assertion 3: zap.RedirectStdLog output is filtered when the zap logger level threshold is configured above InfoLevel
	warnLogger, warnSink := newLogger(zapcore.WarnLevel)
	restoreWarn := zapredirect.RedirectStandardLog(warnLogger)
	defer restoreWarn()

	log.Print("info level message should be filtered out by WarnLevel threshold")
	if len(warnSink.Lines()) != 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 0 logs when zap logger is WarnLevel, got %d\n", len(warnSink.Lines()))
		os.Exit(1)
	}

	fmt.Println("PASS: zap.RedirectStdLog contract passed")
}

Origin Seeder

anonymous