Skip to content

Commit

Permalink
[sdk-logs] Define LoggerSdk (#4455)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Apr 28, 2023
1 parent f772e93 commit 3884bee
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/OpenTelemetry/Logs/LoggerProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ public bool ContainsBatchProcessor(BaseProcessor<LogRecord> processor)
/// <inheritdoc />
protected override bool TryCreateLogger(string? name, out Logger? logger)
{
// TODO: return new LoggerSdk(this, name);
throw new NotImplementedException();
logger = new LoggerSdk(this, name);
return true;
}

/// <inheritdoc/>
Expand Down
65 changes: 65 additions & 0 deletions src/OpenTelemetry/Logs/LoggerSdk.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// <copyright file="LoggerSdk.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 OpenTelemetry.Internal;

namespace OpenTelemetry.Logs;

/// <summary>
/// SDK <see cref="Logger"/> implementation.
/// </summary>
internal sealed class LoggerSdk : Logger
{
private readonly LoggerProviderSdk loggerProvider;

public LoggerSdk(
LoggerProviderSdk loggerProvider,
string? name)
: base(name)
{
Guard.ThrowIfNull(loggerProvider);

this.loggerProvider = loggerProvider;
}

/// <inheritdoc />
public override void EmitLog(in LogRecordData data, in LogRecordAttributeList attributes = default)
{
var provider = this.loggerProvider;
var processor = provider.Processor;
if (processor != null)
{
var pool = provider.LogRecordPool;

var logRecord = pool.Rent();

logRecord.Data = data;
logRecord.ILoggerData = default;

// TODO: logRecord.Logger = this;

logRecord.Attributes = attributes.Export(ref logRecord.AttributeStorage);

processor.OnEnd(logRecord);

// Attempt to return the LogRecord to the pool. This will no-op
// if a batch exporter has added a reference.
pool.Return(logRecord);
}
}
}

0 comments on commit 3884bee

Please sign in to comment.