Skip to content

Commit

Permalink
Merge pull request #175 from dawedawe/use_ilogger
Browse files Browse the repository at this point in the history
Use `Microsoft.Extensions.Logging` instead of `printf` based logging infrastructure
  • Loading branch information
dawedawe authored Dec 18, 2023
2 parents 112fa6c + a6b77ab commit bc1d05d
Show file tree
Hide file tree
Showing 21 changed files with 237 additions and 121 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Changed
* [Add path to ASTCollecting](https://github.com/ionide/FSharp.Analyzers.SDK/pull/171) (thanks @nojaf!)
* [Use Microsoft.Extensions.Logging instead of printf based logging infrastructure](https://github.com/ionide/FSharp.Analyzers.SDK/pull/175) (thanks @dawedawe!)

## [0.21.0] - 2023-11-22

Expand Down
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<PackageVersion Include="Microsoft.Build.Locator" Version="1.4.1" />
<!-- Need to update Directory.Build.props DotNet.ReproducibleBuilds.Isolated version when updating this-->
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.1.1" PrivateAssets="All" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="MSBuild.StructuredLogger" Version="2.1.815" />
<PackageVersion Include="NUnit" Version="3.13.3" />
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ F# analyzers are live, real-time, project based plugins that enables to diagnose
2. Run the console application:

```shell
dotnet run --project src\FSharp.Analyzers.Cli\FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbose
dotnet run --project src\FSharp.Analyzers.Cli\FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbosity d
```

You can also set up a run configuration of FSharp.Analyzers.Cli in your favorite IDE using similar arguments. This also allows you to debug FSharp.Analyzers.Cli.
Expand Down
2 changes: 1 addition & 1 deletion build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pipeline "Build" {
}
stage "sample" {
run
"dotnet run --project src/FSharp.Analyzers.Cli/FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbose"
"dotnet run --project src/FSharp.Analyzers.Cli/FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbosity d"
}
stage "docs" { run "dotnet fsdocs build --properties Configuration=Release --eval --clean --strict" }
runIfOnlySpecified false
Expand Down
6 changes: 3 additions & 3 deletions docs/content/Getting Started Using.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ At the time of writing, the [G-Research analyzers](https://github.com/g-research
With the package downloaded, we can run the CLI tool:

```shell
dotnet fsharp-analyzers --project ./YourProject.fsproj --analyzers-path C:\Users\yourusername\.nuget\packages\g-research.fsharp.analyzers\0.4.0\analyzers\dotnet\fs\ --verbose
dotnet fsharp-analyzers --project ./YourProject.fsproj --analyzers-path C:\Users\yourusername\.nuget\packages\g-research.fsharp.analyzers\0.4.0\analyzers\dotnet\fs\ --verbosity d
```

### Using an MSBuild target
Expand All @@ -57,7 +57,7 @@ Before we can run `dotnet msbuild /t:AnalyzeFSharpProject`, we need to specify o

```xml
<PropertyGroup>
<FSharpAnalyzersOtherFlags>--analyzers-path &quot;$(PkgG-Research_FSharp_Analyzers)/analyzers/dotnet/fs&quot; --report &quot;$(MSBuildProjectName)-$(TargetFramework).sarif&quot; --treat-as-warning IONIDE-004 --verbose</FSharpAnalyzersOtherFlags>
<FSharpAnalyzersOtherFlags>--analyzers-path &quot;$(PkgG-Research_FSharp_Analyzers)/analyzers/dotnet/fs&quot; --report &quot;$(MSBuildProjectName)-$(TargetFramework).sarif&quot; --treat-as-warning IONIDE-004 --verbosity d</FSharpAnalyzersOtherFlags>
</PropertyGroup>
```

Expand Down Expand Up @@ -101,7 +101,7 @@ This is effectively the same as adding a property to each `*proj` file which exi
<PropertyGroup>
<SarifOutput Condition="$(SarifOutput) == ''">./</SarifOutput>
<CodeRoot Condition="$(CodeRoot) == ''">.</CodeRoot>
<FSharpAnalyzersOtherFlags>--analyzers-path &quot;$(PkgG-Research_FSharp_Analyzers)/analyzers/dotnet/fs&quot; --report &quot;$(SarifOutput)$(MSBuildProjectName)-$(TargetFramework).sarif&quot; --code-root $(CodeRoot) --treat-as-warning IONIDE-004 --verbose</FSharpAnalyzersOtherFlags>
<FSharpAnalyzersOtherFlags>--analyzers-path &quot;$(PkgG-Research_FSharp_Analyzers)/analyzers/dotnet/fs&quot; --report &quot;$(SarifOutput)$(MSBuildProjectName)-$(TargetFramework).sarif&quot; --code-root $(CodeRoot) --treat-as-warning IONIDE-004 --verbosity d</FSharpAnalyzersOtherFlags>
</PropertyGroup>
</Project>
```
Expand Down
2 changes: 1 addition & 1 deletion docs/content/Getting Started Writing.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ dotnet tool install --global fsharp-analyzers
```
```shell
fsharp-analyzers --project YourProject.fsproj --analyzers-path ./OptionAnalyzer/bin/Release --verbose
fsharp-analyzers --project YourProject.fsproj --analyzers-path ./OptionAnalyzer/bin/Release --verbosity d
```
### Packaging and Distribution
Expand Down
1 change: 1 addition & 0 deletions docs/content/Programmatic access.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The `Client` needs to know what type of analyzer you intend to load: *console* o
(*** hide ***)
#r "../../src/FSharp.Analyzers.Cli/bin/Release/net6.0/FSharp.Analyzers.SDK.dll"
#r "../../src/FSharp.Analyzers.Cli/bin/Release/net6.0/FSharp.Compiler.Service.dll"
#r "../../src/FSharp.Analyzers.Cli/bin/Release/net6.0/Microsoft.Extensions.Logging.Abstractions.dll"
(** *)

open FSharp.Analyzers.SDK
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ F# analyzers are live, real-time, project based plugins that enables to diagnose
2. Run the console application:

```shell
dotnet run --project src\FSharp.Analyzers.Cli\FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbose
dotnet run --project src\FSharp.Analyzers.Cli\FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbosity d
```

You can also set up a run configuration of FSharp.Analyzers.Cli in your favorite IDE using similar arguments. This also allows you to debug FSharp.Analyzers.Cli.
Expand Down
80 changes: 80 additions & 0 deletions src/FSharp.Analyzers.Cli/CustomLogging.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module FSharp.Analyzers.Cli.CustomLogging

open System
open System.IO
open System.Runtime.CompilerServices
open Microsoft.Extensions.Logging
open Microsoft.Extensions.Logging.Console
open Microsoft.Extensions.Logging.Abstractions
open Microsoft.Extensions.Options

type CustomOptions() =
inherit ConsoleFormatterOptions()

/// if true: no LogLevel as prefix, colored output according to LogLevel
/// if false: LogLevel as prefix, no colored output
member val UseAnalyzersMsgStyle = false with get, set

type CustomFormatter(options: IOptionsMonitor<CustomOptions>) as this =
inherit ConsoleFormatter("customName")

let mutable optionsReloadToken: IDisposable = null
let mutable formatterOptions = options.CurrentValue
let origColor = Console.ForegroundColor

do optionsReloadToken <- options.OnChange(fun x -> this.ReloadLoggerOptions(x))

member private _.ReloadLoggerOptions(opts: CustomOptions) = formatterOptions <- opts

override this.Write<'TState>
(
logEntry: inref<LogEntry<'TState>>,
_scopeProvider: IExternalScopeProvider,
textWriter: TextWriter
)
=
let message = logEntry.Formatter.Invoke(logEntry.State, logEntry.Exception)

if formatterOptions.UseAnalyzersMsgStyle then
this.SetColor(textWriter, logEntry.LogLevel)
textWriter.WriteLine(message)
this.ResetColor()
else
this.WritePrefix(textWriter, logEntry.LogLevel)
textWriter.WriteLine(message)

member private _.WritePrefix(textWriter: TextWriter, logLevel: LogLevel) =
match logLevel with
| LogLevel.Trace -> textWriter.Write("trace: ")
| LogLevel.Debug -> textWriter.Write("debug: ")
| LogLevel.Information -> textWriter.Write("info: ")
| LogLevel.Warning -> textWriter.Write("warn: ")
| LogLevel.Error -> textWriter.Write("error: ")
| LogLevel.Critical -> textWriter.Write("critical: ")
| _ -> ()

// see https://learn.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter
member private _.SetColor(textWriter: TextWriter, logLevel: LogLevel) =
let color =
match logLevel with
| LogLevel.Error -> "\x1B[1m\x1B[31m" // ConsoleColor.Red
| LogLevel.Warning -> "\x1B[33m" // ConsoleColor.DarkYellow
| LogLevel.Information -> "\x1B[1m\x1B[34m" // ConsoleColor.Blue
| LogLevel.Trace -> "\x1B[1m\x1B[36m" // ConsoleColor.Cyan
| _ -> "\x1B[37m" // ConsoleColor.Gray

textWriter.Write(color)

member private _.ResetColor() = Console.ForegroundColor <- origColor

interface IDisposable with
member _.Dispose() = optionsReloadToken.Dispose()

[<Extension>]
type ConsoleLoggerExtensions =

[<Extension>]
static member AddCustomFormatter(builder: ILoggingBuilder, configure: Action<CustomOptions>) : ILoggingBuilder =
builder
.AddConsole(fun options -> options.FormatterName <- "customName")
.AddConsoleFormatter<CustomFormatter, CustomOptions>(configure)
2 changes: 2 additions & 0 deletions src/FSharp.Analyzers.Cli/FSharp.Analyzers.Cli.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="CustomLogging.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

Expand All @@ -26,6 +27,7 @@
<PackageReference Include="Microsoft.Build.Locator" />
<PackageReference Include="Microsoft.Build.Tasks.Core" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Sarif.Sdk" />
</ItemGroup>

Expand Down
Loading

0 comments on commit bc1d05d

Please sign in to comment.