はじめに
mojoliciousアプリをちょこちょこ触っているのですがアプリケーションサーバはPSGI/Plackで起動しています。
静的ファイルはpsgiでPlack::Middleware::Static
をenableすることでリバプロ的に配信していますが、
開発中はログがががーーーーーーーと出て非常にみずらいです。
その静的ファイルログをなんとかするメモ。
概要
おもいっきり他力ではありますが、すでに他の方がPlack-Middleware-LogFilter
という
Plack::Middlewareを作ってくれているのでありがたく使わせていただきます。
参考にさせていただいたのはこちらです。
やりかた
入れる
自分はplenv x cartonな環境なのでこんな感じで入れました。
$ echo "requires 'Plack-Middleware-LogFilter';" >> cpanfile $ plenv exec carton install $ plenv rehash
使う
Plack::Middleware::LogFilterを使う前hoge.psgi
psgiで静的ファイルを配信している想定だったとします。
#!/usr/bin/env perl use strict; use warnings; use lib 'lib'; use Mojo::Server::PSGI; use Plack::Builder; use Hoge::Web; my $psgi = Mojo::Server::PSGI->new( app => Hoge::Web->new ); my $app = $psgi->to_psgi_app; builder { # for local enable "Plack::Middleware::Static", path => qr{^.*(\.jpg|\.jpeg|\.png|\.gif)}, root => 'public'; enable "Plack::Middleware::Static", path => qr{^.*.css}, root => 'public'; enable "Plack::Middleware::Static", path => qr{^.*.js}, root => 'public'; enable "Plack::Middleware::Static", path => qr{^.*.woff}, root => 'public'; $app; };
Plack::Middleware::LogFilter使ってみるhoge.psgiのbuilder
builder内でPlack::Middleware::Static
により
静的ファイルを配信する前の段階でログのfilterを行います。
builder { # ログをfilter enable 'Plack::Middleware::LogFilter', filter => sub { my ($env, $output) = @_; # ignore static file log if ($output =~ /\/(js|css|img|fonts)/) { return 0; } return 1; }; # for local enable "Plack::Middleware::Static", path => qr{^.*(\.jpg|\.jpeg|\.png|\.gif)}, root => 'public'; enable "Plack::Middleware::Static", path => qr{^.*.css}, root => 'public'; enable "Plack::Middleware::Static", path => qr{^.*.js}, root => 'public'; enable "Plack::Middleware::Static", path => qr{^.*.woff}, root => 'public'; $app; };
さらに
自分は何も出てほしくなかったのでさらにこんな感じにしました。
enable 'Plack::Middleware::LogFilter', filter => sub { return 0 };
まとめ
Plack::Middleware::LogFilterの作者さんに感謝!
これですっきり開発できるよになりました!\(^o^)/