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 sprintf wrappers #1909

Closed
wants to merge 4 commits into from

Conversation

guillomovitch
Copy link

Summary

Add sprintf-based wrappers over standard logging methods (debug(), info(), error(), ...)

Motivation

This is just a port of a Log::Any feature:
https://metacpan.org/pod/Log::Any#Logging

Copy link
Contributor

@kathackeray kathackeray left a comment

Choose a reason for hiding this comment

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

There is a fair amount of duplication between _logf() and the original _log(). Might it be preferable to perform only the sprintf in _logf() and then hand off to _log() to do what it does uniquely?

As for the concept of adding new functions for this purpose rather than using sprintf, it will be better for others to comment.

@guillomovitch
Copy link
Author

Agreed, here is a new proposal.

@kraih
Copy link
Member

kraih commented Mar 4, 2022

What are the use cases for this feature?

@rmallah
Copy link

rmallah commented Mar 4, 2022

This feature is very useful.
Although i use https://metacpan.org/pod/Mojolicious::Plugin::Logf
for the same. Embedded %s are useful for not having to use
concat while logging multiple variables in the same line.

regds
Rajesh.

@kraih
Copy link
Member

kraih commented Mar 4, 2022

You are aware that string interpolation exists in Perl, right?

@guillomovitch
Copy link
Author

Sure, and sprintf() exists too. Its main advantage is to allow easier code formatting across multiple lines for long strings with multiple variables, avoiding use of intermediate variables for function call.

lib/Mojo/Log.pm Outdated

$log = $log->debugf('%s is %s', 'Mojolicious', 'cool');

Simple sprint-based wrapper over previous method.
Copy link
Member

Choose a reason for hiding this comment

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

"previous method" is easy to break when new methods are added.

kraih
kraih previously requested changes Mar 4, 2022
Copy link
Member

@kraih kraih left a comment

Choose a reason for hiding this comment

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

No tests.

@kathackeray
Copy link
Contributor

Personally, I question whether the occasional and fairly minor convenience is worth doubling up the log methods and enveloping language features, when $log->error( sprintf('%s is %s', 'Mojolicious', 'cool') ) does the job when needed. But it does seem to have support. Maybe this is more common than I am aware.

@rmallah
Copy link

rmallah commented Mar 4, 2022

You are aware that string interpolation exists in Perl, right?

Surely :-)

$c->logf( debug => "age of student %s is : %s" , $obj->name , $obj->age );

interpolation still does not (imho should not) handle all expression evaluations.

regds
Rajesh.

@rmallah
Copy link

rmallah commented Mar 4, 2022

Personally, I question whether the occasional and fairly minor convenience is worth doubling up the log methods and enveloping language features, when $log->error( sprintf('%s is %s', 'Mojolicious', 'cool') ) does the job when needed. But it does seem to have support. Maybe this is more common than I am aware.

its already difficult to push lazy programmers to do requisite amount of logging in
code. making 'em write an extra sprintf makes it even more challenging.

moreover such features are found even in other logging contexts/environment.
Eg:
https://www.postgresql.org/docs/9.3/plpgsql-errors-and-messages.html

@mergify mergify bot dismissed kraih’s stale review March 11, 2022 16:58

Pull request has been modified.

Copy link
Contributor

@kathackeray kathackeray left a comment

Choose a reason for hiding this comment

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

Judging from the feedback on other reviews, you'll be asked to squash your commits.

t/mojo/log.t Outdated
@@ -229,4 +229,27 @@ subtest 'Context' => sub {
like $buffer, qr/\[.*\] \[fatal\] \[123\] Mojolicious rocks\n/, 'right fatal message';
};

