|
1 | 1 | /*
|
2 |
| - * Copyright 2018-2021 the original author or authors. |
| 2 | + * Copyright 2018-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
22 | 22 | import java.time.Duration;
|
23 | 23 | import java.util.Collection;
|
24 | 24 | import java.util.Map;
|
| 25 | +import java.util.function.Function; |
25 | 26 |
|
| 27 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
26 | 28 | import org.apache.kafka.clients.producer.ProducerConfig;
|
| 29 | +import org.apache.kafka.clients.producer.ProducerRecord; |
27 | 30 |
|
28 | 31 | import org.springframework.messaging.Message;
|
| 32 | +import org.springframework.util.Assert; |
29 | 33 | import org.springframework.util.ClassUtils;
|
30 | 34 |
|
31 | 35 | /**
|
|
38 | 42 | */
|
39 | 43 | public final class KafkaUtils {
|
40 | 44 |
|
| 45 | + private static final ThreadLocal<Boolean> LOG_METADATA_ONLY = new ThreadLocal<>(); |
| 46 | + |
| 47 | + private static Function<ProducerRecord<?, ?>, String> prFormatter = rec -> rec.toString(); |
| 48 | + |
| 49 | + private static Function<ConsumerRecord<?, ?>, String> crFormatter = |
| 50 | + rec -> rec.topic() + "-" + rec.partition() + "@" + rec.offset(); |
| 51 | + |
41 | 52 | /**
|
42 | 53 | * True if micrometer is on the class path.
|
43 | 54 | */
|
@@ -134,6 +145,84 @@ else if (dt instanceof String) {
|
134 | 145 | min));
|
135 | 146 | }
|
136 | 147 |
|
| 148 | + /** |
| 149 | + * Set to true to only log record metadata. |
| 150 | + * @param onlyMeta true to only log record metadata. |
| 151 | + * @since 2.7.12 |
| 152 | + * @see #recordToString(ConsumerRecord) |
| 153 | + */ |
| 154 | + public static void setLogOnlyMetadata(boolean onlyMeta) { |
| 155 | + LOG_METADATA_ONLY.set(onlyMeta); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Return the {@link ConsumerRecord} as a String; either {@code toString()} or |
| 160 | + * {@code topic-partition@offset}. |
| 161 | + * @param record the record. |
| 162 | + * @return the rendered record. |
| 163 | + * @since 2.7.12 |
| 164 | + * @see #setLogOnlyMetadata(boolean) |
| 165 | + */ |
| 166 | + public static String recordToString(ConsumerRecord<?, ?> record) { |
| 167 | + return recordToString(record, Boolean.TRUE.equals(LOG_METADATA_ONLY.get())); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Return the {@link ConsumerRecord} as a String; either {@code toString()} or |
| 172 | + * {@code topic-partition@offset}. |
| 173 | + * @param record the record. |
| 174 | + * @param meta true to log just the metadata. |
| 175 | + * @return the rendered record. |
| 176 | + * @since 2.7.12 |
| 177 | + */ |
| 178 | + public static String recordToString(ConsumerRecord<?, ?> record, boolean meta) { |
| 179 | + if (meta) { |
| 180 | + return crFormatter.apply(record); |
| 181 | + } |
| 182 | + else { |
| 183 | + return record.toString(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Set a formatter for logging {@link ConsumererRecord}s. |
| 189 | + * @param formatter a function to format the record as a String |
| 190 | + * @since 2.7.11 |
| 191 | + */ |
| 192 | + public static void setConsumerRecordFormatter(Function<ConsumerRecord<?, ?>, String> formatter) { |
| 193 | + Assert.notNull(formatter, "'formatter' cannot be null"); |
| 194 | + crFormatter = formatter; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Set a formatter for logging {@link ProducerRecord}s. |
| 199 | + * @param formatter a function to format the record as a String |
| 200 | + * @since 2.7.11 |
| 201 | + */ |
| 202 | + public static void setProducerRecordFormatter(Function<ProducerRecord<?, ?>, String> formatter) { |
| 203 | + Assert.notNull(formatter, "'formatter' cannot be null"); |
| 204 | + prFormatter = formatter; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Format the {@link ConsumerRecord} for logging; default |
| 209 | + * {@code topic-partition@offset}. |
| 210 | + * @param record the record to format. |
| 211 | + * @return the formatted String. |
| 212 | + */ |
| 213 | + public static String format(ConsumerRecord<?, ?> record) { |
| 214 | + return crFormatter.apply(record); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Format the {@link ProducerRecord} for logging; default |
| 219 | + * {@link ProducerRecord}{@link #toString()}. |
| 220 | + * @param record the record to format. |
| 221 | + * @return the formatted String. |
| 222 | + */ |
| 223 | + public static String format(ProducerRecord<?, ?> record) { |
| 224 | + return prFormatter.apply(record); |
| 225 | + } |
137 | 226 |
|
138 | 227 | private KafkaUtils() {
|
139 | 228 | }
|
|
0 commit comments