-
Notifications
You must be signed in to change notification settings - Fork 576
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
Add sprintf wrappers #1909
Conversation
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.
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.
Agreed, here is a new proposal. |
What are the use cases for this feature? |
This feature is very useful. regds |
You are aware that string interpolation exists in Perl, right? |
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. |
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.
"previous method" is easy to break when new methods are added.
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.
No tests.
Personally, I question whether the occasional and fairly minor convenience is worth doubling up the log methods and enveloping language features, when |
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 |
its already difficult to push lazy programmers to do requisite amount of logging in moreover such features are found even in other logging contexts/environment. |
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.
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 { |
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.
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.
34b13cf
to
be666ac
Compare
I just pushed a new version, with commit squashed as requested |
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. |
Given every one of them is just I'm not a maintainer, but I agree that a CPAN module providing these seems to make more sense though. 😇 |
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. |
I find this discussion very interesting. @jhthorsen care to elaborate on |
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 |
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.
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.
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'; |
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.
Test messages are not differentiated for the log level.
IMHO , keeping the spirit of minimalism the function may be left and improved further in plugin Logf as it is Can we not have a curated list of very useful Mojo plugins and have that list suggested in the documentation?
|
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. |
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