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 returns true #65

Closed
wants to merge 1 commit into from

Conversation

fnordfish
Copy link
Contributor

See #64

Types of Changes

  • Logging methods (like Console.info) consistently return true regardless of Output

Contribution

@fnordfish
Copy link
Contributor Author

fnordfish commented Nov 25, 2024

Just realized, that this is not complete yet. A log not matching the current filter will still return nil. For example, with log level info Console.debug("test") will return nil

@fnordfish fnordfish force-pushed the 64-logging-returns-true branch from b0e0c88 to a3b16b9 Compare November 25, 2024 14:10
@ioquatix
Copy link
Member

This looks somewhat reasonable to me.

I think if we had to establish a behaviour, it should be that the log operation returns truthy if it does something and falsey otherwise.

I believe if filter ... end already returns falsey if it isn't logged.

Regarding the other cases, I'm okay for them to return true if something was logged or not.

What do you think?

@fnordfish
Copy link
Contributor Author

fnordfish commented Nov 26, 2024

I have two thought:

1st: I assume ::Logger uses true as a universal return value to allow for log("message") and do_something_else

2nd: Personally, I like to have a consistent return value. My code should not need to care if the log has been written or not.
Consider this code

def do_something
  # more code
  Console.error("we did something") unless it_was_done
end

Now, in tests and development, you'll probably have error logging on, but in production, you might raise the log lever to fatal. Suddenly your method would return a different value.
If we ensure logging returns a uniform value, application code can rely on that and do what's right.

I've went ahead with returning true just because Console already returned truthy values (and a little bit to mimic ::Logger). I would be totally fine changing this and returning nil everywhere.

Anyways, If you have projects relying on truthy/falsey return values, let's do that, but also add some "jumps-in-your-eye" documentation :)

@@ -70,25 +74,28 @@ def before
it "ignores messages below the level" do
logger.level = Console::Logger::INFO

logger.call(MySubject, "Hello World", severity: :debug)
retval = logger.call(MySubject, "Hello World", severity: :debug)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
retval = logger.call(MySubject, "Hello World", severity: :debug)
result = logger.call(MySubject, "Hello World", severity: :debug)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@ioquatix
Copy link
Member

ioquatix commented Nov 28, 2024

If we are planning to return the same thing everywhere, why not always return nil?

In other words:

  1. Always return true (current proposal)
  2. Return true if logged, false otherwise (questionable behaviour/value)
  3. Always return nil (my proposal).

@fnordfish
Copy link
Contributor Author

I'm totally fine with always returning nil.

The current proposal was only to be more compatible with Rubies Logger.

@ioquatix
Copy link
Member

Okay, please let me think about it.

- regardless of used outputs
- regardless of effective log level
@fnordfish fnordfish force-pushed the 64-logging-returns-true branch from a3b16b9 to 9b40a86 Compare November 28, 2024 11:38
@ioquatix ioquatix mentioned this pull request Dec 9, 2024
3 tasks
@ioquatix
Copy link
Member

ioquatix commented Dec 9, 2024

I've decided to go ahead with always returning nil from Console::Filter.

Honestly, I could see different ways this could be implemented. Returning true/false is also interesting, because it might allow conditional behavior (if this was not logged, do something else). The question of whether something is logged or not, however, IMHO, should not affect program behaviour. So, I decided not to encourage that. In addition, returning true might encourage users to use Console... in a conditional and I also don't want to encourage that either.

@ioquatix
Copy link
Member

ioquatix commented Dec 9, 2024

Thanks for your contribution, closing in favour of #68.

@ioquatix ioquatix closed this Dec 9, 2024
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.

2 participants