-
Notifications
You must be signed in to change notification settings - Fork 79
Add --log-for human|machine flag for LLM/agent-friendly output #1508
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
Open
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/add-log-for-flag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
60d59af
Initial plan
Copilot 4f38ff5
Add --log-for human|machine flag for LLM/agent-friendly output
Copilot 4b00ab7
Fix code review feedback: add token count check and remove unnecessar…
Copilot 1859143
Merge branch 'main' into copilot/add-log-for-flag
waldekmastykarz dcdc8c7
Update DevProxy/Logging/ProxyConsoleFormatterOptions.cs
waldekmastykarz 9bb0978
Merge changes from main: add IsJwtCommand and IsRootCommand properties
Copilot d029123
Merge branch 'main' into copilot/add-log-for-flag - resolve conflicts
waldekmastykarz 1b5763b
Improve error handling in CsomParser and adjust logging conditions in…
waldekmastykarz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using DevProxy.Abstractions.Proxy; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using Microsoft.Extensions.Logging.Console; | ||
| using Microsoft.Extensions.Options; | ||
| using System.Globalization; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace DevProxy.Logging; | ||
|
|
||
| sealed class MachineConsoleFormatter : ConsoleFormatter | ||
| { | ||
| public const string FormatterName = "devproxy-machine"; | ||
|
|
||
| private static readonly JsonSerializerOptions _jsonOptions = new() | ||
| { | ||
| PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
| DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull | ||
| }; | ||
|
|
||
| // Mapping from MessageType to semantic type strings for machine output | ||
| private static readonly Dictionary<MessageType, string> _messageTypeStrings = new() | ||
| { | ||
| [MessageType.InterceptedRequest] = "request", | ||
| [MessageType.InterceptedResponse] = "response", | ||
| [MessageType.PassedThrough] = "passthrough", | ||
| [MessageType.Chaos] = "chaos", | ||
| [MessageType.Warning] = "warning", | ||
| [MessageType.Mocked] = "mock", | ||
| [MessageType.Failed] = "error", | ||
| [MessageType.Tip] = "tip", | ||
| [MessageType.Skipped] = "skip", | ||
| [MessageType.Processed] = "processed", | ||
| [MessageType.Timestamp] = "timestamp", | ||
| [MessageType.FinishedProcessingRequest] = "finished", | ||
| [MessageType.Normal] = "info" | ||
| }; | ||
|
|
||
| // Mapping from LogLevel to semantic level strings for machine output | ||
| private static readonly Dictionary<LogLevel, string> _logLevelStrings = new() | ||
| { | ||
| [LogLevel.Trace] = "trace", | ||
| [LogLevel.Debug] = "debug", | ||
| [LogLevel.Information] = "info", | ||
| [LogLevel.Warning] = "warning", | ||
| [LogLevel.Error] = "error", | ||
| [LogLevel.Critical] = "critical" | ||
| }; | ||
|
|
||
| private readonly ProxyConsoleFormatterOptions _options; | ||
| private readonly HashSet<MessageType> _filteredMessageTypes; | ||
|
|
||
| public MachineConsoleFormatter(IOptions<ProxyConsoleFormatterOptions> options) : base(FormatterName) | ||
| { | ||
| Console.OutputEncoding = Encoding.UTF8; | ||
| _options = options.Value; | ||
|
|
||
| _filteredMessageTypes = [MessageType.InterceptedResponse, MessageType.FinishedProcessingRequest]; | ||
| if (!_options.ShowSkipMessages) | ||
| { | ||
| _ = _filteredMessageTypes.Add(MessageType.Skipped); | ||
| } | ||
| if (!_options.ShowTimestamps) | ||
| { | ||
| _ = _filteredMessageTypes.Add(MessageType.Timestamp); | ||
| } | ||
| } | ||
|
|
||
| public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter) | ||
| { | ||
| if (logEntry.State is RequestLog requestLog) | ||
| { | ||
| WriteRequestLog(requestLog, logEntry.Category, scopeProvider, textWriter); | ||
| } | ||
| else | ||
| { | ||
| WriteRegularLogMessage(logEntry, scopeProvider, textWriter); | ||
| } | ||
| } | ||
|
|
||
| private void WriteRequestLog(RequestLog requestLog, string category, IExternalScopeProvider? scopeProvider, TextWriter textWriter) | ||
| { | ||
| var messageType = requestLog.MessageType; | ||
|
|
||
| if (_filteredMessageTypes.Contains(messageType)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var requestId = GetRequestIdScope(scopeProvider); | ||
| var pluginName = category == ProxyConsoleFormatter.DefaultCategoryName ? null : category; | ||
|
|
||
| // Extract short plugin name from full category | ||
| if (pluginName is not null) | ||
| { | ||
| pluginName = pluginName[(pluginName.LastIndexOf('.') + 1)..]; | ||
| } | ||
|
|
||
| var logObject = new MachineRequestLogEntry | ||
| { | ||
| Type = GetMessageTypeString(messageType), | ||
| Message = requestLog.Message, | ||
| Method = requestLog.Method, | ||
| Url = requestLog.Url, | ||
| Plugin = pluginName, | ||
| RequestId = requestId?.ToString(CultureInfo.InvariantCulture), | ||
| Timestamp = DateTime.UtcNow.ToString("O", CultureInfo.InvariantCulture) | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(logObject, _jsonOptions); | ||
| textWriter.WriteLine(json); | ||
| } | ||
|
|
||
| private static void WriteRegularLogMessage<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter) | ||
| { | ||
| var message = logEntry.Formatter(logEntry.State, logEntry.Exception); | ||
| if (string.IsNullOrEmpty(message)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var requestId = GetRequestIdScope(scopeProvider); | ||
| var category = logEntry.Category; | ||
|
|
||
| // Extract short category name | ||
| if (category is not null) | ||
| { | ||
| category = category[(category.LastIndexOf('.') + 1)..]; | ||
| } | ||
|
|
||
| var logObject = new MachineLogEntry | ||
| { | ||
| Type = "log", | ||
| Level = GetLogLevelString(logEntry.LogLevel), | ||
| Message = message, | ||
| Category = category, | ||
| RequestId = requestId?.ToString(CultureInfo.InvariantCulture), | ||
| Timestamp = DateTime.UtcNow.ToString("O", CultureInfo.InvariantCulture), | ||
| Exception = logEntry.Exception?.ToString() | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(logObject, _jsonOptions); | ||
| textWriter.WriteLine(json); | ||
| } | ||
|
|
||
| private static string GetMessageTypeString(MessageType messageType) => | ||
| _messageTypeStrings.TryGetValue(messageType, out var str) ? str : "unknown"; | ||
|
|
||
| private static string GetLogLevelString(LogLevel logLevel) => | ||
| _logLevelStrings.TryGetValue(logLevel, out var str) ? str : "unknown"; | ||
|
|
||
| private static int? GetRequestIdScope(IExternalScopeProvider? scopeProvider) | ||
| { | ||
| int? requestId = null; | ||
| scopeProvider?.ForEachScope((scope, _) => | ||
| { | ||
| if (scope is Dictionary<string, object> dictionary && | ||
| dictionary.TryGetValue(nameof(requestId), out var req)) | ||
| { | ||
| requestId = (int)req; | ||
| } | ||
| }, ""); | ||
| return requestId; | ||
| } | ||
|
|
||
| // JSON serialization models for machine output | ||
| private sealed class MachineRequestLogEntry | ||
| { | ||
| public string? Type { get; set; } | ||
| public string? Message { get; set; } | ||
| public string? Method { get; set; } | ||
| public string? Url { get; set; } | ||
| public string? Plugin { get; set; } | ||
| public string? RequestId { get; set; } | ||
| public string? Timestamp { get; set; } | ||
| } | ||
|
|
||
| private sealed class MachineLogEntry | ||
| { | ||
| public string? Type { get; set; } | ||
| public string? Level { get; set; } | ||
| public string? Message { get; set; } | ||
| public string? Category { get; set; } | ||
| public string? RequestId { get; set; } | ||
| public string? Timestamp { get; set; } | ||
| public string? Exception { get; set; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.