CodeSampleX

示例

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

已验证示例 — golang go.uber.org/zap v1.26.0: NewStdLog. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: zap.NewStdLog returns a standard library…

sha256:5e6d7858000369d474c2eb9b36a18fd1132f7a493ba55b7059b36f7cd6415d62

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
verify zap.NewStdLog in pkg:golang/go.uber.org/zap@v1.26.0
包
符号
  • zap.NewStdLog
创建时间
2026-09-03T06:42:02Z

契约

  1. zap.NewStdLog returns a standard library *log.Logger that redirects output to the zap logger at InfoLevel
  2. zap.NewStdLog dispatches Print and Printf messages formatted as structured zap info entries
  3. zap.NewStdLog entries are filtered when the underlying zap core level threshold is above InfoLevel

文件

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

下载源代码构件 (tar.gz)

源代码

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.NewStdLog 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.NewStdLog

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:90b3ded462edd12f27ca89b98959a52ae8ab75e4585046c8f691a35d182c34ea","contract":["zap.NewStdLog returns a standard library *log.Logger that redirects output to the zap logger at InfoLevel","zap.NewStdLog dispatches Print and Printf messages formatted as structured zap info entries","zap.NewStdLog entries are filtered when the underlying zap core level threshold is above InfoLevel"],"goal":"verify zap.NewStdLog 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.NewStdLog"]},"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.NewStdLog"],"verifierAdapter":"golang@1"}
go.mod
module example.com/zapstdlog

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=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify zap.NewStdLog in pkg:golang/go.uber.org/zap@v1.26.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.uber.org/zap@v1.26.0"
  ],
  "symbols": [
    "zap.NewStdLog"
  ]
}
stdlog.go
package zapstdlog

import (
	"log"

	"go.uber.org/zap"
)

// NewStandardLogger creates a standard library *log.Logger that routes entries to the given zap.Logger at InfoLevel.
func NewStandardLogger(logger *zap.Logger) *log.Logger {
	return zap.NewStdLog(logger)
}
test/contract.go
package main

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

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

	zapstdlog "example.com/zapstdlog"
)

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.NewStdLog returns a standard library *log.Logger that redirects output to the zap logger at InfoLevel
	logger, sink := newLogger(zapcore.DebugLevel)
	stdLogger := zapstdlog.NewStandardLogger(logger)
	if stdLogger == nil {
		fmt.Fprintln(os.Stderr, "FAIL: expected non-nil *log.Logger from zap.NewStdLog")
		os.Exit(1)
	}

	stdLogger.Println("test standard library log output")
	lines := sink.Lines()
	if len(lines) != 1 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 1 log line, got %d\n", len(lines))
		os.Exit(1)
	}
	entry, err := parseLine(lines[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: %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, "test standard library log output") {
		fmt.Fprintf(os.Stderr, "FAIL: expected message to contain 'test standard library log output', got %v\n", msg)
		os.Exit(1)
	}

	// Assertion 2: zap.NewStdLog dispatches Print and Printf messages formatted as structured zap info entries
	sink.Reset()
	stdLogger.Print("print message without newline")
	stdLogger.Printf("formatted message: count=%d", 42)

	lines = sink.Lines()
	if len(lines) != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 2 log lines, got %d\n", len(lines))
		os.Exit(1)
	}
	entry1, err := parseLine(lines[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: %v\n", err)
		os.Exit(1)
	}
	if entry1["level"] != "info" || !strings.Contains(entry1["msg"].(string), "print message without newline") {
		fmt.Fprintf(os.Stderr, "FAIL: unexpected entry 1: %v\n", entry1)
		os.Exit(1)
	}
	entry2, err := parseLine(lines[1])
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: %v\n", err)
		os.Exit(1)
	}
	if entry2["level"] != "info" || !strings.Contains(entry2["msg"].(string), "formatted message: count=42") {
		fmt.Fprintf(os.Stderr, "FAIL: unexpected entry 2: %v\n", entry2)
		os.Exit(1)
	}

	// Assertion 3: zap.NewStdLog entries are filtered when the underlying zap core level threshold is above InfoLevel
	warnLogger, warnSink := newLogger(zapcore.WarnLevel)
	warnStdLogger := zapstdlog.NewStandardLogger(warnLogger)
	warnStdLogger.Println("this info level message should be filtered out")

	if len(warnSink.Lines()) != 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 0 log lines when core is at WarnLevel, got %d\n", len(warnSink.Lines()))
		os.Exit(1)
	}

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

原始种子者

匿名