Skip to content

Commit

Permalink
Extend logging and verbosity options (#2693)
Browse files Browse the repository at this point in the history
* First stab at logging.
--verbosity d should be the same as in the past
--verbosity n should be pretty quiet

* Make use of Serilog

* improve test specificity

* add log messages about count of processed files

* Use interpolated strings for all logger functions.
Change some logs to be error logs.
  • Loading branch information
dawedawe authored Jan 15, 2023
1 parent 5336c50 commit 1c2d6dd
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 105 deletions.
14 changes: 11 additions & 3 deletions src/Fantomas.Tests/Integration/ConfigTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ open NUnit.Framework
open FsUnit
open Fantomas.Tests.TestHelpers

[<Literal>]
let DetailedVerbosity = "--verbosity d"

[<Literal>]
let NormalVerbosity = "--verbosity n"

[<Test>]
let ``config file in working directory should not require relative prefix, 821`` () =
use fileFixture =
Expand All @@ -21,9 +27,10 @@ indent_size=2
"""
)

let { ExitCode = exitCode; Output = output } = runFantomasTool fileFixture.Filename
let args = sprintf "%s %s" DetailedVerbosity fileFixture.Filename
let { ExitCode = exitCode; Output = output } = runFantomasTool args
exitCode |> should equal 0
output |> should startWith (sprintf "Processing %s" fileFixture.Filename)
output |> should contain (sprintf "Processing %s" fileFixture.Filename)
let result = System.IO.File.ReadAllText(fileFixture.Filename)

result
Expand All @@ -45,7 +52,8 @@ end_of_line=cr
"""
)

let { ExitCode = exitCode; Output = output } = runFantomasTool fileFixture.Filename
let args = sprintf "%s %s" NormalVerbosity fileFixture.Filename
let { ExitCode = exitCode; Output = output } = runFantomasTool args
exitCode |> should equal 1
StringAssert.Contains("Carriage returns are not valid for F# code, please use one of 'lf' or 'crlf'", output)

Expand Down
5 changes: 4 additions & 1 deletion src/Fantomas.Tests/Integration/ForceTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ open NUnit.Framework
open FsUnit
open Fantomas.Tests.TestHelpers

[<Literal>]
let Verbosity = "--verbosity d"

// The day this test fails because Fantomas can format the file, is the day you can remove this file.

[<Test>]
Expand All @@ -17,7 +20,7 @@ let ``code that was invalid should be still be written`` () =
use outputFixture = new OutputFile()

let { ExitCode = exitCode; Output = output } =
runFantomasTool $"--force --out {outputFixture.Filename} {sourceFile}"
runFantomasTool $"{Verbosity} --force --out {outputFixture.Filename} {sourceFile}"

exitCode |> should equal 0
output |> should contain "was not valid after formatting"
Expand Down
22 changes: 14 additions & 8 deletions src/Fantomas.Tests/Integration/IgnoreFilesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ open Fantomas.Tests.TestHelpers
[<Literal>]
let Source = "let foo = 47"

[<Literal>]
let Verbosity = "--verbosity d"

[<Test>]
let ``ignore all fs files`` () =
let fileName = "ToBeIgnored"
Expand All @@ -31,8 +34,8 @@ let ``ignore specific file`` () =
use inputFixture = new TemporaryFileCodeSample(Source, fileName = fileName)

use ignoreFixture = new FantomasIgnoreFile("A.fs")

let { ExitCode = exitCode; Output = output } = runFantomasTool inputFixture.Filename
let args = sprintf "%s %s" Verbosity inputFixture.Filename
let { ExitCode = exitCode; Output = output } = runFantomasTool args
exitCode |> should equal 0

output |> should contain "was ignored"
Expand Down Expand Up @@ -61,8 +64,8 @@ let ``don't ignore other files`` () =
use inputFixture = new TemporaryFileCodeSample(Source, fileName = fileName)

use ignoreFixture = new FantomasIgnoreFile("A.fs")

let { ExitCode = exitCode; Output = output } = runFantomasTool inputFixture.Filename
let args = sprintf "%s %s" Verbosity inputFixture.Filename
let { ExitCode = exitCode; Output = output } = runFantomasTool args
exitCode |> should equal 0

