Git Product home page Git Product logo

Comments (8)

SchlenkR avatar SchlenkR commented on June 4, 2024 1

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.

from fshttp.

SchlenkR avatar SchlenkR commented on June 4, 2024 1

Note:

// why we have this? -> search for #121
let mutable internal logDebugMessages = None
let enableDebugLogs () = logDebugMessages <- Some true
let disableDebugLogs () = logDebugMessages <- Some false
let logfn message =
let logDebugMessages = logDebugMessages |> Option.defaultValue false
message
|> Printf.kprintf (fun s ->
if logDebugMessages then
printfn "%s" s
)

|> function
| None -> NoFsiObjectFound
| Some fsiInstance ->
do
// see #121: It's important to not touch the logDebugMessages
// value when it was already set before this init function was called.
match Fsi.logDebugMessages with
| None -> Fsi.enableDebugLogs ()
| _ -> ()

from fshttp.

64J0 avatar 64J0 commented on June 4, 2024 1

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

from fshttp.

SchlenkR avatar SchlenkR commented on June 4, 2024 1

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<_>).

from fshttp.

64J0 avatar 64J0 commented on June 4, 2024

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.

from fshttp.

SchlenkR avatar SchlenkR commented on June 4, 2024

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

from fshttp.

64J0 avatar 64J0 commented on June 4, 2024

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

from fshttp.

64J0 avatar 64J0 commented on June 4, 2024

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

from fshttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.