-
Notifications
You must be signed in to change notification settings - Fork 46
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
Extend custom logger to allow taking the notifications directly and generate it's own string. #57
base: main
Are you sure you want to change the base?
Changes from 4 commits
b5349d9
ec3da32
fb65cfd
93805b7
6a31155
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,17 +135,40 @@ Datadog, ELK stack, or similar, and don't want to have to remove the default red | |
escaping data from messages sent to the Rails logger, or want to tag them | ||
differently with your own custom logger. | ||
|
||
The logger is expected to have a `warn` method that takes a string. | ||
|
||
```ruby | ||
# Turns off logging with red highlights, but still sends them to the Rails logger | ||
Prosopite.custom_logger = Rails.logger | ||
``` | ||
|
||
```ruby | ||
# Use a completely custom logging instance | ||
Prosopite.custom_logger = MyLoggerClass.new | ||
class MyLogger | ||
def warn(str) | ||
# implementation | ||
end | ||
end | ||
|
||
Prosopite.custom_logger = MyLogger.new | ||
``` | ||
|
||
Additionally, you can further customize how notifications are displayed by makni ga `warn` method that takes a `notifications` keyword: | ||
|
||
```ruby | ||
class FancifulLogger | ||
def warn(notifications:) | ||
notification.each do |queries, kaller| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# queries is an array of strings of santitized queries | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
# kaller is the callstack where it was called, and has been cleaned by Prosopite.backtrace_cleaner | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
end | ||
end | ||
end | ||
|
||
Prosopite.custom_logger = FancifulLogger.new | ||
``` | ||
|
||
|
||
## Development Environment Usage | ||
|
||
Prosopite auto-detection can be enabled on all controllers: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,16 +192,16 @@ def mysql_fingerprint(query) | |
query | ||
end | ||
|
||
def send_notifications | ||
@custom_logger ||= false | ||
@rails_logger ||= false | ||
@stderr_logger ||= false | ||
@prosopite_logger ||= false | ||
@raise ||= false | ||
def clean_notifications(notifications) | ||
notifications.map do |queries, kaller| | ||
kaller = backtrace_cleaner.clean(kaller) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. backtrace_cleaner is also called in the |
||
[queries, kaller] | ||
end.to_h | ||
end | ||
|
||
def generate_notification_message(notifications) | ||
notifications_str = '' | ||
|
||
tc[:prosopite_notifications].each do |queries, kaller| | ||
notifications.each do |queries, kaller| | ||
notifications_str << "N+1 queries detected:\n" | ||
|
||
queries.each { |q| notifications_str << " #{q}\n" } | ||
|
@@ -215,7 +215,26 @@ def send_notifications | |
notifications_str << "\n" | ||
end | ||
|
||
@custom_logger.warn(notifications_str) if @custom_logger | ||
notifications_str | ||
end | ||
|
||
def send_notifications | ||
@custom_logger ||= false | ||
@rails_logger ||= false | ||
@stderr_logger ||= false | ||
@prosopite_logger ||= false | ||
@raise ||= false | ||
|
||
notifications = clean_notifications(tc[:prosopite_notifications]) | ||
notifications_str = generate_notification_message(notifications) | ||
|
||
if @custom_logger | ||
if @custom_logger.method(:warn).arity == 0 | ||
@custom_logger.warn(notifications: notifications) | ||
else | ||
@custom_logger.warn(notifications_str) | ||
end | ||
end | ||
|
||
Rails.logger.warn(red(notifications_str)) if @rails_logger | ||
$stderr.puts(red(notifications_str)) if @stderr_logger | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo:
by makni ga