log.rs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //! A logger implementation that writes log messages to stdout. This struct implements the
  2. //! `LogWriter` trait, which defines a way to handle log records. The log records are formatted,
  3. //! assigned a severity level, and emitted as structured tracing events.
  4. pub struct StdoutLogWriter;
  5. impl crate::LogWriter for StdoutLogWriter {
  6. /// Logs a given `LogRecord` instance using structured tracing events.
  7. fn log(&self, record: ldk_node::logger::LogRecord) {
  8. let level = match record.level.to_string().to_ascii_lowercase().as_str() {
  9. "error" => tracing::Level::ERROR,
  10. "warn" | "warning" => tracing::Level::WARN,
  11. "debug" => tracing::Level::DEBUG,
  12. "trace" => tracing::Level::TRACE,
  13. _ => tracing::Level::INFO,
  14. };
  15. // Format message once
  16. let msg = record.args.to_string();
  17. // Emit as a structured tracing event.
  18. // Use level-specific macros (require compile-time level) and record the original module path as a field.
  19. match level {
  20. tracing::Level::ERROR => {
  21. tracing::error!(
  22. module_path = record.module_path,
  23. line = record.line,
  24. "{msg}"
  25. );
  26. }
  27. tracing::Level::WARN => {
  28. tracing::warn!(
  29. module_path = record.module_path,
  30. line = record.line,
  31. "{msg}"
  32. );
  33. }
  34. tracing::Level::INFO => {
  35. tracing::info!(
  36. module_path = record.module_path,
  37. line = record.line,
  38. "{msg}"
  39. );
  40. }
  41. tracing::Level::DEBUG => {
  42. tracing::debug!(
  43. module_path = record.module_path,
  44. line = record.line,
  45. "{msg}"
  46. );
  47. }
  48. tracing::Level::TRACE => {
  49. tracing::trace!(
  50. module_path = record.module_path,
  51. line = record.line,
  52. "{msg}"
  53. );
  54. }
  55. }
  56. }
  57. }
  58. impl Default for StdoutLogWriter {
  59. /// Provides the default implementation for the struct.
  60. fn default() -> Self {
  61. Self {}
  62. }
  63. }