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

Added custom console logger #3709

Merged
merged 1 commit into from
Jan 23, 2024
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
92 changes: 92 additions & 0 deletions src/Fable.Cli/CustomLogging.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Source: https://github.com/ionide/FSharp.Analyzers.SDK/blob/main/src/FSharp.Analyzers.Cli/CustomLogging.fs
module Fable.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 UseNoPrefixMsgStyle = 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.UseNoPrefixMsgStyle 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[31m" // ConsoleColor.DarkRed
| LogLevel.Warning -> "\x1B[33m" // ConsoleColor.DarkYellow
| LogLevel.Information -> "\x1B[37m" // ConsoleColor.Gray
| LogLevel.Trace -> "\x1B[1m\x1B[36m" // ConsoleColor.Cyan
| _ -> "\x1B[39m\x1B[22m" // default foreground color

textWriter.Write(color)

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

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

[<Extension>]
type ConsoleLoggerExtensions =

[<Extension>]
static member AddCustomConsole
(
builder: ILoggingBuilder,
configure: Action<CustomOptions>
)
: ILoggingBuilder
=
builder
.AddConsole(fun options -> options.FormatterName <- "customName")
.AddConsoleFormatter<CustomFormatter, CustomOptions>(configure)
5 changes: 3 additions & 2 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 Fable.Cli.CustomLogging
open Microsoft.Extensions.Logging

type CliArgs(args: string list) =
Expand Down Expand Up @@ -614,8 +615,8 @@ let main argv =
LoggerFactory.Create(fun builder ->
builder
.SetMinimumLevel(level)
.AddSimpleConsole(fun options ->
options.SingleLine <- true
.AddCustomConsole(fun options ->
options.UseNoPrefixMsgStyle <- true
)
|> ignore
)
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 @@ -38,6 +38,7 @@
<Compile Include="FileWatchers.fs" />
<Compile Include="Pipeline.fs" />
<Compile Include="Main.fs" />
<Compile Include="CustomLogging.fs" />
<Compile Include="Entry.fs" />
<Content Include="CHANGELOG.md" />
<Content Include="Properties\launchSettings.json" />
Expand Down
Loading