Snippets

Brian Medley Some logging joy

Created by Brian Medley
package Mojolicious::Plugin::Joy;

use Mojo::Base 'Mojolicious::Plugin';

our $VERSION = '0.01';

sub register {
    my ($self, $app) = @_;

    $app->hook(after_build_tx => sub {
        my ($tx, $app) = @_;
  
        $tx->on(joy => sub {
            my ($tx, $c, @args) = @_;

            push(@{ $c->stash("_loggin") }, @args);
        });
    });

    $app->hook(around_dispatch => sub {
        my ($next, $c) = @_;
        
        $c->stash("_loggin", []);

        $next->();
       
        $c->app->log->info("[LOGGIN] " . join("\t", @{ $c->stash("_loggin") // [] }));
    });
}

1;
#!/opt/perl

use Mojolicious::Lite;

plugin 'Joy';

get '/' => sub {
  my $c = shift;

  $c->tx->emit(joy => $c, "Hello");

  $c->render(template => 'index');

  $c->tx->emit(joy => $c, "There");
};

app->start;

__DATA__

@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>Welcome to the Mojolicious real-time web framework!</h1>
To learn more, you can browse through the documentation
<%= link_to 'here' => '/perldoc' %>.

@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head><title><%= title %></title></head>
  <body><%= content %></body>
</html>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.