CodeSampleX

示例

log 0.4.34: log::Log, log::set_logger, log::set_max_level

已验证示例 — cargo log 0.4.34: log::Log, log::set_logger, log::set_max_level. contract 在 rust 1 · linux alpine/x64 · docker 上运行并通过.

sha256:80742d5ac941f77b2399369a79e8846b6f7e04b84fd22729a54ccec98aa069b6

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

执行证据

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

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux x64 cargo

验证运行环境

环境 契约 阶段 运行日期
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-10-16

案例

HOW
目标
verify pkg:cargo/log@0.4.34
包
符号
  • log::Log
  • log::set_logger
  • log::set_max_level
  • log::max_level
  • log::logger
  • log::Level
  • log::LevelFilter
  • log::Metadata
  • log::Record
  • log::log_enabled
  • log::log
  • log::error
  • log::warn
  • log::info
  • log::debug
  • log::trace
创建时间
2026-10-16T18:41:36Z

契约

  1. Registers a static `log::Log` implementation via `log::set_logger` (available with default features without `alloc`) and verifies a second registration returns `Err(SetLoggerError)`.
  2. Controls active filtering via `log::set_max_level` and `log::max_level`, emitting structured records with explicit targets using `error!`, `warn!`, `info!`, `debug!`, `trace!`, and `log!`.
  3. Verifies `log_enabled!` reflects both `LevelFilter` and `Log::enabled`, and flushes the global logger via `log::logger().flush()`.

文件

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/main.rs

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

源代码

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "log"
version = "0.4.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6"

[[package]]
name = "sample-log"
version = "0.1.0"
dependencies = [
 "log",
]
Cargo.toml
[package]
name = "sample-log"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
log = "=0.4.34"
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:cargo/log@0.4.34
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/log@0.4.34

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:5afc0acc314671a7a26b7679b9289cda546a73c7bf42e8964007e7824253a9e3","contract":["Registers a static `log::Log` implementation via `log::set_logger` (available with default features without `alloc`) and verifies a second registration returns `Err(SetLoggerError)`.","Controls active filtering via `log::set_max_level` and `log::max_level`, emitting structured records with explicit targets using `error!`, `warn!`, `info!`, `debug!`, `trace!`, and `log!`.","Verifies `log_enabled!` reflects both `LevelFilter` and `Log::enabled`, and flushes the global logger via `log::logger().flush()`."],"goal":"verify pkg:cargo/log@0.4.34","kind":"HOW","packages":["pkg:cargo/log@0.4.34"],"schemaVersion":1,"symbols":["log::Log","log::set_logger","log::set_max_level","log::max_level","log::logger","log::Level","log::LevelFilter","log::Metadata","log::Record","log::log_enabled","log::log","log::error","log::warn","log::info","log::debug","log::trace"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/log@0.4.34"],"schemaVersion":1,"subject":"pkg:cargo/log@0.4.34","symbols":["log::Log","log::set_logger","log::set_max_level","log::max_level","log::logger","log::Level","log::LevelFilter","log::Metadata","log::Record","log::log_enabled","log::log","log::error","log::warn","log::info","log::debug","log::trace"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:cargo/log@0.4.34",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/log@0.4.34"
  ]
}
src/main.rs
use log::{debug, error, info, log, log_enabled, trace, warn, Level, LevelFilter, Log, Metadata, Record};
use std::sync::Mutex;

static LOGGER: MemoryLogger = MemoryLogger {
    entries: Mutex::new(Vec::new()),
    flushed: Mutex::new(0),
};

struct MemoryLogger {
    entries: Mutex<Vec<(Level, String, String)>>,
    flushed: Mutex<usize>,
}

impl Log for MemoryLogger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        metadata.level() <= Level::Debug
    }

    fn log(&self, record: &Record) {
        if self.enabled(record.metadata()) {
            let mut guard = self.entries.lock().unwrap();
            guard.push((
                record.level(),
                record.target().to_string(),
                record.args().to_string(),
            ));
        }
    }

    fn flush(&self) {
        let mut guard = self.flushed.lock().unwrap();
        *guard += 1;
    }
}

fn main() {
    log::set_logger(&LOGGER).expect("set_logger should succeed once");
    assert!(log::set_logger(&LOGGER).is_err());

    log::set_max_level(LevelFilter::Info);
    assert_eq!(log::max_level(), LevelFilter::Info);

    assert!(log_enabled!(Level::Error));
    assert!(log_enabled!(Level::Warn));
    assert!(log_enabled!(Level::Info));
    assert!(!log_enabled!(Level::Debug));
    assert!(!log_enabled!(Level::Trace));

    error!(target: "app::auth", "login failed: code={}", 401);
    warn!(target: "app::cache", "cache miss");
    info!(target: "app::core", "service started");
    debug!(target: "app::core", "filtered out at Info filter");
    trace!(target: "app::core", "filtered out by both filter and enabled()");

    log::set_max_level(LevelFilter::Trace);
    assert_eq!(log::max_level(), LevelFilter::Trace);
    assert!(log_enabled!(Level::Debug));
    assert!(!log_enabled!(Level::Trace));

    log!(target: "app::custom", Level::Debug, "custom debug event {}", 7);
    trace!(target: "app::custom", "still rejected by MemoryLogger::enabled");

    log::logger().flush();

    let recorded = LOGGER.entries.lock().unwrap();
    assert_eq!(recorded.len(), 4);
    assert_eq!(
        recorded[0],
        (Level::Error, "app::auth".to_string(), "login failed: code=401".to_string())
    );
    assert_eq!(
        recorded[1],
        (Level::Warn, "app::cache".to_string(), "cache miss".to_string())
    );
    assert_eq!(
        recorded[2],
        (Level::Info, "app::core".to_string(), "service started".to_string())
    );
    assert_eq!(
        recorded[3],
        (Level::Debug, "app::custom".to_string(), "custom debug event 7".to_string())
    );

    assert_eq!(*LOGGER.flushed.lock().unwrap(), 1);
}

原始种子者

匿名