Skip to content

Commit

Permalink
Add Exception overloads to all logging methods; close #3424
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb authored and Danthar committed Jul 5, 2018
1 parent 810f8fc commit 913fc75
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 29 deletions.
65 changes: 65 additions & 0 deletions src/core/Akka.Tests/Event/LoggerSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,75 @@
using Akka.TestKit;
using Xunit;
using Xunit.Abstractions;
using FluentAssertions;

namespace Akka.Tests.Event
{
public class LoggerSpec : AkkaSpec
{
public static readonly Config Config = @"akka.loglevel = DEBUG";

public LoggerSpec(ITestOutputHelper helper) : base(Config, helper) { }

[Theory]
[InlineData(LogLevel.ErrorLevel, false, "foo", new object[] { })]
[InlineData(LogLevel.ErrorLevel, true, "foo", new object[] { })]
[InlineData(LogLevel.ErrorLevel, false, "foo {0}", new object[] { 1 })]
[InlineData(LogLevel.ErrorLevel, true, "foo {0}", new object[] { 1 })]
[InlineData(LogLevel.WarningLevel, false, "foo", new object[] { })]
[InlineData(LogLevel.WarningLevel, true, "foo", new object[] { })]
[InlineData(LogLevel.WarningLevel, false, "foo {0}", new object[] { 1 })]
[InlineData(LogLevel.WarningLevel, true, "foo {0}", new object[] { 1 })]
[InlineData(LogLevel.InfoLevel, false, "foo", new object[] { })]
[InlineData(LogLevel.InfoLevel, true, "foo", new object[] { })]
[InlineData(LogLevel.InfoLevel, false, "foo {0}", new object[] { 1 })]
[InlineData(LogLevel.InfoLevel, true, "foo {0}", new object[] { 1 })]
[InlineData(LogLevel.DebugLevel, false, "foo", new object[]{})]
[InlineData(LogLevel.DebugLevel, true, "foo", new object[] { })]
[InlineData(LogLevel.DebugLevel, false, "foo {0}", new object[] { 1 })]
[InlineData(LogLevel.DebugLevel, true, "foo {0}", new object[] { 1 })]
public void LoggingAdapter_should_log_all_information(LogLevel logLevel, bool includeException, string formatStr, object [] args)
{
Sys.EventStream.Subscribe(TestActor, typeof(LogEvent));
var msg = args != null ? string.Format(formatStr, args) : formatStr;
var ex = new Exception();
switch (logLevel)
{
case LogLevel.DebugLevel when includeException:
Log.Debug(ex, formatStr, args);
break;
case LogLevel.DebugLevel:
Log.Debug(formatStr, args);
break;
case LogLevel.InfoLevel when includeException:
Log.Info(ex, formatStr, args);
break;
case LogLevel.InfoLevel:
Log.Info(formatStr, args);
break;
case LogLevel.WarningLevel when includeException:
Log.Warning(ex, formatStr, args);
break;
case LogLevel.WarningLevel:
Log.Warning(formatStr, args);
break;
case LogLevel.ErrorLevel when includeException:
Log.Error(ex, formatStr, args);
break;
case LogLevel.ErrorLevel:
Log.Error(formatStr, args);
break;
}

var log = ExpectMsg<LogEvent>();
log.Message.ToString().Should().Be(msg);
log.LogLevel().Should().Be(logLevel);
if (includeException)
log.Cause.Should().Be(ex);
else
log.Cause.Should().BeNull();
}

[Fact]
public async Task LoggingBus_should_stop_all_loggers_on_termination()
{
Expand All @@ -46,3 +110,4 @@ public async Task LoggingBus_should_stop_all_loggers_on_termination()
}
}
}

15 changes: 14 additions & 1 deletion src/core/Akka/Event/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@ public class Debug : LogEvent
/// <param name="logSource">The source that generated the log event.</param>
/// <param name="logClass">The type of logger used to log the event.</param>
/// <param name="message">The message that is being logged.</param>
public Debug(string logSource, Type logClass, object message)
public Debug(string logSource, Type logClass, object message)
: this(null, logSource, logClass, message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Debug" /> class.
/// </summary>
/// <param name="cause">The exception that generated the log event.</param>
/// <param name="logSource">The source that generated the log event.</param>
/// <param name="logClass">The type of logger used to log the event.</param>
/// <param name="message">The message that is being logged.</param>
public Debug(Exception cause, string logSource, Type logClass, object message)
{
LogSource = logSource;
LogClass = logClass;
Message = message;
Cause = cause;
}

/// <summary>
Expand Down
19 changes: 0 additions & 19 deletions src/core/Akka/Event/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public Error(Exception cause, string logSource, Type logClass, object message)
Message = message;
}

