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

Provide a way for Log.debug{} blocks to not print anything #11997

Closed
robacarp opened this issue Apr 13, 2022 · 2 comments · Fixed by #12000
Closed

Provide a way for Log.debug{} blocks to not print anything #11997

robacarp opened this issue Apr 13, 2022 · 2 comments · Fixed by #12000

Comments

@robacarp
Copy link
Contributor

Feature Request

Log nicely forces you to encapsulate logging-specific logic into blocks which aren't executed unless the log level is going to print something.

  • Is your feature request related to a problem? Please describe clearly and concisely what is it.

The other day I found myself doing this to avoid a blank line being emitted to the log:

def get_things_and_filter
  list_of_things = whatever_list_generator
  filtered_list = filter_method list_of_things
  if filtered_list.size < list_of_things.size # comparison doesn't need to run when log level isn't <= debug
    Log.for("get_things_and_filter").debug { "filter_method removed #{filtered_list.size - list_of_things.size} things from the list!" }
  end
end
  • Describe the feature you would like, optionally illustrated by examples, and how it will solve the above problem.

Does it make sense to modify the API so that this wouldn't print a blank line in the log?

def get_things_and_filter
  list_of_things = whatever_list_generator
  filtered_list = filter_method list_of_things

  Log.for("get_things_and_filter").debug do
    if filtered_list.size < list_of_things.size # comparison doesn't run when log level isn't <= debug
      "filter_method removed #{filtered_list.size - list_of_things.size} things from the list!"
    end
  end
end
  • Describe considered alternative solutions, and the reasons why you have not proposed them as a solution here.

Other options include some sort of magic flag or exception. I don't like that.

  • Does it break backward compatibility, if yes then what's the migration path?

I suppose technically yes. Code which is written to emit a blank line would no longer emit the blank line.

@Blacksmoke16
Copy link
Member

Blacksmoke16 commented Apr 13, 2022

So i think you can already do this via using break within the log method's block because of:

A break expression inside a block exits early from the method:

For example:

Log.info do
  "This prints"
end

Log.info do
  break
  "This does not"
end

Which outputs:

$ crystal test.cr
2022-04-13T22:49:16.236872Z   INFO - This prints

@straight-shoota
Copy link
Member

straight-shoota commented Apr 14, 2022

I think it would be a good change to not emit a log entry when the block returns nil. An entry with nil message does't make much sense. If you want an entry without a message, you can use the empty string for that (that's what the nil gets turned to anyway).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants