Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use config to control log report level and enrich log message #549

Merged
merged 4 commits into from
Mar 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@

namespace SkyApm.Config
{
public class LogPushSkywalkingConfig
[Config("SkyWalking", "Diagnostics", "Logging")]
public class DiagnosticsLoggingConfig
{
public bool Enable { get; set; }
public LogCollectLevel CollectLevel { get; set; } = LogCollectLevel.Information;
}

public enum LogCollectLevel
{
Trace = 0,
Debug = 1,
Information = 2,
Warning = 3,
Error = 4,
Critical = 5,
None = 6
}
}
22 changes: 18 additions & 4 deletions src/SkyApm.Diagnostics.MSLogging/SkyApmLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.Logging;
using SkyApm.Common;
using SkyApm.Config;
using SkyApm.Tracing;
using SkyApm.Tracing.Segments;
using SkyApm.Transport;
Expand All @@ -32,34 +34,46 @@ public class SkyApmLogger : ILogger
private readonly ISkyApmLogDispatcher _skyApmLogDispatcher;
private readonly ISegmentContextAccessor _segmentContextAccessor;
private readonly IEntrySegmentContextAccessor _entrySegmentContextAccessor;
private readonly TracingConfig _tracingConfig;
private readonly DiagnosticsLoggingConfig _logCollectorConfig;

public SkyApmLogger(string categoryName, ISkyApmLogDispatcher skyApmLogDispatcher,
ISegmentContextAccessor segmentContextAccessor,
IEntrySegmentContextAccessor entrySegmentContextAccessor)
IEntrySegmentContextAccessor entrySegmentContextAccessor,
IConfigAccessor configAccessor)
{
_categoryName = categoryName;
_skyApmLogDispatcher = skyApmLogDispatcher;
_segmentContextAccessor = segmentContextAccessor;
_entrySegmentContextAccessor = entrySegmentContextAccessor;
_tracingConfig = configAccessor.Get<TracingConfig>();
_logCollectorConfig = configAccessor.Get<DiagnosticsLoggingConfig>();
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel)) return;

var tags = new Dictionary<string, object>
{
{ "logger", _categoryName },
{ "Level", logLevel },
{ "level", logLevel },
{ "thread", Thread.CurrentThread.ManagedThreadId },
};
if (exception != null)
{
tags["errorType"] = exception.GetType().ToString();
}
var message = state.ToString();
if (exception != null)
{
message += "\r\n" + (exception.HasInnerExceptions() ? exception.ToDemystifiedString(_tracingConfig.ExceptionMaxDepth) : exception.ToString());
}
SegmentContext segmentContext = _segmentContextAccessor.Context;
var logContext = new LoggerRequest()
{
Message = state.ToString() ?? string.Empty,
Message = message ?? string.Empty,
Tags = tags,
SegmentReference = segmentContext == null
? null
Expand All @@ -76,7 +90,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
_skyApmLogDispatcher.Dispatch(logContext);
}

public bool IsEnabled(LogLevel logLevel) => true;
public bool IsEnabled(LogLevel logLevel) => (int)logLevel >= (int)_logCollectorConfig.CollectLevel;

public IDisposable BeginScope<TState>(TState state) => default!;
}
Expand Down
8 changes: 6 additions & 2 deletions src/SkyApm.Diagnostics.MSLogging/SkyApmLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using SkyApm.Config;
using SkyApm.Tracing;
using SkyApm.Transport;

Expand All @@ -29,14 +30,17 @@ public class SkyApmLoggerProvider : ILoggerProvider
private readonly ISkyApmLogDispatcher _skyApmLogDispatcher;
private readonly ISegmentContextAccessor _segmentContextAccessor;
private readonly IEntrySegmentContextAccessor _entrySegmentContextAccessor;
private readonly IConfigAccessor _configAccessor;

public SkyApmLoggerProvider(ISkyApmLogDispatcher skyApmLogDispatcher,
ISegmentContextAccessor segmentContextAccessor,
IEntrySegmentContextAccessor entrySegmentContextAccessor)
IEntrySegmentContextAccessor entrySegmentContextAccessor,
IConfigAccessor configAccessor)
{
_skyApmLogDispatcher = skyApmLogDispatcher;
_segmentContextAccessor = segmentContextAccessor;
_entrySegmentContextAccessor = entrySegmentContextAccessor;
_configAccessor = configAccessor;
}

public void Dispose()
Expand All @@ -46,7 +50,7 @@ public void Dispose()
public ILogger CreateLogger(string categoryName)
{
return _doveLoggers.GetOrAdd(categoryName,
_ => new SkyApmLogger(categoryName, _skyApmLogDispatcher, _segmentContextAccessor, _entrySegmentContextAccessor));
_ => new SkyApmLogger(categoryName, _skyApmLogDispatcher, _segmentContextAccessor, _entrySegmentContextAccessor, _configAccessor));
}
}
}