/// <summary>
/// The exception that caused the log event.
/// </summary>
public Exception Cause { get; private set; }

/// <summary>
/// Retrieves the <see cref="Akka.Event.LogLevel" /> used to classify this event.
/// </summary>
Expand All @@ -42,19 +37,5 @@ public override LogLevel LogLevel()
{
return Event.LogLevel.ErrorLevel;
}

/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
public override string ToString()
{
var cause = Cause;
var causeStr = cause == null ? "Unknown" : cause.ToString();
var errorStr = string.Format("[{0}][{1}][Thread {2}][{3}] {4}{5}Cause: {6}",
LogLevel().ToString().Replace("Level", "").ToUpperInvariant(), Timestamp,
Thread.ManagedThreadId.ToString().PadLeft(4, '0'), LogSource, Message, Environment.NewLine, causeStr);
return errorStr;
}
}
}
48 changes: 48 additions & 0 deletions src/core/Akka/Event/ILoggingAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,44 @@ public interface ILoggingAdapter
/// <param name="args">An optional list of items used to format the message.</param>
void Debug(string format, params object[] args);

/// <summary>
/// Logs a <see cref="LogLevel.DebugLevel"/> message and associated exception.
/// </summary>
/// <param name="cause">The exception associated with this message.</param>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
void Debug(Exception cause, string format, params object[] args);

/// <summary>
/// Logs a <see cref="LogLevel.InfoLevel"/> message.
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
void Info(string format, params object[] args);

/// <summary>
/// Logs a <see cref="LogLevel.InfoLevel"/> message and associated exception.
/// </summary>
/// <param name="cause">The exception associated with this message.</param>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
void Info(Exception cause, string format, params object[] args);

/// <summary>
/// Logs a <see cref="LogLevel.WarningLevel"/> message.
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
void Warning(string format, params object[] args);

/// <summary>
/// Logs a <see cref="LogLevel.WarningLevel"/> message and associated exception.
/// </summary>
/// <param name="cause">The exception associated with this message.</param>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
void Warning(Exception cause, string format, params object[] args);

/// <summary>
/// Logs a <see cref="LogLevel.ErrorLevel"/> message.
/// </summary>
Expand Down Expand Up @@ -136,13 +160,29 @@ public bool IsEnabled(LogLevel logLevel)
/// <param name="args">An optional list of items used to format the message.</param>
public void Debug(string format, params object[] args) { }

/// <summary>
/// Logs a <see cref="LogLevel.ErrorLevel" /> message and associated exception.
/// </summary>
/// <param name="cause">The exception associated with this message.</param>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public void Debug(Exception cause, string format, params object[] args){ }

/// <summary>
/// Logs a <see cref="LogLevel.InfoLevel" /> message.
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public void Info(string format, params object[] args) { }

/// <summary>
/// Logs a <see cref="LogLevel.InfoLevel" /> message and associated exception.
/// </summary>
/// <param name="cause">The exception associated with this message.</param>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public void Info(Exception cause, string format, params object[] args){ }

/// <summary>
/// Obsolete. Use <see cref="Warning" /> instead!
/// </summary>
Expand All @@ -157,6 +197,14 @@ public bool IsEnabled(LogLevel logLevel)
/// <param name="args">An optional list of items used to format the message.</param>
public void Warning(string format, params object[] args) { }

/// <summary>
/// Logs a <see cref="LogLevel.WarningLevel" /> message and associated exception.
/// </summary>
/// <param name="cause">The exception associated with this message.</param>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public void Warning(Exception cause, string format, params object[] args){ }

