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

Logging support via tracing #8

Open
srid opened this issue Aug 17, 2023 · 3 comments
Open

Logging support via tracing #8

srid opened this issue Aug 17, 2023 · 3 comments

Comments

@srid
Copy link
Contributor

srid commented Aug 17, 2023

I wrote a wrapper around use_query, called use_server_query, specifically geared to call server functions. The main reason (aside from the nicety of being able to pass server fn directly, and it using default options) is so that I can log the call to each server fns in a general fashion.

You can find the implementation here: https://github.com/juspay/nix-browser/blob/132ed807fa909b0eaa73871b28f0f6c255b50703/src/leptos_extra/query.rs#L20-L61

The downside is that I had to make all server fns 1-ary, taking a tuple, because Rust type system is not powerful enough to let me a way find the function name at runtime for n-ary functions (the function name is used in log message).

This is used like this:

https://github.com/juspay/nix-browser/blob/132ed807fa909b0eaa73871b28f0f6c255b50703/src/app.rs#L122-L123

    let (query, set_query) = use_signal::<FlakeUrl>(cx);
    let result = query::use_server_query(cx, query, get_flake);

get_flake is a server_fn here (taking the value of query as argument), and every time either use_query is called or the fetcher is actually invoked, it logs to browser console. So, I guess, my feature request - can a similar kind of logging (via tracing instrumentation) support be added to this library?

@nicoburniske
Copy link
Member

nicoburniske commented Aug 20, 2023

I tried getting something to work in #9

The main code is here

https://github.com/nicoburniske/leptos_query/pull/9/files#diff-02bceb27aaf870f25dc3fc975775371e4ff5db2c41831c0b251dc765ed6b2f3bR60

The thing is, whenever I change the trace level from info to debug, then nothing shows up in the console. Do you have any ideas as to why? I don't think I can keep this as info because it's extremely noisy. Unless I hide it behind a feature.

@nicoburniske
Copy link
Member

didn't mean to close it haha.

also as a side note, if you just want to observer query changes, you can do the following. The tracing is the actual execution of the fetcher, and you can just use an effect to observe the query state for debugging

  let QueryResult { state, .. } = use_post_query(cx, post_id);

    // Log the state of the query.
   create_effect(cx, move |_| log!("State: {:#?}", state.get()));

Full example is here

https://github.com/nicoburniske/leptos_query/blob/2084345eb281119fef33963fe6128c47d8ed3475/example/start-axum/src/app.rs#L197C3-L197C3

@srid
Copy link
Contributor Author

srid commented Aug 24, 2023

The thing is, whenever I change the trace level from info to debug, then nothing shows up in the console. Do you have any ideas as to why?

FWIW, I use tracing_subscriber and debug logging does work in browser console. You can see the setup code I use here:

https://github.com/juspay/nix-browser/blob/4db42296672adbaecfbed5ae9b31c868a44e40c7/src/logging.rs#L21-L32

(change INFO to DEBUG)

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