Skip to main content

confluent_kafka/common/utils/
log_macros.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Logging macros that prepend a [`LogContext`](super::internals::LogContext) prefix.
16//!
17//! These macros wrap the standard [`log`] crate macros and automatically
18//! prepend the `LogContext` prefix to every log message, matching Java's
19//! `LogContext.logger()` behavior.
20//!
21//! The `module_path!()` inside `log::*!` expands at the **call site**, so
22//! `RUST_LOG` filtering works by the caller's module path.
23//!
24//! # Usage
25//!
26//! ```ignore
27//! use confluent_kafka::common::utils::internals::LogContext;
28//!
29//! let ctx = LogContext::new("[Producer clientId=my-producer] ");
30//! kafka_debug!(ctx, "Starting Kafka producer I/O task.");
31//! kafka_warn!(ctx, "Error connecting to node {}: {}", node_id, err);
32//! ```
33
34/// Log at ERROR level with a `LogContext` prefix.
35#[macro_export]
36macro_rules! kafka_error {
37    ($ctx:expr, $($arg:tt)*) => {
38        log::error!("{}{}", $ctx.prefix(), format_args!($($arg)*))
39    };
40}
41
42/// Log at WARN level with a `LogContext` prefix.
43#[macro_export]
44macro_rules! kafka_warn {
45    ($ctx:expr, $($arg:tt)*) => {
46        log::warn!("{}{}", $ctx.prefix(), format_args!($($arg)*))
47    };
48}
49
50/// Log at INFO level with a `LogContext` prefix.
51#[macro_export]
52macro_rules! kafka_info {
53    ($ctx:expr, $($arg:tt)*) => {
54        log::info!("{}{}", $ctx.prefix(), format_args!($($arg)*))
55    };
56}
57
58/// Log at DEBUG level with a `LogContext` prefix.
59#[macro_export]
60macro_rules! kafka_debug {
61    ($ctx:expr, $($arg:tt)*) => {
62        log::debug!("{}{}", $ctx.prefix(), format_args!($($arg)*))
63    };
64}
65
66/// Log at TRACE level with a `LogContext` prefix.
67#[macro_export]
68macro_rules! kafka_trace {
69    ($ctx:expr, $($arg:tt)*) => {
70        log::trace!("{}{}", $ctx.prefix(), format_args!($($arg)*))
71    };
72}