diff --git a/src/OpenTelemetry.Api/Logs/LogRecordSeverity.cs b/src/OpenTelemetry.Api/Logs/LogRecordSeverity.cs index 3256b530952..7d11791eeda 100644 --- a/src/OpenTelemetry.Api/Logs/LogRecordSeverity.cs +++ b/src/OpenTelemetry.Api/Logs/LogRecordSeverity.cs @@ -23,21 +23,78 @@ namespace OpenTelemetry.Logs; /// internal enum LogRecordSeverity { - /// Trace severity. - Trace, + /// Unspecified severity (0). + Unspecified = 0, - /// Debug severity. - Debug, + /// Trace severity (1). + Trace = 1, - /// Information severity. - Information, + /// Trace1 severity (2). + Trace2 = Trace + 1, - /// Warning severity. - Warning, + /// Trace3 severity (3). + Trace3 = Trace2 + 1, - /// Error severity. - Error, + /// Trace4 severity (4). + Trace4 = Trace3 + 1, - /// Fatal severity. - Fatal, + /// Debug severity (5). + Debug = 5, + + /// Debug2 severity (6). + Debug2 = Debug + 1, + + /// Debug3 severity (7). + Debug3 = Debug2 + 1, + + /// Debug4 severity (8). + Debug4 = Debug3 + 1, + + /// Info severity (9). + Info = 9, + + /// Info2 severity (11). + Info2 = Info + 1, + + /// Info3 severity (12). + Info3 = Info2 + 1, + + /// Info4 severity (13). + Info4 = Info3 + 1, + + /// Warn severity (13). + Warn = 13, + + /// Warn2 severity (14). + Warn2 = Warn + 1, + + /// Warn3 severity (15). + Warn3 = Warn2 + 1, + + /// Warn severity (16). + Warn4 = Warn3 + 1, + + /// Error severity (17). + Error = 17, + + /// Error2 severity (18). + Error2 = Error + 1, + + /// Error3 severity (19). + Error3 = Error2 + 1, + + /// Error4 severity (20). + Error4 = Error3 + 1, + + /// Fatal severity (21). + Fatal = 21, + + /// Fatal2 severity (22). + Fatal2 = Fatal + 1, + + /// Fatal3 severity (23). + Fatal3 = Fatal2 + 1, + + /// Fatal4 severity (24). + Fatal4 = Fatal3 + 1, } diff --git a/src/OpenTelemetry.Api/Logs/LogRecordSeverityExtensions.cs b/src/OpenTelemetry.Api/Logs/LogRecordSeverityExtensions.cs new file mode 100644 index 00000000000..875ff46f71e --- /dev/null +++ b/src/OpenTelemetry.Api/Logs/LogRecordSeverityExtensions.cs @@ -0,0 +1,115 @@ +// +// 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. +// + +#nullable enable + +namespace OpenTelemetry.Logs; + +/// +/// Contains extension methods for the enum. +/// +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, + }; + + /// + /// Returns the OpenTelemetry Specification short name for the suitable for display. + /// + /// + /// See: . + /// + /// . + /// OpenTelemetry Specification short name for the supplied . + public static string ToShortName(this LogRecordSeverity logRecordSeverity) + { + int severityLevel = (int)logRecordSeverity; + + if (severityLevel < 0 || severityLevel > 24) + { + severityLevel = 0; + } + + return LogRecordSeverityShortNames[severityLevel]; + } +} diff --git a/test/OpenTelemetry.Api.Tests/Logs/LogRecordSeverityExtensionsTests.cs b/test/OpenTelemetry.Api.Tests/Logs/LogRecordSeverityExtensionsTests.cs new file mode 100644 index 00000000000..969743b0ebd --- /dev/null +++ b/test/OpenTelemetry.Api.Tests/Logs/LogRecordSeverityExtensionsTests.cs @@ -0,0 +1,59 @@ +// +// 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. +// + +#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()); + } +}