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

Make logfn optional #133

Closed
64J0 opened this issue Aug 30, 2023 · 8 comments
Closed

Make logfn optional #133

64J0 opened this issue Aug 30, 2023 · 8 comments

Comments

@64J0
Copy link

64J0 commented Aug 30, 2023

I'd like to have a toggle option to make the usage of logfn optional. After making several requests, it's weird to see the same string pattern appearing at the terminal. In this case, I'd prefer to not show it at all.

The version I'm using is 11.0.0.

Let me know if you think this is something that we could add to this project.

@SchlenkR
Copy link
Owner

Hi @64J0 and thanks for the issue. I'm not sure what's your exact use case, so let me explain how it is meant to work:

  • In F# interactive, console logging (via logfn) are enabled per default.
  • It's then possible to deactivate logging globally, using Fsi.disableDebugLogs()
  • In a non-F# interactive environment, there should be no logging at all.

Can you explain your use case, and if there's still an issue after all?

Thank you.

@64J0
Copy link
Author

64J0 commented Aug 30, 2023

Nice, I was not aware of this configuration! Thanks for the information @RonaldSchlenker.

To give more context, I'm using an FSI script to trigger some requests,:

dotnet fsi samples/ResponseCachingApp/test-run.fsx

After adding the Fsi.disableDebugLogs() to the top of my script, the debug logs are not showing anymore.

@64J0
Copy link
Author

64J0 commented Aug 30, 2023

I'll close this issue now, thanks again @RonaldSchlenker!

@64J0 64J0 closed this as completed Aug 30, 2023
@SchlenkR
Copy link
Owner

Nice, glad we could solve it :) Have a nice day!

@64J0
Copy link
Author

64J0 commented Aug 30, 2023

I'm using it on this PR -> giraffe-fsharp/Giraffe#553

@SchlenkR
Copy link
Owner

SchlenkR commented Aug 30, 2023

Nice that you contribute to Giraffe! I have seen the fsx, and wanted to ask you what do you think of some changes:

#r "nuget: FsHttp, 11.0.0"

open System
open FsHttp

// Uncomment if you don't want FsHttp debug logs
// Fsi.disableDebugLogs()

type QueryParams = (string * obj) list
type Url = Url of string
type Title = Title of string

let urls =
    {|
        notCached = Url "http://localhost:5000/cached/not"
        publicCached = Url "http://localhost:5000/cached/public"
        privateCached = Url "http://localhost:5000/cached/private"
        publicCachedNoVaryByQueryKeys = Url "http://localhost:5000/cached/vary/not"
        cachedVaryByQueryKeys = Url "http://localhost:5000/cached/vary/yes"
    |}

let queryParams1: QueryParams = [ ("query1", "a"); ("query2", "b") ]
let queryParams2: QueryParams = [ ("query1", "c"); ("query2", "d") ]

let waitForOneSecond () =
    do Threading.Thread.Sleep (TimeSpan.FromSeconds 1.0)

let makeRequest (Url url: Url) (queryParams: list<string * obj>) =
    let response =
        http {
            GET url
            CacheControl "max-age=3600"
            query queryParams
        }
        |> Request.send
        |> Response.toFormattedText

    printfn "%s" response
    printfn ""

let printRunTitle (Title title) =
    printfn "-----------------------------------"
    printfn "%s" title
    printfn ""

let printTimeTaken (duration: TimeSpan) =
    printfn "The time it took to finish:"
    printfn "%.2f seconds" duration.TotalSeconds
    printfn ""

let run (qps: QueryParams list) title url =
    printRunTitle title

    let stopWatch = Diagnostics.Stopwatch.StartNew()
    for queryParams in qps do
        makeRequest url queryParams |> waitForOneSecond

    stopWatch.Stop()
    printTimeTaken stopWatch.Elapsed


let runFiveRequests = run [ for _ in 1..5 do [] ]

let testPublicCachedNoVaryByQueryKeys () =
    let allQueryParams = 
        [
            queryParams1
            queryParams1
            queryParams2
            queryParams2
        ]
    let title = Title "Testing the /cached/vary/not endpoint"
    let url = urls.publicCachedNoVaryByQueryKeys
    run allQueryParams title url

let testCachedVaryByQueryKeys () =
    let title = Title "Testing the /cached/vary/yes endpoint"
    let url = urls.cachedVaryByQueryKeys
    let allQueryParams = 
        [
            queryParams1
            queryParams1
            queryParams2
            queryParams2
        ]
    run allQueryParams title url

let main () =
    runFiveRequests (Title "Testing the /cached/not endpoint") urls.notCached
    runFiveRequests (Title "Testing the /cached/public endpoint") urls.publicCached
    runFiveRequests (Title "Testing the /cached/private endpoint") urls.privateCached
    testPublicCachedNoVaryByQueryKeys ()
    testCachedVaryByQueryKeys ()

main ()

The changes are basically:

  • use of an anonymous record to access the well-known URLs in a type safe way
  • removed redundancies
  • used single case DU to for url and title to not accidently mix them on call site + better documentation
  • used semantic data types like TimeSpan + better documentation

You can think about it and if you like, use it. Say hi to Jimmy, and have a good day / night.

P.S.: As an alternative to blocking the thread using Thread.Sleep, you could lift your computation to Async<_> and use Async.Sleep (or also Task<_>).

@64J0
Copy link
Author

64J0 commented Aug 30, 2023

Great suggestions @RonaldSchlenker, I'm going to apply it there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants