Skip to content

Commit

Permalink
Update logging in IrcMembershipClient (#151)
Browse files Browse the repository at this point in the history
Co-authored-by: Foretack <[email protected]>
  • Loading branch information
occluder and occluder authored Jan 26, 2025
1 parent 9767415 commit 0a8193b
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions MiniTwitch.Irc/IrcMembershipClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class IrcMembershipClient : IAsyncDisposable
{
#region Constants
private const string CONN_URL = "wss://irc-ws.chat.twitch.tv:443";
private const string LOG_HEADER = "[MiniTwitch:Membership]";
private const string LOG_HEADER = "Irc-Membership";
#endregion

#region Properties
Expand All @@ -25,6 +25,11 @@ public sealed class IrcMembershipClient : IAsyncDisposable
/// </summary>
public Action<Exception> ExceptionHandler { get; set; } = default!;
/// <summary>
/// The default logger for <see cref="IrcChannel"/>, only used when <see cref="ILogger"/> is not provided in client options
/// <para>Can be toggled with <see cref="DefaultMiniTwitchLogger{T}.Enabled"/></para>
/// </summary>
public DefaultMiniTwitchLogger<IrcMembershipClient> DefaultLogger { get; } = new();
/// <summary>
/// Whether the client is currently connected
/// </summary>
public bool IsConnected => _ws.IsConnected;
Expand Down Expand Up @@ -64,6 +69,7 @@ public sealed class IrcMembershipClient : IAsyncDisposable
private readonly List<string> _joinedChannels = new();
private readonly RateLimitManager _manager;
private bool _connectInvoked;
IDisposable? _loggingScope;
#endregion

#region Init
Expand All @@ -77,6 +83,7 @@ public IrcMembershipClient(Action<IMembershipClientOptions> options)
_options = clientOptions;
_ws = new(_options.ReconnectionDelay, 8192);
_manager = new(clientOptions);
_loggingScope = GetLogger().BeginScope(LOG_HEADER);

InternalInt();
}
Expand Down Expand Up @@ -308,9 +315,10 @@ private void HandleMessage(IrcMessage message)
#endregion

#region Utils
private ILogger GetLogger() => _options.Logger ?? this.DefaultLogger;
private void LogEventException(Exception ex) => LogException(ex, "🚨 Exception caught in an event:");
private void Log(LogLevel level, string template, params object[] properties) => _options.Logger?.Log(level, $"{LOG_HEADER} " + template, properties);
private void LogException(Exception ex, string template, params object[] properties) => _options.Logger?.LogError(ex, $"{LOG_HEADER} " + template, properties);
private void Log(LogLevel level, string template, params object[] properties) => GetLogger().Log(level, template, properties);
private void LogException(Exception ex, string template, params object[] properties) => GetLogger().LogError(ex, template, properties);
#endregion

/// <inheritdoc/>
Expand All @@ -320,5 +328,6 @@ public async ValueTask DisposeAsync()
_joinedChannels.Clear();
_connectionWaiter.Dispose();
_joinChannelWaiter.Dispose();
_loggingScope?.Dispose();
}
}

0 comments on commit 0a8193b

Please sign in to comment.