CodeSampleX

Sample

github.com/sirupsen/logrus v1.6.0: JSONFormatter

Verified sample for golang github.com/sirupsen/logrus v1.6.0: JSONFormatter. The contract ran on go 1.26 · linux debian/x64 · docker and passed.

sha256:e5e9fc6f95de6da96d4a3a4dbe8d0add7be26d841342154598e78dabf44581c7

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-05

Case

HOW
Goal
verify logrus.JSONFormatter in pkg:golang/github.com/sirupsen/logrus@v1.6.0
Packages
Symbols
  • logrus.JSONFormatter
Created
2026-09-05T03:01:21Z

Contract

  1. logrus.JSONFormatter formats log entries into JSON containing level, msg, time, and custom fields
  2. logrus.JSONFormatter omits the timestamp field when DisableTimestamp is true
  3. logrus.JSONFormatter formats timestamps according to custom TimestampFormat
  4. logrus.JSONFormatter nests custom fields under DataKey when DataKey is configured
  5. logrus.JSONFormatter renames default field keys using FieldMap

Files

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.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 logrus.JSONFormatter in pkg:golang/github.com/sirupsen/logrus@v1.6.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/sirupsen/logrus@v1.6.0
Demonstrate these symbols/APIs:
  - logrus.JSONFormatter

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:5fe84abf2a870a8086520b22f1127127e0508335d070aa5965d63cada1e61104","contract":["logrus.JSONFormatter formats log entries into JSON containing level, msg, time, and custom fields","logrus.JSONFormatter omits the timestamp field when DisableTimestamp is true","logrus.JSONFormatter formats timestamps according to custom TimestampFormat","logrus.JSONFormatter nests custom fields under DataKey when DataKey is configured","logrus.JSONFormatter renames default field keys using FieldMap"],"goal":"verify logrus.JSONFormatter in pkg:golang/github.com/sirupsen/logrus@v1.6.0","kind":"HOW","packages":["pkg:golang/github.com/sirupsen/logrus@v1.6.0"],"schemaVersion":1,"symbols":["logrus.JSONFormatter"]},"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/sirupsen/logrus@v1.6.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/sirupsen/logrus@v1.6.0","symbols":["logrus.JSONFormatter"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/sirupsen/logrus v1.6.0

require (
	github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
	golang.org/x/sys v0.13.0 // indirect
)
go.sum
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBBo=
github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
main.go
package main

import (
	"os"

	"github.com/sirupsen/logrus"
)

func main() {
	logger := logrus.New()
	logger.SetOutput(os.Stdout)
	logger.SetFormatter(&logrus.JSONFormatter{
		TimestampFormat: "2006-01-02T15:04:05Z07:00",
		FieldMap: logrus.FieldMap{
			logrus.FieldKeyMsg: "message",
		},
	})

	logger.WithFields(logrus.Fields{
		"service": "sample-service",
		"status":  "healthy",
	}).Info("application status update")
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify logrus.JSONFormatter in pkg:golang/github.com/sirupsen/logrus@v1.6.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/sirupsen/logrus@v1.6.0"
  ],
  "symbols": [
    "logrus.JSONFormatter"
  ]
}
test/contract.go
package main

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

	"github.com/sirupsen/logrus"
)

