Skip to content

Commit 739ebb1

Browse files
committed
refactor(fmt): Pull out logger's builder methods
1 parent 3acb571 commit 739ebb1

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

src/fmt/mod.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,78 @@ pub(crate) struct DefaultFormat {
292292
pub(crate) kv_format: Option<Box<KvFormatFn>>,
293293
}
294294

295+
impl DefaultFormat {
296+
/// Whether or not to write the level in the default format.
297+
pub(crate) fn level(&mut self, write: bool) -> &mut Self {
298+
self.level = write;
299+
self
300+
}
301+
302+
/// Whether or not to write the source file path in the default format.
303+
pub(crate) fn file(&mut self, write: bool) -> &mut Self {
304+
self.source_file = write;
305+
self
306+
}
307+
308+
/// Whether or not to write the source line number path in the default format.
309+
///
310+
/// Only has effect if `format_file` is also enabled
311+
pub(crate) fn line_number(&mut self, write: bool) -> &mut Self {
312+
self.source_line_number = write;
313+
self
314+
}
315+
316+
/// Whether or not to write the module path in the default format.
317+
pub(crate) fn module_path(&mut self, write: bool) -> &mut Self {
318+
self.module_path = write;
319+
self
320+
}
321+
322+
/// Whether or not to write the target in the default format.
323+
pub(crate) fn target(&mut self, write: bool) -> &mut Self {
324+
self.target = write;
325+
self
326+
}
327+
328+
/// Configures the amount of spaces to use to indent multiline log records.
329+
/// A value of `None` disables any kind of indentation.
330+
pub(crate) fn indent(&mut self, indent: Option<usize>) -> &mut Self {
331+
self.indent = indent;
332+
self
333+
}
334+
335+
/// Configures if timestamp should be included and in what precision.
336+
pub(crate) fn timestamp(&mut self, timestamp: Option<TimestampPrecision>) -> &mut Self {
337+
self.timestamp = timestamp;
338+
self
339+
}
340+
341+
/// Configures the end of line suffix.
342+
pub(crate) fn suffix(&mut self, suffix: &'static str) -> &mut Self {
343+
self.suffix = suffix;
344+
self
345+
}
346+
347+
/// Set the format for structured key/value pairs in the log record
348+
///
349+
/// With the default format, this function is called for each record and should format
350+
/// the structured key-value pairs as returned by [`log::Record::key_values`].
351+
///
352+
/// The format function is expected to output the string directly to the `Formatter` so that
353+
/// implementations can use the [`std::fmt`] macros, similar to the main format function.
354+
///
355+
/// The default format uses a space to separate each key-value pair, with an "=" between
356+
/// the key and value.
357+
#[cfg(feature = "kv")]
358+
pub(crate) fn key_values<F>(&mut self, format: F) -> &mut Self
359+
where
360+
F: Fn(&mut Formatter, &dyn log::kv::Source) -> io::Result<()> + Sync + Send + 'static,
361+
{
362+
self.kv_format = Some(Box::new(format));
363+
self
364+
}
365+
}
366+
295367
impl Default for DefaultFormat {
296368
fn default() -> Self {
297369
Self {

src/logger.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,21 @@ impl Builder {
258258

259259
/// Whether or not to write the level in the default format.
260260
pub fn format_level(&mut self, write: bool) -> &mut Self {
261-
self.format.default_format.level = write;
261+
self.format.default_format.level(write);
262262
self
263263
}
264264

265265
/// Whether or not to write the source file path in the default format.
266266
pub fn format_file(&mut self, write: bool) -> &mut Self {
267-
self.format.default_format.source_file = write;
267+
self.format.default_format.file(write);
268268
self
269269
}
270270

271271
/// Whether or not to write the source line number path in the default format.
272272
///
273273
/// Only has effect if `format_file` is also enabled
274274
pub fn format_line_number(&mut self, write: bool) -> &mut Self {
275-
self.format.default_format.source_line_number = write;
275+
self.format.default_format.line_number(write);
276276
self
277277
}
278278

@@ -287,26 +287,26 @@ impl Builder {
287287

288288
/// Whether or not to write the module path in the default format.
289289
pub fn format_module_path(&mut self, write: bool) -> &mut Self {
290-
self.format.default_format.module_path = write;
290+
self.format.default_format.module_path(write);
291291
self
292292
}
293293

294294
/// Whether or not to write the target in the default format.
295295
pub fn format_target(&mut self, write: bool) -> &mut Self {
296-
self.format.default_format.target = write;
296+
self.format.default_format.target(write);
297297
self
298298
}
299299

300300
/// Configures the amount of spaces to use to indent multiline log records.
301301
/// A value of `None` disables any kind of indentation.
302302
pub fn format_indent(&mut self, indent: Option<usize>) -> &mut Self {
303-
self.format.default_format.indent = indent;
303+
self.format.default_format.indent(indent);
304304
self
305305
}
306306

307307
/// Configures if timestamp should be included and in what precision.
308308
pub fn format_timestamp(&mut self, timestamp: Option<fmt::TimestampPrecision>) -> &mut Self {
309-
self.format.default_format.timestamp = timestamp;
309+
self.format.default_format.timestamp(timestamp);
310310
self
311311
}
312312

@@ -332,7 +332,7 @@ impl Builder {
332332

333333
/// Configures the end of line suffix.
334334
pub fn format_suffix(&mut self, suffix: &'static str) -> &mut Self {
335-
self.format.default_format.suffix = suffix;
335+
self.format.default_format.suffix(suffix);
336336
self
337337
}
338338

@@ -351,7 +351,7 @@ impl Builder {
351351
where
352352
F: Fn(&mut Formatter, &dyn log::kv::Source) -> io::Result<()> + Sync + Send + 'static,
353353
{
354-
self.format.default_format.kv_format = Some(Box::new(format));
354+
self.format.default_format.key_values(format);
355355
self
356356
}
357357

0 commit comments

Comments
 (0)