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

Add current stacktrace log #363

Conversation

tashirosota
Copy link

@tashirosota tashirosota commented Dec 2, 2021

Hi! Special thanks for ecto_sql and contributers.
AR has powerfull debug log output when sql called. I like it.

I implemented it to ecto_sql. If you don't mind check it.
How about incorporating this feature?

@josevalim
Copy link
Member

@tashirosota this looks nice, thanks for pinging! How would you configure it in your app?

Copy link
Member

@v0idpwn v0idpwn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! I'm not really sure if this feature fits (though I personally like the idea!), but I'm leaving my review :)

Comment on lines 64 to 78
before_log_level = Logger.level()

try do
Logger.configure(level: :debug)
assert ExUnit.CaptureLog.capture_log(fn ->
TestRepo.insert!(%Post{title: "1"}, [log: true])
end) =~ "[debug]"

refute ExUnit.CaptureLog.capture_log(fn ->
TestRepo.insert!(%Post{title: "1"}, [log: true])
end) =~ " ↳ "
after
Logger.configure(level: before_log_level)
end

Copy link
Member

@v0idpwn v0idpwn Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this really has an impact, but logger level is for the whole application, so changing it through Log.configure may affect other tests and may also be flaky (as this test is async)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@v0idpwn
Thanks for review!
I understood. But I don't know how to test it other than that.
Please give me a hint🙇‍♂️

@@ -983,6 +995,12 @@ defmodule Ecto.Adapters.SQL do
?\s,
inspect(params, charlists: false)
]
if stacktrace_match_path = Application.get_env(:ecto_sql, :log_stacktrace_match_path) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be stored in the opts, as opposed to application env. This is a better practice for configurability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'm wondering if there's a better approach instead of matching the first result into selected path. Is there any other option you've considered?

Copy link
Author

@tashirosota tashirosota Dec 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@v0idpwn
Thanks for review.

This should be stored in the opts, as opposed to application env. This is a better practice for configurability.

I changed it to log_stacktrace_match_path = Keyword.get(opts, :log_stacktrace_match_path) || Keyword.get(opts, :otp_app) |> to_string() and this logging to default output. Is it bad to have the default output?

Also, I'm wondering if there's a better approach instead of matching the first result into selected path. Is there any other option you've considered?

Because I referenced to the Rails implementation. And it's often useful.
Or output with all match stack trace? But I think that will became noisy log.

@tashirosota
Copy link
Author

tashirosota commented Dec 2, 2021

@josevalim @v0idpwn
Thanks for response!!I will reply after deep thinking!

@tashirosota tashirosota marked this pull request as draft December 4, 2021 03:56
@tashirosota tashirosota changed the title Add current stacktrace log [WIP] Add current stacktrace log Dec 4, 2021
@tashirosota tashirosota force-pushed the feature/log_current_stacktrace branch 4 times, most recently from f5f8768 to 4664159 Compare December 4, 2021 15:26
@tashirosota tashirosota closed this Dec 4, 2021
@tashirosota tashirosota reopened this Dec 4, 2021
@tashirosota tashirosota marked this pull request as ready for review December 4, 2021 15:45
@@ -60,6 +60,30 @@ defmodule Ecto.Adapters.SQL do
* `:driver` (required) - the database driver library.
For example: `:postgrex`

## Debug log

You can get caller location when SQL called and log level is :debug.
Copy link
Author

@tashirosota tashirosota Dec 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josevalim
I want to this log is default debug output, and get stacktrace location from String.match?(/otp_app_name/).
But may not work well, so users can set log_stacktrace_match_path and get it from String.match?(/log_stacktrace_match_path/).

@tashirosota tashirosota changed the title [WIP] Add current stacktrace log Add current stacktrace log Dec 4, 2021
@josevalim
Copy link
Member

Hi @tashirosota! Thanks for the work so far, I am discussing with @v0idpwn the best way to provide this, which may require some changes in Ecto too.

@v0idpwn
Copy link
Member

v0idpwn commented Dec 13, 2021

Hey, @tashirosota! I opened a PR where I tried to move the get stacktrace part to ecto, and only turn it into a log in ecto_sql. My goal is to avoid the traces of ecto insides so we don't need to process the stacktrace.

@tashirosota
Copy link
Author

tashirosota commented Dec 14, 2021

@v0idpwn
Thanks!That's great!
I have a question, how select stacktrace. I asked in your PR.

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

Successfully merging this pull request may close these issues.

3 participants