func main() {
	// Assertion 1: logrus.JSONFormatter formats log entries into JSON containing level, msg, time, and custom fields
	{
		var buf bytes.Buffer
		log := logrus.New()
		log.SetOutput(&buf)
		log.SetFormatter(&logrus.JSONFormatter{})

		log.WithField("animal", "walrus").Info("hello world")

		var parsed map[string]interface{}
		if err := json.Unmarshal(buf.Bytes(), &parsed); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: json unmarshal: %v\n", err)
			os.Exit(1)
		}
		if parsed["msg"] != "hello world" || parsed["level"] != "info" || parsed["animal"] != "walrus" {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: unexpected content: %+v\n", parsed)
			os.Exit(1)
		}
		if _, ok := parsed["time"]; !ok {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: time field missing\n")
			os.Exit(1)
		}
	}

	// Assertion 2: logrus.JSONFormatter omits the timestamp field when DisableTimestamp is true
	{
		formatter := &logrus.JSONFormatter{
			DisableTimestamp: true,
		}
		entry := &logrus.Entry{
			Logger:  logrus.New(),
			Data:    logrus.Fields{"status": "ok"},
			Time:    time.Now(),
			Level:   logrus.WarnLevel,
			Message: "no timestamp",
		}
		data, err := formatter.Format(entry)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: %v\n", err)
			os.Exit(1)
		}
		var parsed map[string]interface{}
		if err := json.Unmarshal(data, &parsed); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: json unmarshal: %v\n", err)
			os.Exit(1)
		}
		if _, ok := parsed["time"]; ok {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: time field should be omitted\n")
			os.Exit(1)
		}
		if parsed["level"] != "warning" || parsed["msg"] != "no timestamp" || parsed["status"] != "ok" {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: unexpected content: %+v\n", parsed)
			os.Exit(1)
		}
	}

	// Assertion 3: logrus.JSONFormatter formats timestamps according to custom TimestampFormat
	{
		customFormat := "2006-01-02 15:04:05"
		formatter := &logrus.JSONFormatter{
			TimestampFormat: customFormat,
		}
		testTime := time.Date(2026, 9, 5, 12, 34, 56, 0, time.UTC)
		entry := &logrus.Entry{
			Logger:  logrus.New(),
			Data:    logrus.Fields{},
			Time:    testTime,
			Level:   logrus.InfoLevel,
			Message: "custom time",
		}
		data, err := formatter.Format(entry)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: %v\n", err)
			os.Exit(1)
		}
		var parsed map[string]interface{}
		if err := json.Unmarshal(data, &parsed); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: json unmarshal: %v\n", err)
			os.Exit(1)
		}
		expectedTime := testTime.Format(customFormat)
		if parsed["time"] != expectedTime {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: expected time %q, got %q\n", expectedTime, parsed["time"])
			os.Exit(1)
		}
	}

	// Assertion 4: logrus.JSONFormatter nests custom fields under DataKey when DataKey is configured
	{
		formatter := &logrus.JSONFormatter{
			DataKey:          "payload",
			DisableTimestamp: true,
		}
		entry := &logrus.Entry{
			Logger: logrus.New(),
			Data: logrus.Fields{
				"request_id": "req-123",
				"retries":    3,
			},
			Level:   logrus.ErrorLevel,
			Message: "operation failed",
		}
		data, err := formatter.Format(entry)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: %v\n", err)
			os.Exit(1)
		}
		var parsed map[string]interface{}
		if err := json.Unmarshal(data, &parsed); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: json unmarshal: %v\n", err)
			os.Exit(1)
		}
		payload, ok := parsed["payload"].(map[string]interface{})
		if !ok {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: payload key missing or not a map: %+v\n", parsed)
			os.Exit(1)
		}
		if payload["request_id"] != "req-123" || payload["retries"] != float64(3) {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: unexpected nested payload: %+v\n", payload)
			os.Exit(1)
		}
		if parsed["level"] != "error" || parsed["msg"] != "operation failed" {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: root fields mismatch: %+v\n", parsed)
			os.Exit(1)
		}
	}

	// Assertion 5: logrus.JSONFormatter renames default field keys using FieldMap
	{
		formatter := &logrus.JSONFormatter{
			DisableTimestamp: true,
			FieldMap: logrus.FieldMap{
				logrus.FieldKeyTime:  "@timestamp",
				logrus.FieldKeyLevel: "@level",
				logrus.FieldKeyMsg:   "@message",
			},
		}
		entry := &logrus.Entry{
			Logger:  logrus.New(),
			Data:    logrus.Fields{"count": 10},
			Level:   logrus.InfoLevel,
			Message: "field mapping test",
		}
		data, err := formatter.Format(entry)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: %v\n", err)
			os.Exit(1)
		}
		var parsed map[string]interface{}
		if err := json.Unmarshal(data, &parsed); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: json unmarshal: %v\n", err)
			os.Exit(1)
		}
		if parsed["@message"] != "field mapping test" || parsed["@level"] != "info" {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: remapped keys mismatch: %+v\n", parsed)
			os.Exit(1)
		}
		if _, ok := parsed["msg"]; ok {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: old key 'msg' still present\n")
			os.Exit(1)
		}
		if _, ok := parsed["level"]; ok {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: old key 'level' still present\n")
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed successfully.")
}

Origin Seeder

anonymous