Skip to content

Commit

Permalink
Print logs as json
Browse files Browse the repository at this point in the history
  • Loading branch information
milandjurdjevic committed Jan 4, 2025
1 parent 4bc65aa commit c67c8cc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Analog.Tests/FilterEvaluatorTest.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module FilterEvaluatorTest
module Analog.Tests.FilterEvaluatorTest

open System
open Xunit
Expand Down
1 change: 1 addition & 0 deletions src/Analog/Analog.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="grok.net" Version="2.0.0" />
<PackageReference Include="PCRE.NET" Version="1.1.0" />
<PackageReference Include="Spectre.Console" Version="0.49.1"/>
<PackageReference Include="Spectre.Console.Json" Version="0.49.1"/>
</ItemGroup>

</Project>
50 changes: 36 additions & 14 deletions src/Analog/Program.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
open System
open System.IO
open System.Text.Json
open Analog
open Argu
open Spectre.Console
open Spectre.Console.Json

type Argument =
| [<MainCommand; Mandatory; First>] File of string
Expand All @@ -15,19 +18,17 @@ type Argument =
| Pattern _ -> "GROK pattern."
| Filter _ -> "Filter expression."

let handle (args: ParseResults<Argument>) =
let text =
args.GetResults Argument.File
|> List.map File.ReadAllText
|> List.reduce (fun all next -> all + next)

let entries =
args.TryGetResult Argument.Pattern
|> Option.map EntryParser.create
|> Option.defaultValue (EntryParser.value |> Result.Ok)
|> Result.map (EntryParser.parse text)

args.TryGetResult Argument.Filter
let import files =
files |> List.map File.ReadAllText |> List.reduce (fun all next -> all + next)

let parse pattern text =
pattern
|> Option.map EntryParser.create
|> Option.defaultValue (EntryParser.value |> Result.Ok)
|> Result.map (EntryParser.parse text)

let filter filter entries =
filter
|> Option.map (ParserRunner.run FilterParser.expression)
|> Option.map (fun res ->
res
Expand All @@ -38,6 +39,27 @@ let handle (args: ParseResults<Argument>) =
entries |> List.filter (fun entry -> FilterEvaluator.evaluate entry filter)))
|> Option.defaultValue entries

let handle (args: ParseResults<Argument>) =
import (args.GetResults Argument.File)
|> parse (args.TryGetResult Argument.Pattern)
|> filter (args.TryGetResult Argument.Filter)

let normalize (entry: Entry) =
entry
|> Map.map (fun _ value ->
match value with
| String value -> box value
| Number value -> box value
| Boolean value -> box value
| Timestamp value -> box value)

let print entries =
entries
|> List.map normalize
|> JsonSerializer.Serialize
|> JsonText
|> AnsiConsole.Write

let args =
try
ArgumentParser
Expand All @@ -48,5 +70,5 @@ let args =
Result.Error err.Message

match args |> Result.bind handle with
| Ok entries -> entries.Length |> printf "%i"
| Ok entries -> print entries
| Error error -> error |> eprintf "%s"

0 comments on commit c67c8cc

Please sign in to comment.