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

Extend custom logger to allow taking the notifications directly and generate it's own string. #57

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Owner

Choose a reason for hiding this comment

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

Typo: by makni ga


```ruby
class FancifulLogger
def warn(notifications:)
notification.each do |queries, kaller|
Copy link
Owner

Choose a reason for hiding this comment

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

notification vs notifications - the singular form doesn't exist.

# queries is an array of strings of santitized queries
Copy link
Owner

Choose a reason for hiding this comment

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

typo santitized - however what do you mean by sanitized queries?
I would write this as The SQL queries that constitute the N+1 case

# kaller is the callstack where it was called, and has been cleaned by Prosopite.backtrace_cleaner
Copy link
Owner

Choose a reason for hiding this comment

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

I think they were called is more appropriate since we are referring to multiple queries.

end
end
end

Prosopite.custom_logger = FancifulLogger.new
```


## Development Environment Usage

Prosopite auto-detection can be enabled on all controllers:
Expand Down
37 changes: 28 additions & 9 deletions lib/prosopite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

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

backtrace_cleaner is also called in the generate_notification_message - should be removed from there

[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" }
Expand All @@ -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
Expand Down