Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ignore-messages can be specified by regex #67

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Set up a filter to catch abnormal log messages and notify using Reporter.
<dt>Handle Error</dt><dd>Report when error message is detected in log</dd>
<dt>Handle Assert</dt><dd>Report when assert message is detected in log</dd>
<dt>Handle Warning</dt><dd>Report when warning message is detected in log</dd>
<dt>Ignore Messages</dt><dd>Log messages containing this string will not be report</dd>
<dt>Ignore Messages</dt><dd>Log messages containing this string will not be report. Can specify regex.</dd>
asurato marked this conversation as resolved.
Show resolved Hide resolved
</dl>


Expand Down
2 changes: 1 addition & 1 deletion README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ v1.0.0時点では `EmergencyExitAgent` の使用を想定しています。
<dt>handle Error</dt><dd>エラーを検知したらレポータで通知します</dd>
<dt>handle Assert</dt><dd>アサート違反を検知したらレポータで通知します</dd>
<dt>handle Warning</dt><dd>警告を検知したらレポータで通知します</dd>
<dt>Ignore Messages</dt><dd>ここに設定した文字列を含むメッセージはレポータで通知しません</dd>
<dt>Ignore Messages</dt><dd>ここに設定した文字列を含むメッセージはレポータで通知しません。正規表現でも指定できます</dd>
</dl>


Expand Down
23 changes: 17 additions & 6 deletions Runtime/Utilities/LogMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// This software is released under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Cysharp.Threading.Tasks;
using DeNA.Anjin.Reporters;
using DeNA.Anjin.Settings;
Expand All @@ -17,6 +20,7 @@ public class LogMessageHandler : IDisposable
{
private readonly AutopilotSettings _settings;
private readonly AbstractReporter _reporter;
private List<Regex> _ignoreMessagesRegexes;

/// <summary>
/// Constructor
Expand Down Expand Up @@ -63,7 +67,7 @@ public async void HandleLog(string logString, string stackTrace, LogType type)
}
}

private static bool IsIgnoreMessage(string logString, string stackTrace, LogType type,
private bool IsIgnoreMessage(string logString, string stackTrace, LogType type,
AutopilotSettings settings)
{
if (type == LogType.Log)
Expand Down Expand Up @@ -91,12 +95,14 @@ private static bool IsIgnoreMessage(string logString, string stackTrace, LogType
return true;
}

foreach (var ignoreMessage in settings.ignoreMessages)
if (_ignoreMessagesRegexes == null)
{
if (logString.Contains(ignoreMessage))
{
return true;
}
_ignoreMessagesRegexes = CreateIgnoreMessageRegexes(_settings);
}

if (_ignoreMessagesRegexes.Exists(regex => regex.IsMatch(logString)))
{
return true;
}

if (stackTrace.Contains(nameof(LogMessageHandler)) || stackTrace.Contains(nameof(SlackAPI)))
Expand All @@ -107,5 +113,10 @@ private static bool IsIgnoreMessage(string logString, string stackTrace, LogType

return false;
}

private static List<Regex> CreateIgnoreMessageRegexes(AutopilotSettings settings)
{
return settings.ignoreMessages.Select(ignoreMessage => new Regex(ignoreMessage)).ToList();
}
}
}
30 changes: 30 additions & 0 deletions Tests/Runtime/Utilities/LogMessageHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,36 @@ public async Task HandleLog_ContainsIgnoreMessage_notReported()
Assert.That(spyReporter.Arguments, Is.Empty);
}

[Test]
public async Task HandleLog_MatchIgnoreMessagePattern_notReported()
{
var settings = CreateEmptyAutopilotSettings();
var spyReporter = ScriptableObject.CreateInstance<SpyReporter>();
var sut = new LogMessageHandler(settings, spyReporter);

settings.ignoreMessages = new[] { "ignore.+ignore" };
settings.handleException = true;
sut.HandleLog("ignore_xxx_ignore", string.Empty, LogType.Exception);
await UniTask.NextFrame();

Assert.That(spyReporter.Arguments, Is.Empty);
}

[Test]
public async Task HandleLog_NotMatchIgnoreMessagePattern_Reported()
{
var settings = CreateEmptyAutopilotSettings();
var spyReporter = ScriptableObject.CreateInstance<SpyReporter>();
var sut = new LogMessageHandler(settings, spyReporter);

settings.ignoreMessages = new[] { "ignore.+ignore" };
settings.handleException = true;
sut.HandleLog("ignore", string.Empty, LogType.Exception);
await UniTask.NextFrame();

Assert.That(spyReporter.Arguments, Is.Not.Empty);
}

asurato marked this conversation as resolved.
Show resolved Hide resolved
[Test]
public async Task HandleLog_StacktraceByLogMessageHandler_notReported()
{
Expand Down