use orion_error::op_context;
fn main() {
env_logger::init();
println!("=== OperationContext 日志记录示例 ===\n");
process_order("order_123", 100.0, "customer_456");
println!("\n=== 成功场景下的日志价值 ===");
successful_operation();
}
fn process_order(order_id: &str, amount: f64, customer_id: &str) {
let mut ctx = op_context!("process_order").with_auto_log();
ctx.record_field("order_id", order_id);
{
let mut scope = ctx.scoped_success();
scope.record_field("order_id", order_id);
scope.record_field("amount", amount.to_string());
scope.record_field("customer_id", customer_id);
scope.info("开始处理订单");
let validation_result = validate_order(amount);
scope.record_field("validation_result", validation_result.to_string());
scope.debug("订单验证完成");
if validation_result {
scope.info("订单处理成功");
println!("订单 {order_id} 处理成功");
} else {
scope.error("订单验证失败");
scope.mark_failure();
println!("订单 {order_id} 验证失败");
}
}
}
fn validate_order(amount: f64) -> bool {
amount > 0.0 && amount <= 10000.0
}
fn successful_operation() {
let mut ctx = op_context!("data_processing");
{
let mut scope = ctx.scoped_success();
scope.record_field("batch_size", "1000");
scope.record_field("processor", "worker_1");
scope.record_field("start_time", "2024-01-01T10:00:00Z");
scope.info("开始数据处理");
for i in 0..5 {
scope.record_field("current_item", i.to_string());
scope.debug("处理数据项");
std::thread::sleep(std::time::Duration::from_millis(100));
}
scope.record_field("end_time", "2024-01-01T10:05:00Z");
scope.record_field("items_processed", "5");
scope.info("数据处理完成");
}
println!("数据处理操作完成,记录了完整的上下文信息");
}