From 5939e19f7208de4530a8d8b0f10c27ece5011686 Mon Sep 17 00:00:00 2001 From: d11n Date: Wed, 21 Feb 2024 14:45:05 +0100 Subject: [PATCH] Lightning: Replace user info in server URL when logging (#5750) * Lightning: Replace user info in server URL when logging Fixes #5747. * Fix empty user info case --- .../Payments/Lightning/LightningListener.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/BTCPayServer/Payments/Lightning/LightningListener.cs b/BTCPayServer/Payments/Lightning/LightningListener.cs index 5bdd8f76d..7cca99b0e 100644 --- a/BTCPayServer/Payments/Lightning/LightningListener.cs +++ b/BTCPayServer/Payments/Lightning/LightningListener.cs @@ -504,17 +504,19 @@ namespace BTCPayServer.Payments.Lightning async Task Listen(CancellationToken cancellation) { Uri? uri = null; + string? logUrl = null; try { var lightningClient = _lightningClientFactory.Create(ConnectionString, _network); uri = lightningClient.GetServerUri(); - Logs.PayServer.LogInformation($"{_network.CryptoCode} (Lightning): Start listening {uri}"); + logUrl = string.IsNullOrEmpty(uri.UserInfo) ? uri.ToString() : uri.ToString().Replace(uri.UserInfo, "***"); + Logs.PayServer.LogInformation("{CryptoCode} (Lightning): Start listening {Uri}", _network.CryptoCode, logUrl); using var session = await lightningClient.Listen(cancellation); // Just in case the payment arrived after our last poll but before we listened. await PollAllListenedInvoices(cancellation); if (_ErrorAlreadyLogged) { - Logs.PayServer.LogInformation($"{_network.CryptoCode} (Lightning): Could reconnect successfully to {uri}"); + Logs.PayServer.LogInformation("{CryptoCode} (Lightning): Could reconnect successfully to {Uri}", _network.CryptoCode, logUrl); } _ErrorAlreadyLogged = false; while (!_ListenedInvoices.IsEmpty) @@ -532,7 +534,7 @@ namespace BTCPayServer.Payments.Lightning { if (await AddPayment(notification, listenedInvoice.InvoiceId, listenedInvoice.PaymentMethod.GetId().PaymentType)) { - Logs.PayServer.LogInformation($"{_network.CryptoCode} (Lightning): Payment detected via notification ({listenedInvoice.InvoiceId})"); + Logs.PayServer.LogInformation("{CryptoCode} (Lightning): Payment detected via notification ({InvoiceId})", _network.CryptoCode, listenedInvoice.InvoiceId); } _ListenedInvoices.TryRemove(notification.Id, out var _); } @@ -543,16 +545,15 @@ namespace BTCPayServer.Payments.Lightning } } } - catch (Exception ex) when (!cancellation.IsCancellationRequested && !_ErrorAlreadyLogged) { _ErrorAlreadyLogged = true; - Logs.PayServer.LogError(ex, $"{_network.CryptoCode} (Lightning): Error while contacting {uri}"); - Logs.PayServer.LogInformation($"{_network.CryptoCode} (Lightning): Stop listening {uri}"); + Logs.PayServer.LogError(ex, "{CryptoCode} (Lightning): Error while contacting {Uri}", _network.CryptoCode, logUrl); + Logs.PayServer.LogInformation("{CryptoCode} (Lightning): Stop listening {Uri}", _network.CryptoCode, logUrl); } catch (OperationCanceledException) when (cancellation.IsCancellationRequested) { } if (_ListenedInvoices.IsEmpty) - Logs.PayServer.LogInformation($"{_network.CryptoCode} (Lightning): No more invoice to listen on {uri}, releasing the connection."); + Logs.PayServer.LogInformation("{CryptoCode} (Lightning): No more invoice to listen on {Uri}, releasing the connection", _network.CryptoCode, logUrl); } public DateTimeOffset? LastFullPoll { get; set; }