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

feat: toggle log severity using rules config #9460

Merged
merged 5 commits into from
Nov 24, 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
14 changes: 14 additions & 0 deletions docs/reference/docfx-json-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ The `docfx.json` file indicates that the directory is the root of a docfx projec
}
```

## Global properties

### `rules`

Overrides default log message severity level. Key is the log code, supported values are `verbose`, `info`, `suggestion`, `warning`, `error`:

```json
{
"rules": {
"InvalidHref": "info"
}
}
```

## `build`

Configuration options that are applied for `docfx build` command:
Expand Down
3 changes: 3 additions & 0 deletions samples/seed/docfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@
"TODO": "alert alert-secondary"
}
}
},
"rules": {
"InvalidCref": "info"
}
}
4 changes: 4 additions & 0 deletions src/Docfx.App/Config/DocfxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#nullable enable

using Docfx.Common;

namespace Docfx;

class DocfxConfig
Expand All @@ -14,4 +16,6 @@ class DocfxConfig
public BuildJsonConfig? build { get; init; }

public PdfJsonConfig? pdf { get; init; }

public Dictionary<string, LogLevel>? rules { get; init; }
}
6 changes: 5 additions & 1 deletion src/Docfx.App/Docset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ internal static (DocfxConfig, string configDirectory) GetConfig(string configFil
if (!File.Exists(configFile))
throw new FileNotFoundException($"Cannot find config file {configFile}");

return (JsonUtility.Deserialize<DocfxConfig>(configFile), Path.GetDirectoryName(configFile));
var config = JsonUtility.Deserialize<DocfxConfig>(configFile);

Logger.Rules = config.rules;

return (config, Path.GetDirectoryName(configFile));
}


Expand Down
2 changes: 2 additions & 0 deletions src/Docfx.Common/Loggers/ConsoleLogListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public void WriteLine(ILogItem item)
{
if (level >= LogLevel.Warning)
message.Append($"{level.ToString().ToLowerInvariant()}: ");
if (!string.IsNullOrEmpty(item.Code))
message.Append($"{item.Code}: ");
}

message.Append(item.Message);
Expand Down
8 changes: 7 additions & 1 deletion src/Docfx.Common/Loggers/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class Logger
private static int _errorCount = 0;
public static volatile LogLevel LogLevelThreshold = LogLevel.Info;
public static volatile bool WarningsAsErrors = false;
public static volatile Dictionary<string, LogLevel> Rules;

public static void RegisterListener(ILoggerListener listener)
{
Expand Down Expand Up @@ -45,8 +46,13 @@ public static void UnregisterAllListeners()
_syncListener.RemoveAllListeners();
}

public static void Log(ILogItem item)
private static void Log(LogItem item)
{
if (Rules is not null && !string.IsNullOrEmpty(item.Code) && Rules.TryGetValue(item.Code, out var level))
{
item.LogLevel = level;
}

if (item.LogLevel < LogLevelThreshold)
{
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Docfx.Dotnet/CompilationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void GetReferenceAssembliesCore(PEFile assembly)
var file = assemblyResolver.FindAssemblyFile(reference);
if (file is null)
{
Logger.LogWarning($"Unable to resolve assembly reference {reference}");
Logger.LogWarning($"Unable to resolve assembly reference {reference}", code: "InvalidAssemblyReference");
continue;
}

Expand Down
2 changes: 0 additions & 2 deletions src/Docfx.Dotnet/DotnetApiCatalog.Toc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class TocNode

private static List<TocNode> CreateToc(List<(IAssemblySymbol symbol, Compilation compilation)> assemblies, ExtractMetadataConfig config, DotnetApiOptions options)
{
Logger.LogWarning($"Markdown output format is experimental.");

Directory.CreateDirectory(config.OutputFolder);

var filter = new SymbolFilter(config, options);
Expand Down
4 changes: 3 additions & 1 deletion src/Docfx.Dotnet/DotnetApiCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public static async Task GenerateManagedReferenceYamlFiles(string configPath, Do
try
{
var configDirectory = Path.GetDirectoryName(Path.GetFullPath(configPath));

var config = JObject.Parse(File.ReadAllText(configPath));
if (config.TryGetValue("metadata", out var value))
{
Logger.Rules = config["rules"]?.ToObject<Dictionary<string, LogLevel>>();
await Exec(value.ToObject<MetadataJsonConfig>(JsonUtility.DefaultSerializer.Value), options, configDirectory);
}
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ string ResolveCode(string source)
var path = Path.GetFullPath(Path.Combine(basePath, source));
if (!File.Exists(path))
{
Logger.LogWarning($"Source file '{path}' not found.");
Logger.LogWarning($"Source file '{path}' not found.", code: "CodeNotFound");
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Docfx.Dotnet/Parsers/XmlComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static XmlComment Parse(string xml, XmlCommentParserContext context = nul
// Quick turnaround for badly formed XML comment
if (xml.StartsWith("<!-- Badly formed XML comment ignored for member ", StringComparison.Ordinal))
{
Logger.LogWarning($"Invalid triple slash comment is ignored: {xml}");
Logger.LogWarning($"Invalid XML comment: {xml}", code: "InvalidXmlComment");
return null;
}
try
Expand Down Expand Up @@ -360,7 +360,7 @@ private void ResolveCrefLink(XNode node, string nodeSelector, Action<string, str
}
}

Logger.Log(LogLevel.Warning, $"Invalid cref value \"{cref}\" found in XML documentation comment {detailedInfo}.");
Logger.Log(LogLevel.Warning, $"Invalid cref value \"{cref}\" found in XML documentation comment {detailedInfo}.", code: "InvalidCref");

if (cref.StartsWith("!:"))
{
Expand Down
5 changes: 0 additions & 5 deletions src/Docfx.MarkdigEngine.Extensions/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ namespace Docfx.MarkdigEngine.Extensions;
internal static class Constants
{
public static readonly string FencedCodePrefix = "lang-";

public static class WarningCodes
{
public const string InvalidTabGroup = "InvalidTabGroup";
}
}