| 1 | package Mojolicious::Plugin::MoreLog; | ||
| 2 | |||
| 3 | # ABSTRACT: Adds printf and dump to Mojo::Log | ||
| 4 | |||
| 5 | use Mojo::Base 'Mojolicious::Plugin'; | ||
| 6 | use Mojo::Util qw/dumper monkey_patch/; | ||
| 7 | |||
| 8 | sub register { | ||
| 9 | my ($self, $app, $conf) = @_; | ||
| 10 | monkey_patch "Mojo::Log", "dump" => sub { | ||
| 11 | my $self = shift; | ||
| 12 | if (scalar @_ > 1) { | ||
| 13 | $self->info(dumper \@_) | ||
| 14 | } else { | ||
| 15 | $self->info(dumper shift) | ||
| 16 | } | ||
| 17 | }; | ||
| 18 | monkey_patch "Mojo::Log", "printf" => sub { | ||
| 19 | my $self = shift; | ||
| 20 | $app->log->info(sprintf shift, @_); | ||
| 21 | }; | ||
| 22 | } | ||
| 23 | |||
| 24 | 1; | ||
| 25 | |||
| 26 | =pod | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Mojolicious::Plugin::MoreLog - Add printf and dump methods to Mojo::Log | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | plugin 'MoreLog'; | ||
| 35 | |||
| 36 | # Later in your app | ||
| 37 | $app->log->printf("User %s logged in", $user); | ||
| 38 | $app->log->dump($data); | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | L<Mojolicious::Plugin::MoreLog> is a plugin for Mojolicious that monkey-patches | ||
| 43 | L<Mojo::Log> to add two convenience methods: C<dump> and C<printf>. | ||
| 44 | |||
| 45 | These methods allow quick structured logging and formatted message logging | ||
| 46 | respectively. | ||
| 47 | |||
| 48 | =head1 METHODS | ||
| 49 | |||
| 50 | =head2 register | ||
| 51 | |||
| 52 | $plugin->register($app, \%conf); | ||
| 53 | |||
| 54 | Registers the plugin and injects the following methods into L<Mojo::Log>: | ||
| 55 | |||
| 56 | =over 4 | ||
| 57 | |||
| 58 | =item * dump(@args) | ||
| 59 | |||
| 60 | Logs a data dump of the arguments using L<Mojo::Util/dumper> at C<info> level. | ||
| 61 | |||
| 62 | $app->log->dump($user); | ||
| 63 | $app->log->dump($user, $session, \%env); | ||
| 64 | |||
| 65 | =item * printf($format, @args) | ||
| 66 | |||
| 67 | Logs a message using C<sprintf> with the provided format string and arguments, | ||
| 68 | also at C<info> level. | ||
| 69 | |||
| 70 | $app->log->printf("Logged in user %s with ID %d", $name, $id); | ||
| 71 | |||
| 72 | =back | ||
| 73 | |||
| 74 | =head1 SEE ALSO | ||
| 75 | |||
| 76 | L<Mojolicious>, L<Mojo::Log>, L<Mojo::Util/monkey_patch>, L<Mojo::Util/dumper> | ||
| 77 | |||
| 78 | =head2 AUTHORS | ||
| 79 | |||
| 80 | Simone Cesano <scesano@cpan.org> | ||
| 81 | |||
| 82 | =head2 COPYRIGHT AND LICENSE | ||
| 83 | |||
| 84 | This software is copyright (c) 2021 by Simone Cesano. | ||
| 85 | |||
| 86 | This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. |