Skip to content

Commit

Permalink
Use Logging from Microsoft.Extensions.Logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Dec 20, 2023
1 parent 0be1719 commit 754b2ed
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 57 deletions.
55 changes: 42 additions & 13 deletions src/Fable.Cli/Entry.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open System
open Main
open Fable
open Fable.Compiler.Util
open Microsoft.Extensions.Logging

type CliArgs(args: string list) =
let argsMap =
Expand Down Expand Up @@ -249,6 +250,7 @@ type Runner =
language: Language,
rootDir: string,
runProc: RunProcess option,
verbosity: Fable.Verbosity,
?fsprojPath: string,
?watch,
?precompile
Expand Down Expand Up @@ -355,13 +357,6 @@ type Runner =
else
Ok()

let verbosity =
if args.FlagEnabled "--verbose" then
Log.makeVerbose ()
Verbosity.Verbose
else
Verbosity.Normal

let configuration =
let defaultConfiguration =
if watch then
Expand Down Expand Up @@ -603,12 +598,25 @@ let main argv =
| Some rootDir -> File.getExactFullPath rootDir
| None -> IO.Directory.GetCurrentDirectory()

do
let verbosity =
match commands with
| [ "--version" ] -> ()
| [ "--version" ] -> Verbosity.Normal
| _ ->
if args.FlagEnabled "--verbose" then
Log.makeVerbose ()
let verbosity =
let level, verbosity =
if args.FlagEnabled "--verbose" then
LogLevel.Debug, Fable.Verbosity.Verbose
else
LogLevel.Information, Fable.Verbosity.Normal

use factory =
LoggerFactory.Create(fun builder ->
builder.SetMinimumLevel(level).AddConsole()
|> ignore
)

Log.setLogger (factory.CreateLogger(""))
verbosity

let status =
match getStatus language with
Expand Down Expand Up @@ -636,6 +644,8 @@ let main argv =
+ "\n"
)

verbosity

match commands with
| [ "--help" ] -> return printHelp ()
| [ "--version" ] -> return Log.always Literals.VERSION
Expand All @@ -648,31 +658,49 @@ let main argv =
language,
rootDir,
runProc,
verbosity,
fsprojPath = path,
watch = true
)
| [ "watch" ] ->
return! Runner.Run(args, language, rootDir, runProc, watch = true)
return!
Runner.Run(
args,
language,
rootDir,
runProc,
verbosity,
watch = true
)
| [ "precompile"; path ] ->
return!
Runner.Run(
args,
language,
rootDir,
runProc,
verbosity,
fsprojPath = path,
precompile = true
)
| [ "precompile" ] ->
return!
Runner.Run(args, language, rootDir, runProc, precompile = true)
Runner.Run(
args,
language,
rootDir,
runProc,
verbosity,
precompile = true
)
| [ path ] ->
return!
Runner.Run(
args,
language,
rootDir,
runProc,
verbosity,
fsprojPath = path,
watch = args.FlagEnabled("--watch")
)
Expand All @@ -683,6 +711,7 @@ let main argv =
language,
rootDir,
runProc,
verbosity,
watch = args.FlagEnabled("--watch")
)
| _ ->
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Cli/Fable.Cli.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<ItemGroup>
<PackageReference Include="Buildalyzer" Version="5.0.1" />
<PackageReference Include="FSharp.SystemTextJson" Version="1.2.42" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="source-map-sharp" Version="1.0.9" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions src/Fable.Compiler/Fable.Compiler.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<ItemGroup>
<PackageReference Include="FSharp.SystemTextJson" Version="1.2.42" />
<PackageReference Include="Buildalyzer" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 14 additions & 40 deletions src/Fable.Compiler/Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

open System
open System.Threading
open Microsoft.Extensions.Logging
open Microsoft.Extensions.Logging.Abstractions

type RunProcess(exeFile: string, args: string list, ?watch: bool, ?fast: bool) =
member _.ExeFile = exeFile
Expand Down Expand Up @@ -82,29 +84,20 @@ type Agent<'T>

