Skip to content

Commit 916d376

Browse files
committed
refactor(fmt): Pull out logger's builder methods
1 parent 7addf98 commit 916d376

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
@@ -303,6 +303,78 @@ pub(crate) struct DefaultFormat {
303303
pub(crate) kv_format: Option<Box<KvFormatFn>>,
304304
}
305305

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

src/logger.rs

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

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

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

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

@@ -289,26 +289,26 @@ impl Builder {
289289

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

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

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

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

@@ -334,7 +334,7 @@ impl Builder {
334334

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

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

0 commit comments

Comments
 (0)