Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 31 additions & 1 deletion src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -66,7 +77,6 @@ public void Log<TState>(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;
Expand All @@ -75,6 +85,8 @@ public void Log<TState>(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
Expand All @@ -96,6 +108,9 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
: null;
}

// TODO: Set this to a real instance.
record.Logger = null;

processor.OnEnd(record);

iloggerData.ScopeProvider = null;
Expand All @@ -114,6 +129,21 @@ public bool IsEnabled(LogLevel logLevel)

public IDisposable BeginScope<TState>(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<KeyValuePair<string, object?>>? ProcessState<TState>(
LogRecord logRecord,
ref LogRecord.LogRecordILoggerData iLoggerData,
Expand Down
47 changes: 42 additions & 5 deletions src/OpenTelemetry/Logs/LogRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -156,10 +157,24 @@ public string? CategoryName
/// <summary>
/// Gets or sets the log <see cref="Microsoft.Extensions.Logging.LogLevel"/>.
/// </summary>
// 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);
}
}

/// <summary>
Expand Down Expand Up @@ -247,6 +262,30 @@ public Exception? Exception
set => this.ILoggerData.Exception = value;
}

/// <summary>
/// Gets or sets the original string representation of the severity as it is
/// known at the source.
/// </summary>
internal string? SeverityText
{
get => this.Data.SeverityText;
set => this.Data.SeverityText = value;
}

/// <summary>
/// Gets or sets the log <see cref="LogRecordSeverity"/>.
/// </summary>
internal LogRecordSeverity? Severity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nullable or just Unspecified?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like Nullable is more the .NET way for saying a value type is unspecified but originally I didn't have LogRecordSeverity.Unspecified defined 🤣 If you think it would be better to just default to that and drop Nullable I'm good with it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok leaving this for now. Good question though.. I think we should opt for one or the other: nullable or Unspecified. I think I'll open an issue where we can capture a punch list of small questions like this for us to circle back to before we declare stable.

{
get => this.Data.Severity;
set => this.Data.Severity = value;
}

/// <summary>
/// Gets or sets the <see cref="Logs.Logger"/> which emitted the <see cref="LogRecord"/>.
/// </summary>
internal Logger? Logger { get; /*todo: internal*/ set; }

/// <summary>
/// Executes callback for each currently active scope objects in order
/// of creation. All callbacks are guaranteed to be called inline from
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Logs/LoggerSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down