/// <summary>
/// Logs a <see cref="LogLevel.ErrorLevel" /> message.
/// </summary>
Expand Down
15 changes: 14 additions & 1 deletion src/core/Akka/Event/Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@ public class Info : LogEvent
/// <param name="logSource">The source that generated the log event.</param>
/// <param name="logClass">The type of logger used to log the event.</param>
/// <param name="message">The message that is being logged.</param>
public Info(string logSource, Type logClass, object message)
public Info(string logSource, Type logClass, object message)
: this(null, logSource, logClass, message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Info" /> class.
/// </summary>
/// <param name="cause">The exception that generated the log event.</param>
/// <param name="logSource">The source that generated the log event.</param>
/// <param name="logClass">The type of logger used to log the event.</param>
/// <param name="message">The message that is being logged.</param>
public Info(Exception cause, string logSource, Type logClass, object message)
{
Cause = cause;
LogSource = logSource;
LogClass = logClass;
Message = message;
Expand Down
11 changes: 10 additions & 1 deletion src/core/Akka/Event/LogEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ protected LogEvent()
Thread = Thread.CurrentThread;
}

/// <summary>
/// The exception that caused the log event. Can be <c>null</c>
/// </summary>
public Exception Cause { get; protected set; }

/// <summary>
/// The timestamp that this event occurred.
/// </summary>
Expand Down Expand Up @@ -89,7 +94,11 @@ protected LogEvent()
/// <returns>A <see cref="System.String" /> that represents this LogEvent.</returns>
public override string ToString()
{
return string.Format("[{0}][{1}][Thread {2}][{3}] {4}", LogLevel().PrettyNameFor(), Timestamp, Thread.ManagedThreadId.ToString().PadLeft(4, '0'), LogSource, Message);
if(Cause == null)
return
$"[{LogLevel().PrettyNameFor()}][{Timestamp}][Thread {Thread.ManagedThreadId.ToString().PadLeft(4, '0')}][{LogSource}] {Message}";
return
$"[{LogLevel().PrettyNameFor()}][{Timestamp}][Thread {Thread.ManagedThreadId.ToString().PadLeft(4, '0')}][{LogSource}] {Message}{Environment.NewLine}Cause: {Cause}";
}
}
}
29 changes: 22 additions & 7 deletions src/core/Akka/Event/LoggingAdapterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected void NotifyLog(LogLevel logLevel, object message)
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public void Debug(string format, params object[] args)
public virtual void Debug(string format, params object[] args)
{
if (!IsDebugEnabled)
return;
Expand All @@ -150,22 +150,32 @@ public void Debug(string format, params object[] args)
}
}

public virtual void Debug(Exception cause, string format, params object[] args)
{
throw new NotImplementedException();
}

/// <summary>
/// Obsolete. Use <see cref="Warning" /> instead!
/// </summary>
/// <param name="format">N/A</param>
/// <param name="args">N/A</param>
public void Warn(string format, params object[] args)
public virtual void Warn(string format, params object[] args)
{
Warning(format, args);
}

public virtual void Info(Exception cause, string format, params object[] args)
{
throw new NotImplementedException();
}

/// <summary>
/// Logs a <see cref="LogLevel.WarningLevel" /> message.
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public void Warning(string format, params object[] args)
public virtual void Warning(string format, params object[] args)
{
if (!IsWarningEnabled)
return;
Expand All @@ -180,13 +190,18 @@ public void Warning(string format, params object[] args)
}
}

public virtual void Warning(Exception cause, string format, params object[] args)
{
throw new NotImplementedException();
}

/// <summary>
/// Logs a <see cref="LogLevel.ErrorLevel" /> message and associated exception.
/// </summary>
/// <param name="cause">The exception associated with this message.</param>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public void Error(Exception cause, string format, params object[] args)
public virtual void Error(Exception cause, string format, params object[] args)
{
if (!IsErrorEnabled)
return;
Expand All @@ -206,7 +221,7 @@ public void Error(Exception cause, string format, params object[] args)
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public void Error(string format, params object[] args)
public virtual void Error(string format, params object[] args)
{
if (!IsErrorEnabled)
return;
Expand All @@ -226,7 +241,7 @@ public void Error(string format, params object[] args)
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public void Info(string format, params object[] args)
public virtual void Info(string format, params object[] args)
{
if (!IsInfoEnabled)
return;
Expand All @@ -247,7 +262,7 @@ public void Info(string format, params object[] args)
/// <param name="logLevel">The level used to log the message.</param>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public void Log(LogLevel logLevel, string format, params object[] args)
public virtual void Log(LogLevel logLevel, string format, params object[] args)
{
if (args == null || args.Length == 0)
{
Expand Down
13 changes: 13 additions & 0 deletions src/core/Akka/Event/Warning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@ public class Warning : LogEvent
/// <param name="logClass">The type of logger used to log the event.</param>
/// <param name="message">The message that is being logged.</param>
public Warning(string logSource, Type logClass, object message)
: this(null, logSource, logClass, message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Warning" /> class.
/// </summary>
/// <param name="cause">The exception that caused the log event.</param>
/// <param name="logSource">The source that generated the log event.</param>
/// <param name="logClass">The type of logger used to log the event.</param>
/// <param name="message">The message that is being logged.</param>
public Warning(Exception cause, string logSource, Type logClass, object message)
{
LogSource = logSource;
LogClass = logClass;
Message = message;
Cause = cause;
}

/// <summary>
Expand Down

0 comments on commit 913fc75

Please sign in to comment.