output |> should contain "Processing"
Expand All @@ -79,12 +82,14 @@ let ``ignore file in folder`` () =

use ignoreFixture = new FantomasIgnoreFile("A.fs")

let { ExitCode = exitCode } =
runFantomasTool (sprintf ".%c%s" Path.DirectorySeparatorChar subFolder)
let { ExitCode = exitCode; Output = output } =
runFantomasTool (sprintf "%s .%c%s" Verbosity Path.DirectorySeparatorChar subFolder)

exitCode |> should equal 0
File.ReadAllText inputFixture.Filename |> should equal Source

output |> should contain "Processed files: 0"

[<Test>]
let ``ignore file while checking`` () =
let fileName = "A"
Expand All @@ -94,7 +99,7 @@ let ``ignore file while checking`` () =
use ignoreFixture = new FantomasIgnoreFile("A.fs")

let { ExitCode = exitCode; Output = output } =
sprintf "%s --check" inputFixture.Filename |> runFantomasTool
sprintf "%s %s --check" Verbosity inputFixture.Filename |> runFantomasTool

exitCode |> should equal 0

Expand Down Expand Up @@ -127,6 +132,7 @@ let ``honor ignore file when processing a folder`` () =
use inputFixture = new FantomasIgnoreFile("*.fsx")

let { Output = output } =
runFantomasTool (sprintf ".%c%s" Path.DirectorySeparatorChar subFolder)
runFantomasTool (sprintf "%s .%c%s" Verbosity Path.DirectorySeparatorChar subFolder)

output |> should not' (contain "ignored")
output |> should contain "Processed files: 1"
5 changes: 4 additions & 1 deletion src/Fantomas.Tests/Integration/MultiplePathsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ let UserCode = "let a = 9"
[<Literal>]
let FormattedCode = "let a = 9\n"

[<Literal>]
let Verbosity = "--verbosity d"

let private fileContentMatches (expectedContent: string) (actualPath: string) : unit =
if File.Exists(actualPath) then
let actualContent = File.ReadAllText(actualPath)
Expand Down Expand Up @@ -45,7 +48,7 @@ let ``format multiple paths with recursive flag`` () =
use fileFixtureThree = new TemporaryFileCodeSample(UserCode, subFolder = "sub")

let arguments =
sprintf "\"%s\" \"%s\" \"sub\" -r" fileFixtureOne.Filename fileFixtureTwo.Filename
sprintf "%s \"%s\" \"%s\" \"sub\" -r" Verbosity fileFixtureOne.Filename fileFixtureTwo.Filename

let { ExitCode = exitCode; Output = output } = runFantomasTool arguments

Expand Down
11 changes: 7 additions & 4 deletions src/Fantomas.Tests/Integration/WriteTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ let FormattedCode =
[<Literal>]
let UnformattedCode = "let a = 9"

[<Literal>]
let Verbosity = "--verbosity d"

[<Test>]
let ``correctly formatted file should not be written, 1984`` () =
let fileName = "A"

use inputFixture = new TemporaryFileCodeSample(FormattedCode, fileName = fileName)

let { ExitCode = exitCode; Output = output } = runFantomasTool inputFixture.Filename
let args = sprintf "%s %s" Verbosity inputFixture.Filename
let { ExitCode = exitCode; Output = output } = runFantomasTool args
exitCode |> should equal 0

output |> should contain "was unchanged"
Expand All @@ -28,8 +31,8 @@ let ``incorrectly formatted file should be written`` () =
let fileName = "A"

use inputFixture = new TemporaryFileCodeSample(UnformattedCode, fileName = fileName)

let { ExitCode = exitCode; Output = output } = runFantomasTool inputFixture.Filename
let args = sprintf "%s %s" Verbosity inputFixture.Filename
let { ExitCode = exitCode; Output = output } = runFantomasTool args
exitCode |> should equal 0