[<RequireQualifiedAccess>]
module Log =
let mutable logger: ILogger = NullLogger.Instance
let setLogger newLogger = logger <- newLogger
let newLine = Environment.NewLine

let isCi =
String.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI")) |> not

let mutable private verbosity = Fable.Verbosity.Normal

/// To be called only at the beginning of the app
let makeVerbose () = verbosity <- Fable.Verbosity.Verbose

let makeSilent () = verbosity <- Fable.Verbosity.Silent

let isVerbose () = verbosity = Fable.Verbosity.Verbose

let canLog msg =
verbosity <> Fable.Verbosity.Silent && not (String.IsNullOrEmpty(msg))

let inSameLineIfNotCI (msg: string) =
match verbosity with
| Fable.Verbosity.Silent -> ()
| Fable.Verbosity.Verbose -> Console.Out.WriteLine(msg)
| Fable.Verbosity.Normal ->
// Avoid log pollution in CI. Also, if output is redirected don't try to rewrite
| Fable.Verbosity.Verbose -> logger.LogDebug msg
| Fable.Verbosity.Normal -> logger.LogInformation msg

(* TODO: move this check to Fable.Cli?
// Avoid log pollution in CI. Also, if output is redirected don't try to rewrite
// the same line as it seems to cause problems, see #2727
if not isCi && not Console.IsOutputRedirected then
// If the message is longer than the terminal width it will jump to next line
Expand All @@ -122,36 +115,17 @@ module Log =
if diff > 0 then
Console.Out.Write(String.replicate diff " ")
Console.SetCursorPosition(msg.Length, Console.CursorTop)
*)

let alwaysWithColor color (msg: string) =
if canLog msg then
Console.ForegroundColor <- color
Console.Out.WriteLine(msg)
Console.ResetColor()

let always (msg: string) =
if canLog msg then
Console.Out.WriteLine(msg)
let always (msg: string) = logger.LogInformation msg

let verbose (msg: Lazy<string>) =
if isVerbose () then
always msg.Value

let verboseOrIf condition (msg: string) =
if canLog msg && (condition || verbosity = Fable.Verbosity.Verbose) then
always msg

let warning (msg: string) =
if canLog msg then
Console.ForegroundColor <- ConsoleColor.DarkYellow
Console.Out.WriteLine(msg)
Console.ResetColor()
let warning (msg: string) = logger.LogWarning msg

let error (msg: string) =
if canLog msg then
Console.ForegroundColor <- ConsoleColor.DarkRed
Console.Error.WriteLine(msg)
Console.ResetColor()
let error (msg: string) = logger.LogError msg

let mutable private femtoMsgShown = false

Expand All @@ -161,7 +135,7 @@ module Log =
femtoMsgShown <- true

"Some Nuget packages contain information about NPM dependencies that can be managed by Femto: https://github.com/Zaid-Ajaj/Femto"
|> alwaysWithColor ConsoleColor.Blue
|> logger.LogInformation

module File =
open System.IO
Expand Down
8 changes: 5 additions & 3 deletions src/Fable.Compiler/Util.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ type CliArgs =

[<RequireQualifiedAccess>]
module Log =
val newLine: string
open Microsoft.Extensions.Logging

/// To be called only at the beginning of the app
val makeVerbose: unit -> unit
val makeSilent: unit -> unit
val setLogger: ILogger -> unit

val newLine: string
val inSameLineIfNotCI: msg: string -> unit
val always: msg: string -> unit
val verbose: msg: Lazy<string> -> unit
Expand Down
1 change: 0 additions & 1 deletion tests/Integration/Compiler/Util/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module Compiler =
let sourceFile = IO.Path.Join(projDir, "Program.fs" ) |> Path.normalizeFullPath

let cliArgs =
Log.makeSilent()
let compilerOptions = CompilerOptionsHelper.Make()
{ CliArgs.ProjectFile = projFile
FableLibraryPath = None
Expand Down

0 comments on commit 754b2ed

Please sign in to comment.