subtest 'wrappers' => sub {
Copy link
Contributor

Choose a reason for hiding this comment

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

The convention seems to be capitalisation for subtest names, e.g. subtest 'History' => sub {, unless the subtest name is a function name, in which case it is quoted, e.g. subtest '"error"' => sub {.

As this tests specifically your new sprintf wrappers, in my opinion it should be something like Text formatting wrappers for the subtest name.

@guillomovitch
Copy link
Author

I just pushed a new version, with commit squashed as requested

@jberger
Copy link
Member

jberger commented Mar 15, 2022

I'm with some of the other voices here, this functionality is easy to access by other mechanisms and thus does not need to be implemented by core mojo. It can also be provided by a role on CPAN. However it would increase the complexity of the module, adding nearly double the number of methods.

I know some other logging frameworks use this style of logging by default, and so I will not vote against if enough people think that is worthwhile, but I will also not vote in support.

@tianon
Copy link
Contributor

tianon commented Mar 16, 2022

Given every one of them is just sub foof { shift->foo(sprintf(shift, @_)) }, couldn't they be defined in a code loop somehow instead? I seem to recall other modules in Mojo doing something like that.

I'm not a maintainer, but I agree that a CPAN module providing these seems to make more sense though. 😇

@jhthorsen
Copy link
Member

I do like my logf() plugin (though it should be a Mojo::Log role these days), and from the experience of writing multiple implementations, I don't think a plain "sprintf" is enough in many cases.

Based on that I'm not in favor of adding this to core.

@daleif
Copy link

daleif commented Mar 16, 2022

I find this discussion very interesting.

@jhthorsen care to elaborate on I don't think a plain "sprintf" is enough in many cases.?

@jhthorsen
Copy link
Member

@jhthorsen care to elaborate on I don't think a plain "sprintf" is enough in many cases.?

Once I need sprintf, I also often need some sort of Data::Dumper or encode_json to make it useful. Other than that I think the most useful feature is handling undef in some sensible way. The only thing this PR adds (imo) is that you don't have to do "foo=@{[$obj->foo]}", which I don't find that useful.

Copy link
Contributor

@kathackeray kathackeray left a comment

Choose a reason for hiding this comment

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

In terms of the code alone, the tests pass and documentation is adequate. I believe a change is required to the test messages, see annotation below.

However, I still feel that this concept isn't in the minimal spirit of core Mojo.

I appreciate the 'lazy developers' argument mentioned above, and there are occasions it might have sway. But that argument is unavoidably tied to what the developers are being asked to do, e.g. if another set of lazy developers are being asked to log entirely in uppercase, but they're too lazy to uc themselves, should core Mojo have to accommodate with a set of erroruc(), warnuc() etc. methods? I think rather it is the duty of an extension.

Additionally the existing logf plugin is fuller featured than the implementation here. Striving to handle undefined values and complex data structures (surely a boon for lazy developers). If the functionality was to be integrated into core, I might expect it to at least live up to that standard.

Comment on lines +247 to +252
like $content, qr/\[.*\] \[trace\] Mojolicious works\n/, 'right error message';
like $content, qr/\[.*\] \[debug\] Mojolicious just works\n/, 'right error message';
like $content, qr/\[.*\] \[info\] Mojolicious really works\n/, 'right error message';
like $content, qr/\[.*\] \[warn\] Mojolicious works, really\n/, 'right error message';
like $content, qr/\[.*\] \[error\] Mojolicious works great\n/, 'right error message';
like $content, qr/\[.*\] \[fatal\] Mojolicious works well\n/, 'right error message';
Copy link
Contributor

Choose a reason for hiding this comment

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

Test messages are not differentiated for the log level.

@rmallah
Copy link

rmallah commented Mar 17, 2022

IMHO , keeping the spirit of minimalism the function may be left and improved further in plugin Logf as it is
already more complete and useful implementation rather than plain sprintf.

Can we not have a curated list of very useful Mojo plugins and have that list suggested in the documentation?

In terms of the code alone, the tests pass and documentation is adequate. I believe a change is required to the test messages, see annotation below.

However, I still feel that this concept isn't in the minimal spirit of core Mojo.

I appreciate the 'lazy developers' argument mentioned above, and there are occasions it might have sway. But that argument is unavoidably tied to what the developers are being asked to do, e.g. if another set of lazy developers are being asked to log entirely in uppercase, but they're too lazy to uc themselves, should core Mojo have to accommodate with a set of erroruc(), warnuc() etc. methods? I think rather it is the duty of an extension.

Additionally the existing logf plugin is fuller featured than the implementation here. Striving to handle undefined values and complex data structures (surely a boon for lazy developers). If the functionality was to be integrated into core, I might expect it to at least live up to that standard.

@guillomovitch
Copy link
Author

I didn't expected so much discussion for such a feature, which is indeed trivial to implement outside of Mojo. I prefer to close the integration request rather than continue to try to satisfy minor cosmetics issues, while the general consensus seems to reject the change.

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

Successfully merging this pull request may close these issues.

9 participants