output |> should contain "has been written"
26 changes: 10 additions & 16 deletions src/Fantomas.Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,15 @@
},
"Serilog": {
"type": "Transitive",
"resolved": "2.8.0",
"contentHash": "zjuKXW5IQws43IHX7VY9nURsaCiBYh2kyJCWLJRSWrTsx/syBKHV8MibWe2A+QH3Er0AiwA+OJmO3DhFJDY1+A==",
"resolved": "2.12.0",
"contentHash": "xaiJLIdu6rYMKfQMYUZgTy8YK7SMZjB4Yk50C/u//Z4OsvxkUfSPJy4nknfvwAC34yr13q7kcyh4grbwhSxyZg=="
},
"Serilog.Sinks.Console": {
"type": "Transitive",
"resolved": "4.1.0",
"contentHash": "K6N5q+5fetjnJPvCmkWOpJ/V8IEIoMIB1s86OzBrbxwTyHxdx3pmz4H+8+O/Dc/ftUX12DM1aynx/dDowkwzqg==",
"dependencies": {
"System.Collections.NonGeneric": "4.3.0"
"Serilog": "2.10.0"
}
},
"SerilogTraceListener": {
Expand Down Expand Up @@ -397,19 +402,6 @@
"resolved": "5.0.0",
"contentHash": "FXkLXiK0sVVewcso0imKQoOxjoPAj42R8HtjjbSjVPAzwDfzoyoznWxgA3c38LDbN9SJux1xXoXYAhz98j7r2g=="
},
"System.Collections.NonGeneric": {
"type": "Transitive",
"resolved": "4.3.0",
"contentHash": "prtjIEMhGUnQq6RnPEYLpFt8AtLbp9yq2zxOSrY7KJJZrw25Fi97IzBqY7iqssbM61Ek5b8f3MG/sG1N2sN5KA==",
"dependencies": {
"System.Diagnostics.Debug": "4.3.0",
"System.Globalization": "4.3.0",
"System.Resources.ResourceManager": "4.3.0",
"System.Runtime": "4.3.0",
"System.Runtime.Extensions": "4.3.0",
"System.Threading": "4.3.0"
}
},
"System.Configuration.ConfigurationManager": {
"type": "Transitive",
"resolved": "4.4.0",
Expand Down Expand Up @@ -921,6 +913,8 @@
"Fantomas.Client": "[1.0.0, )",
"Fantomas.Core": "[1.0.0, )",
"Ignore": "[0.1.46, )",
"Serilog": "[2.12.0, )",
"Serilog.Sinks.Console": "[4.1.0, )",
"SerilogTraceListener": "[3.2.1-dev-00011, )",
"StreamJsonRpc": "[2.8.28, )",
"System.IO.Abstractions": "[17.2.3, )",
Expand Down
3 changes: 3 additions & 0 deletions src/Fantomas/Fantomas.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<ItemGroup>
<Compile Include="AssemblyInfo.fsi" />
<Compile Include="AssemblyInfo.fs" />
<Compile Include="Logging.fs" />
<Compile Include="IgnoreFile.fsi" />
<Compile Include="IgnoreFile.fs" />
<Compile Include="EditorConfig.fsi" />
Expand All @@ -33,6 +34,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharp.Core" Version="$(FSharpCoreVersion)" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="StreamJsonRpc" Version="$(StreamJsonRpcVersion)" />
<PackageReference Include="Argu" Version="6.1.1" />
<PackageReference Include="Thoth.Json.Net" Version="8.0.0" />
Expand Down
3 changes: 2 additions & 1 deletion src/Fantomas/IgnoreFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Fantomas

open System.IO.Abstractions
open Ignore
open Fantomas.Logging

type AbsoluteFilePath =
private
Expand Down Expand Up @@ -89,5 +90,5 @@ module IgnoreFile =
try
ignoreFile.IsIgnored fullPath
with ex ->
printfn "%A" ex
elog $"%A{ex}"
false
27 changes: 27 additions & 0 deletions src/Fantomas/Logging.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Fantomas.Logging

open Serilog

[<RequireQualifiedAccess>]
type VerbosityLevel =
| Normal
| Detailed

let private logger =
Log.Logger <- LoggerConfiguration().WriteTo.Console().CreateLogger()
Log.Logger

/// log a message
let stdlog (s: string) = logger.Information(s)

/// log an error
let elog (s: string) = logger.Error(s)

/// log a message if the verbosity level is >= Detailed
let logGrEqDetailed verbosity s =
if verbosity = VerbosityLevel.Detailed then
logger.Information(s)
else
()

let closeAndFlushLog () = Log.CloseAndFlush()
Loading

0 comments on commit 1c2d6dd

Please sign in to comment.