パルカワ2

最近はFlutterをやっています

MyApp::Logger

漫画ではありません。

use strict;
use warnings;
use MyApp::Logger;

MyApp::Logger->post({
    user   => $user,
    amount => 5,
});
package MyApp::Logger;
use 5.10.1;
use strict;
use warnings;
use Fluent::Logger;
use MyApp::Logger::Stderr;
use MyApp::Config;

sub client {
    state $client = do {
        if (my $config = config->param('Fluent::Logger')) {
            Fluent::Logger->new($config);
        }
        else {
            MyApp::Logger::Stderr->new;
        }
    };
}

sub post {
    my ($class, $type, $msg) = @_;
    Carp::croak('$msg: must be a HashRef.') if ref $msg ne 'HASH';

    my $message = $class->_build_message($msg);
    $class->client->post($type, $message);
}

sub _build_message {
    my ($class, $msg) = @_;

    for my $name (keys %$msg) {
        my $v = $msg->{$name};

        if (ref($v) =~ m/^MyApp::DB::Row/) {
            $msg->{$name} = $v->get_columns();
        }
        elsif (ref($v) =~ m/^MyApp::Exception/) {
            $msg->{$name} = {
                code    => $v->code,
                message => $v->to_string,
            };
        }
    }

    return $msg;
}

1;
package MyApp::Logger::Stderr;
use 5.10.1;
use strict;
use warnings;
use Log::Minimal qw/warnf/;

sub new { bless {}, shift }

sub post {
    my ($self, $tag, $hash) = @_;

    local $Log::Minimal::AUTODUMP = 1;
    warnf "$tag: %s", $hash;
}

1;

という感じのを作ればいいのかなって思ったんだけど、MyApp::DB::Rowのデータ全部入っちゃうと結構邪魔になるかって思った。