[api-logs] LogRecordSeverity enum tweaks (#4451)
This commit is contained in:
parent
bb16c20612
commit
361750c45d
|
|
@ -23,21 +23,78 @@ namespace OpenTelemetry.Logs;
|
|||
/// </summary>
|
||||
internal enum LogRecordSeverity
|
||||
{
|
||||
/// <summary>Trace severity.</summary>
|
||||
Trace,
|
||||
/// <summary>Unspecified severity (0).</summary>
|
||||
Unspecified = 0,
|
||||
|
||||
/// <summary>Debug severity.</summary>
|
||||
Debug,
|
||||
/// <summary>Trace severity (1).</summary>
|
||||
Trace = 1,
|
||||
|
||||
/// <summary>Information severity.</summary>
|
||||
Information,
|
||||
/// <summary>Trace1 severity (2).</summary>
|
||||
Trace2 = Trace + 1,
|
||||
|
||||
/// <summary>Warning severity.</summary>
|
||||
Warning,
|
||||
/// <summary>Trace3 severity (3).</summary>
|
||||
Trace3 = Trace2 + 1,
|
||||
|
||||
/// <summary>Error severity.</summary>
|
||||
Error,
|
||||
/// <summary>Trace4 severity (4).</summary>
|
||||
Trace4 = Trace3 + 1,
|
||||
|
||||
/// <summary>Fatal severity.</summary>
|
||||
Fatal,
|
||||
/// <summary>Debug severity (5).</summary>
|
||||
Debug = 5,
|
||||
|
||||
/// <summary>Debug2 severity (6).</summary>
|
||||
Debug2 = Debug + 1,
|
||||
|
||||
/// <summary>Debug3 severity (7).</summary>
|
||||
Debug3 = Debug2 + 1,
|
||||
|
||||
/// <summary>Debug4 severity (8).</summary>
|
||||
Debug4 = Debug3 + 1,
|
||||
|
||||
/// <summary>Info severity (9).</summary>
|
||||
Info = 9,
|
||||
|
||||
/// <summary>Info2 severity (11).</summary>
|
||||
Info2 = Info + 1,
|
||||
|
||||
/// <summary>Info3 severity (12).</summary>
|
||||
Info3 = Info2 + 1,
|
||||
|
||||
/// <summary>Info4 severity (13).</summary>
|
||||
Info4 = Info3 + 1,
|
||||
|
||||
/// <summary>Warn severity (13).</summary>
|
||||
Warn = 13,
|
||||
|
||||
/// <summary>Warn2 severity (14).</summary>
|
||||
Warn2 = Warn + 1,
|
||||
|
||||
/// <summary>Warn3 severity (15).</summary>
|
||||
Warn3 = Warn2 + 1,
|
||||
|
||||
/// <summary>Warn severity (16).</summary>
|
||||
Warn4 = Warn3 + 1,
|
||||
|
||||
/// <summary>Error severity (17).</summary>
|
||||
Error = 17,
|
||||
|
||||
/// <summary>Error2 severity (18).</summary>
|
||||
Error2 = Error + 1,
|
||||
|
||||
/// <summary>Error3 severity (19).</summary>
|
||||
Error3 = Error2 + 1,
|
||||
|
||||
/// <summary>Error4 severity (20).</summary>
|
||||
Error4 = Error3 + 1,
|
||||
|
||||
/// <summary>Fatal severity (21).</summary>
|
||||
Fatal = 21,
|
||||
|
||||
/// <summary>Fatal2 severity (22).</summary>
|
||||
Fatal2 = Fatal + 1,
|
||||
|
||||
/// <summary>Fatal3 severity (23).</summary>
|
||||
Fatal3 = Fatal2 + 1,
|
||||
|
||||
/// <summary>Fatal4 severity (24).</summary>
|
||||
Fatal4 = Fatal3 + 1,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,115 @@
|
|||
// <copyright file="LogRecordSeverityExtensions.cs" company="OpenTelemetry Authors">
|
||||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace OpenTelemetry.Logs;
|
||||
|
||||
/// <summary>
|
||||
/// Contains extension methods for the <see cref="LogRecordSeverity"/> enum.
|
||||
/// </summary>
|
||||
internal static class LogRecordSeverityExtensions
|
||||
{
|
||||
internal const string UnspecifiedShortName = "UNSPECIFIED";
|
||||
|
||||
internal const string TraceShortName = "TRACE";
|
||||
internal const string Trace2ShortName = TraceShortName + "2";
|
||||
internal const string Trace3ShortName = TraceShortName + "3";
|
||||
internal const string Trace4ShortName = TraceShortName + "4";
|
||||
|
||||
internal const string DebugShortName = "DEBUG";
|
||||
internal const string Debug2ShortName = DebugShortName + "2";
|
||||
internal const string Debug3ShortName = DebugShortName + "3";
|
||||
internal const string Debug4ShortName = DebugShortName + "4";
|
||||
|
||||
internal const string InfoShortName = "INFO";
|
||||
internal const string Info2ShortName = InfoShortName + "2";
|
||||
internal const string Info3ShortName = InfoShortName + "3";
|
||||
internal const string Info4ShortName = InfoShortName + "4";
|
||||
|
||||
internal const string WarnShortName = "WARN";
|
||||
internal const string Warn2ShortName = WarnShortName + "2";
|
||||
internal const string Warn3ShortName = WarnShortName + "3";
|
||||
internal const string Warn4ShortName = WarnShortName + "4";
|
||||
|
||||
internal const string ErrorShortName = "ERROR";
|
||||
internal const string Error2ShortName = ErrorShortName + "2";
|
||||
internal const string Error3ShortName = ErrorShortName + "3";
|
||||
internal const string Error4ShortName = ErrorShortName + "4";
|
||||
|
||||
internal const string FatalShortName = "FATAL";
|
||||
internal const string Fatal2ShortName = FatalShortName + "2";
|
||||
internal const string Fatal3ShortName = FatalShortName + "3";
|
||||
internal const string Fatal4ShortName = FatalShortName + "4";
|
||||
|
||||
private static readonly string[] LogRecordSeverityShortNames = new string[]
|
||||
{
|
||||
UnspecifiedShortName,
|
||||
|
||||
TraceShortName,
|
||||
Trace2ShortName,
|
||||
Trace3ShortName,
|
||||
Trace4ShortName,
|
||||
|
||||
DebugShortName,
|
||||
Debug2ShortName,
|
||||
Debug3ShortName,
|
||||
Debug4ShortName,
|
||||
|
||||
InfoShortName,
|
||||
Info2ShortName,
|
||||
Info3ShortName,
|
||||
Info4ShortName,
|
||||
|
||||
WarnShortName,
|
||||
Warn2ShortName,
|
||||
Warn3ShortName,
|
||||
Warn4ShortName,
|
||||
|
||||
ErrorShortName,
|
||||
Error2ShortName,
|
||||
Error3ShortName,
|
||||
Error4ShortName,
|
||||
|
||||
FatalShortName,
|
||||
Fatal2ShortName,
|
||||
Fatal3ShortName,
|
||||
Fatal4ShortName,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Returns the OpenTelemetry Specification short name for the <see
|
||||
/// cref="LogRecordSeverity"/> suitable for display.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// See: <see
|
||||
/// href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#displaying-severity"/>.
|
||||
/// </remarks>
|
||||
/// <param name="logRecordSeverity"><see cref="LogRecordSeverity"/>.</param>
|
||||
/// <returns>OpenTelemetry Specification short name for the supplied <see
|
||||
/// cref="LogRecordSeverity"/>.</returns>
|
||||
public static string ToShortName(this LogRecordSeverity logRecordSeverity)
|
||||
{
|
||||
int severityLevel = (int)logRecordSeverity;
|
||||
|
||||
if (severityLevel < 0 || severityLevel > 24)
|
||||
{
|
||||
severityLevel = 0;
|
||||
}
|
||||
|
||||
return LogRecordSeverityShortNames[severityLevel];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
// <copyright file="LogRecordSeverityExtensionsTests.cs" company="OpenTelemetry Authors">
|
||||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
#nullable enable
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace OpenTelemetry.Logs.Tests;
|
||||
|
||||
public sealed class LogRecordSeverityExtensionsTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0, LogRecordSeverityExtensions.UnspecifiedShortName)]
|
||||
[InlineData(int.MinValue, LogRecordSeverityExtensions.UnspecifiedShortName)]
|
||||
[InlineData(int.MaxValue, LogRecordSeverityExtensions.UnspecifiedShortName)]
|
||||
[InlineData(1, LogRecordSeverityExtensions.TraceShortName)]
|
||||
[InlineData(2, LogRecordSeverityExtensions.Trace2ShortName)]
|
||||
[InlineData(3, LogRecordSeverityExtensions.Trace3ShortName)]
|
||||
[InlineData(4, LogRecordSeverityExtensions.Trace4ShortName)]
|
||||
[InlineData(5, LogRecordSeverityExtensions.DebugShortName)]
|
||||
[InlineData(6, LogRecordSeverityExtensions.Debug2ShortName)]
|
||||
[InlineData(7, LogRecordSeverityExtensions.Debug3ShortName)]
|
||||
[InlineData(8, LogRecordSeverityExtensions.Debug4ShortName)]
|
||||
[InlineData(9, LogRecordSeverityExtensions.InfoShortName)]
|
||||
[InlineData(10, LogRecordSeverityExtensions.Info2ShortName)]
|
||||
[InlineData(11, LogRecordSeverityExtensions.Info3ShortName)]
|
||||
[InlineData(12, LogRecordSeverityExtensions.Info4ShortName)]
|
||||
[InlineData(13, LogRecordSeverityExtensions.WarnShortName)]
|
||||
[InlineData(14, LogRecordSeverityExtensions.Warn2ShortName)]
|
||||
[InlineData(15, LogRecordSeverityExtensions.Warn3ShortName)]
|
||||
[InlineData(16, LogRecordSeverityExtensions.Warn4ShortName)]
|
||||
[InlineData(17, LogRecordSeverityExtensions.ErrorShortName)]
|
||||
[InlineData(18, LogRecordSeverityExtensions.Error2ShortName)]
|
||||
[InlineData(19, LogRecordSeverityExtensions.Error3ShortName)]
|
||||
[InlineData(20, LogRecordSeverityExtensions.Error4ShortName)]
|
||||
[InlineData(21, LogRecordSeverityExtensions.FatalShortName)]
|
||||
[InlineData(22, LogRecordSeverityExtensions.Fatal2ShortName)]
|
||||
[InlineData(23, LogRecordSeverityExtensions.Fatal3ShortName)]
|
||||
[InlineData(24, LogRecordSeverityExtensions.Fatal4ShortName)]
|
||||
public void ToShortNameTest(int logRecordSeverityValue, string expectedName)
|
||||
{
|
||||
var logRecordSeverity = (LogRecordSeverity)logRecordSeverityValue;
|
||||
|
||||
Assert.Equal(expectedName, logRecordSeverity.ToShortName());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue