diff --git a/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs b/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs index 2b92c974a33..63c76231e65 100644 --- a/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs +++ b/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs @@ -27,6 +27,17 @@ namespace OpenTelemetry.Logs { internal sealed class OpenTelemetryLogger : ILogger { + private static readonly string[] LogLevels = new string[] + { + nameof(LogLevel.Trace), + nameof(LogLevel.Debug), + nameof(LogLevel.Information), + nameof(LogLevel.Warning), + nameof(LogLevel.Error), + nameof(LogLevel.Critical), + nameof(LogLevel.None), + }; + private readonly string categoryName; private readonly OpenTelemetryLoggerProvider provider; @@ -66,7 +77,6 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except : null; iloggerData.CategoryName = this.categoryName; iloggerData.EventId = eventId; - iloggerData.LogLevel = logLevel; iloggerData.Exception = exception; iloggerData.ScopeProvider = provider.IncludeScopes ? this.ScopeProvider : null; iloggerData.BufferedScopes = null; @@ -75,6 +85,8 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except data.TimestampBacking = DateTime.UtcNow; + SetLogRecordSeverityFields(ref data, logLevel); + LogRecordData.SetActivityContext(ref data, activity); var attributes = record.Attributes = provider.IncludeAttributes @@ -96,6 +108,9 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except : null; } + // TODO: Set this to a real instance. + record.Logger = null; + processor.OnEnd(record); iloggerData.ScopeProvider = null; @@ -114,6 +129,21 @@ public bool IsEnabled(LogLevel logLevel) public IDisposable BeginScope(TState state) => this.ScopeProvider?.Push(state) ?? NullScope.Instance; + internal static void SetLogRecordSeverityFields(ref LogRecordData logRecordData, LogLevel logLevel) + { + uint intLogLevel = (uint)logLevel; + if (intLogLevel < 6) + { + logRecordData.Severity = (LogRecordSeverity)((intLogLevel * 4) + 1); + logRecordData.SeverityText = LogLevels[intLogLevel]; + } + else + { + logRecordData.Severity = null; + logRecordData.SeverityText = null; + } + } + private static IReadOnlyList>? ProcessState( LogRecord logRecord, ref LogRecord.LogRecordILoggerData iLoggerData, diff --git a/src/OpenTelemetry/Logs/LogRecord.cs b/src/OpenTelemetry/Logs/LogRecord.cs index 9baca46c862..aff363de800 100644 --- a/src/OpenTelemetry/Logs/LogRecord.cs +++ b/src/OpenTelemetry/Logs/LogRecord.cs @@ -65,11 +65,12 @@ internal LogRecord( Body = formattedMessage, }; + OpenTelemetryLogger.SetLogRecordSeverityFields(ref this.Data, logLevel); + this.ILoggerData = new() { TraceState = activity?.TraceStateString, CategoryName = categoryName, - LogLevel = logLevel, FormattedMessage = formattedMessage, EventId = eventId, Exception = exception, @@ -156,10 +157,24 @@ public string? CategoryName /// /// Gets or sets the log . /// + // todo: [Obsolete("Use Severity instead LogLevel will be removed in a future version.")] public LogLevel LogLevel { - get => this.ILoggerData.LogLevel; - set => this.ILoggerData.LogLevel = value; + get + { + if (!this.Data.Severity.HasValue) + { + return LogLevel.Trace; + } + + uint severity = (uint)this.Data.Severity.Value; + return (LogLevel)((severity - 1) / 4); + } + + set + { + OpenTelemetryLogger.SetLogRecordSeverityFields(ref this.Data, value); + } } /// @@ -247,6 +262,30 @@ public Exception? Exception set => this.ILoggerData.Exception = value; } + /// + /// Gets or sets the original string representation of the severity as it is + /// known at the source. + /// + internal string? SeverityText + { + get => this.Data.SeverityText; + set => this.Data.SeverityText = value; + } + + /// + /// Gets or sets the log . + /// + internal LogRecordSeverity? Severity + { + get => this.Data.Severity; + set => this.Data.Severity = value; + } + + /// + /// Gets or sets the which emitted the . + /// + internal Logger? Logger { get; /*todo: internal*/ set; } + /// /// Executes callback for each currently active scope objects in order /// of creation. All callbacks are guaranteed to be called inline from @@ -378,7 +417,6 @@ internal struct LogRecordILoggerData public string? TraceState; public string? CategoryName; public EventId EventId; - public LogLevel LogLevel; public string? FormattedMessage; public Exception? Exception; public object? State; @@ -392,7 +430,6 @@ public LogRecordILoggerData Copy() TraceState = this.TraceState, CategoryName = this.CategoryName, EventId = this.EventId, - LogLevel = this.LogLevel, FormattedMessage = this.FormattedMessage, Exception = this.Exception, State = this.State, diff --git a/src/OpenTelemetry/Logs/LoggerSdk.cs b/src/OpenTelemetry/Logs/LoggerSdk.cs index 328ad250ace..2db29740a3e 100644 --- a/src/OpenTelemetry/Logs/LoggerSdk.cs +++ b/src/OpenTelemetry/Logs/LoggerSdk.cs @@ -51,7 +51,7 @@ public override void EmitLog(in LogRecordData data, in LogRecordAttributeList at logRecord.Data = data; logRecord.ILoggerData = default; - // TODO: logRecord.Logger = this; + logRecord.Logger = this; logRecord.Attributes = attributes.Export(ref logRecord.AttributeStorage);