Skip to content

Commit

Permalink
Use config to control log report level and enrich log message (#549)
Browse files Browse the repository at this point in the history
* Append Exception dedetail into message if exception is not null.

* We can use SkyWalking::LogCollector::Level to control log report level.

* Rename log level tag name from Level to level, same as java.

* Change log collect configuration section from SkyWalking::LogCollector to SkyWalking::Diagnostics::Logging
  • Loading branch information
inversionhourglass committed Mar 19, 2023
1 parent 466754f commit 606a14d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
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));
}
}
}

0 comments on commit 606a14d

Please sign in to comment.