示例
go.uber.org/zap v1.28.0: zapcore.NewJSONEncoder
已验证示例 — golang go.uber.org/zap v1.28.0: zapcore.NewJSONEncoder. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: CreateJSONEncoder constructs a…
sha256:33f669a7104e8993907f0bf41fe826581894e15e7c3614a3e8e7cac8a764ef9a
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
linux x64 go go
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| 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-09-04 |
案例
HOW- 目标
- verify go.uber.org/zap/zapcore.NewJSONEncoder in pkg:golang/go.uber.org/zap@v1.28.0
- 符号
-
- go.uber.org/zap/zapcore.NewJSONEncoder
- 创建时间
- 2026-09-04T18:18:14Z
契约
- CreateJSONEncoder constructs a zapcore.Encoder using NewJSONEncoder that formats log entries with custom EncoderConfig settings
- EncodeLogEntry formats log metadata and structured fields into a valid JSON string with configured timestamp and level keys
- NewJSONLogger creates a zap.Logger configured with NewJSONEncoder and writes structured JSON logs to the destination buffer
文件
- PROMPT.md
- csx.json
- go.mod
- go.sum
- sample.go
- spec.json
- test/contract.go
源代码
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 go.uber.org/zap/zapcore.NewJSONEncoder in pkg:golang/go.uber.org/zap@v1.28.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/go.uber.org/zap@v1.28.0
Demonstrate these symbols/APIs:
- go.uber.org/zap/zapcore.NewJSONEncoder
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.
{"case":{"caseId":"case:sha256:93e5d16489039f392c0e638d743320a2529dfe014984fa2909a1548a23be09b7","contract":["CreateJSONEncoder constructs a zapcore.Encoder using NewJSONEncoder that formats log entries with custom EncoderConfig settings","EncodeLogEntry formats log metadata and structured fields into a valid JSON string with configured timestamp and level keys","NewJSONLogger creates a zap.Logger configured with NewJSONEncoder and writes structured JSON logs to the destination buffer"],"goal":"verify go.uber.org/zap/zapcore.NewJSONEncoder in pkg:golang/go.uber.org/zap@v1.28.0","kind":"HOW","packages":["pkg:golang/go.uber.org/zap@v1.28.0"],"schemaVersion":1,"symbols":["go.uber.org/zap/zapcore.NewJSONEncoder"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","ecosystem":"golang","language":"go","os":"linux","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/go.uber.org/zap@v1.28.0"],"schemaVersion":1,"subject":"pkg:golang/go.uber.org/zap@v1.28.0","symbols":["go.uber.org/zap/zapcore.NewJSONEncoder"],"verifierAdapter":"golang@1"}
module example.com/sample
go 1.22.0
require go.uber.org/zap v1.28.0
require go.uber.org/multierr v1.10.0 // indirect
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
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.28.0 h1:IZzaP1Fv73/T/pBMLk4VutPl36uNC+OSUh3JLG3FIjo=
go.uber.org/zap v1.28.0/go.mod h1:rDLpOi171uODNm/mxFcuYWxDsqWSAVkFdX4XojSKg/Q=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
package sample
import (
"bytes"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// DefaultJSONEncoderConfig returns a standard EncoderConfig for JSON formatting.
func DefaultJSONEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}
// CreateJSONEncoder creates a zapcore.Encoder configured with JSON formatting via NewJSONEncoder.
func CreateJSONEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
return zapcore.NewJSONEncoder(cfg)
}
// EncodeLogEntry encodes a single log entry and fields directly using the JSON encoder.
func EncodeLogEntry(enc zapcore.Encoder, level zapcore.Level, msg string, t time.Time, fields ...zapcore.Field) (string, error) {
entry := zapcore.Entry{
Level: level,
Time: t,
Message: msg,
LoggerName: "json-test",
}
buf, err := enc.EncodeEntry(entry, fields)
if err != nil {
return "", err
}
defer buf.Free()
return buf.String(), nil
}
// NewJSONLogger creates a zap.Logger configured with NewJSONEncoder writing to the provided buffer.
func NewJSONLogger(cfg zapcore.EncoderConfig, buf *bytes.Buffer, level zapcore.Level) *zap.Logger {
encoder := zapcore.NewJSONEncoder(cfg)
core := zapcore.NewCore(encoder, zapcore.AddSync(buf), level)
return zap.New(core)
}
{
"schemaVersion": 1,
"goal": "verify go.uber.org/zap/zapcore.NewJSONEncoder in pkg:golang/go.uber.org/zap@v1.28.0",
"kind": "HOW",
"packages": [
"pkg:golang/go.uber.org/zap@v1.28.0"
],
"symbols": [
"go.uber.org/zap/zapcore.NewJSONEncoder"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@10.9.8",
"runtime": "node@22.23.2"
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"time"
"example.com/sample"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type defaultLogEntry struct {
Level string `json:"level"`
Ts string `json:"ts"`
Logger string `json:"logger"`
Msg string `json:"msg"`
Service string `json:"service,omitempty"`
Attempt int `json:"attempt,omitempty"`
}
type customLogEntry struct {
Severity string `json:"severity"`
Message string `json:"message"`
Code int `json:"code"`
}
func main() {
// Test 1: Verify direct encoding using NewJSONEncoder
cfg := sample.DefaultJSONEncoderConfig()
enc := sample.CreateJSONEncoder(cfg)
if enc == nil {
fmt.Fprintf(os.Stderr, "expected non-nil encoder\n")
os.Exit(1)
}
fixedTime := time.Date(2026, 9, 4, 12, 0, 0, 0, time.UTC)
output, err := sample.EncodeLogEntry(
enc,
zapcore.InfoLevel,
"direct encode test",
fixedTime,
zap.String("service", "auth"),
zap.Int("attempt", 1),
)
if err != nil {
fmt.Fprintf(os.Stderr, "EncodeLogEntry failed: %v\n", err)
os.Exit(1)
}
var entry defaultLogEntry
if err := json.Unmarshal([]byte(output), &entry); err != nil {
fmt.Fprintf(os.Stderr, "failed to parse JSON from EncodeLogEntry: %v, raw: %s\n", err, output)
os.Exit(1)
}
if entry.Level != "info" || entry.Msg != "direct encode test" || entry.Service != "auth" || entry.Attempt != 1 {
fmt.Fprintf(os.Stderr, "unexpected field values in encoded entry: %+v\n", entry)
os.Exit(1)
}
// Test 2: Verify NewJSONEncoder integrated into zap.Logger via zapcore.NewCore
var buf bytes.Buffer
logger := sample.NewJSONLogger(cfg, &buf, zapcore.InfoLevel)
if logger == nil {
fmt.Fprintf(os.Stderr, "expected non-nil logger\n")
os.Exit(1)
}
logger.Info("pipeline operational", zap.String("service", "indexer"), zap.Int("attempt", 3))
var loggerEntry defaultLogEntry
if err := json.Unmarshal(buf.Bytes(), &loggerEntry); err != nil {
fmt.Fprintf(os.Stderr, "failed to parse JSON from logger: %v, raw: %s\n", err, buf.String())
os.Exit(1)
}
if loggerEntry.Level != "info" || loggerEntry.Msg != "pipeline operational" || loggerEntry.Service != "indexer" || loggerEntry.Attempt != 3 {
fmt.Fprintf(os.Stderr, "unexpected field values from logger: %+v\n", loggerEntry)
os.Exit(1)
}
// Test 3: Verify custom EncoderConfig key mapping
buf.Reset()
customCfg := zapcore.EncoderConfig{
LevelKey: "severity",
MessageKey: "message",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
}
customLogger := sample.NewJSONLogger(customCfg, &buf, zapcore.WarnLevel)
customLogger.Warn("rate limit exceeded", zap.Int("code", 429))
var customEntry customLogEntry
if err := json.Unmarshal(buf.Bytes(), &customEntry); err != nil {
fmt.Fprintf(os.Stderr, "failed to parse custom JSON: %v, raw: %s\n", err, buf.String())
os.Exit(1)
}
if customEntry.Severity != "warn" || customEntry.Message != "rate limit exceeded" || customEntry.Code != 429 {
fmt.Fprintf(os.Stderr, "unexpected custom log values: %+v\n", customEntry)
os.Exit(1)
}
fmt.Println("All contracts passed successfully.")
}
原始种子者
匿名