[sdk-logs] Define LoggerSdk (#4455)

This commit is contained in:
Mikel Blanchard 2023-04-28 14:42:50 -07:00 committed by GitHub
parent f772e9321c
commit 3884bee5f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 2 deletions

View File

@ -202,8 +202,8 @@ internal sealed class LoggerProviderSdk : LoggerProvider
/// <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/>

View File

@ -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);
}
}
}