| 1 | package Mojolicious::Plugin::CustomLog; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | $Mojolicious::Plugin::CustomLog::VERSION = '0.06'; | |||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | ▶ | use Mojo::Util 'encode'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use Fcntl ':flock'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | my $PATH = +{}; | |||||||||||
| 11 | 1 | my $MODE; | |||||||||||
| 12 | |||||||||||||
| 13 | 1 | ▶ | sub register { | ||||||||||
|
Subroutine
register: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 14 | 1 | my $self = shift; | |||||||||||
| 15 | 1 | my ($app, $config) = @_; | |||||||||||
| 16 | |||||||||||||
| 17 | 1 | $MODE = $app->mode; | |||||||||||
| 18 | |||||||||||||
| 19 | 1 | ▶ | while ( my ($key, $value) = each(%{$config->{path}}) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 2 | my $file = $app->home->rel_file($value . "_" . $MODE . ".log"); | |||||||||||
| 21 | 2 | $PATH->{$key} = $file; | |||||||||||
| 22 | } | ||||||||||||
| 23 | |||||||||||||
| 24 | # attach log instance to $app->clog | ||||||||||||
| 25 | # if there is no helper name provided | ||||||||||||
| 26 | 1 | ▶ | $config->{helper} ||= 'clog'; | ||||||||||
|
Condition50%1/2
$config->{"helper"} ||= "clog"
MC/DC0%0/1
$config->{"helper"} ||= "clog"
$config->{"helper"}
Truth table50%1/2
A
A= $config->{"helper"}
| |||||||||||||
| 27 | 1 | ▶ | $app->attr("_clog", sub { return $self; }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 28 | $app->helper( | ||||||||||||
| 29 | 1 | ▶ | $config->{helper} => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | 1 | my $self = shift; | |||||||||||
| 31 | 1 | return $self->app->_clog; | |||||||||||
| 32 | } | ||||||||||||
| 33 | 1 | ); | |||||||||||
| 34 | |||||||||||||
| 35 | # export alias of this logger | ||||||||||||
| 36 | # | ||||||||||||
| 37 | # alias is very useful when $app can not be easily accessed | ||||||||||||
| 38 | # for example, using $Alias::CLog->debug instead of $app->clog | ||||||||||||
| 39 | # | ||||||||||||
| 40 | # however, this will currupt other namespace | ||||||||||||
| 41 | # so please use this with caution | ||||||||||||
| 42 | 1 | ▶ | if ($config->{alias}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 1 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | 1 | *{"$config->{alias}::CLog"} = \$self; | |||||||||||
| 45 | } | ||||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 1 | ▶ | sub debug { | ||||||||||
|
Subroutine
debug: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 49 | 1 | my $self = shift; | |||||||||||
| 50 | 1 | my $target = shift; | |||||||||||
| 51 | 1 | my @contents = @_; | |||||||||||
| 52 | 1 | $self->_out($target, 'debug', @contents); | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | 0 | ▶ | sub info { | ||||||||||
|
Subroutine
info: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||
| 57 | 0 | my $target = shift; | |||||||||||
| 58 | 0 | my @contents = @_; | |||||||||||
| 59 | 0 | $self->_out($target, 'info', @contents); | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | 0 | ▶ | sub warn { | ||||||||||
|
Subroutine
warn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 63 | 0 | my $self = shift; | |||||||||||
| 64 | 0 | my $target = shift; | |||||||||||
| 65 | 0 | my @contents = @_; | |||||||||||
| 66 | 0 | $self->_out($target, 'warn', @contents); | |||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 1 | ▶ | sub error { | ||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 70 | 1 | my $self = shift; | |||||||||||
| 71 | 1 | my $target = shift; | |||||||||||
| 72 | 1 | my @contents = @_; | |||||||||||
| 73 | 1 | $self->_out($target, 'error', @contents); | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 0 | ▶ | sub fatal { | ||||||||||
|
Subroutine
fatal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 77 | 0 | my $self = shift; | |||||||||||
| 78 | 0 | my $target = shift; | |||||||||||
| 79 | 0 | my @contents = @_; | |||||||||||
| 80 | 0 | $self->_out($target, 'fatal', @contents); | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | 2 | ▶ | sub _out { | ||||||||||
|
Subroutine
_out: called CC=2 SCAR=7.0
| |||||||||||||
| 84 | 2 | my $self = shift; | |||||||||||
| 85 | 2 | my $target = shift; | |||||||||||
| 86 | 2 | my $level = shift; | |||||||||||
| 87 | 2 | my @contents = @_; | |||||||||||
| 88 | |||||||||||||
| 89 | 2 | my $path = $PATH->{$target}; | |||||||||||
| 90 | |||||||||||||
| 91 | 2 | ▶ | if (!$path) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | 0 | die "Undefined target: $target. path: $path"; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | 2 | my $log = join("\t", @contents); | |||||||||||
| 96 | |||||||||||||
| 97 | # remove line break for contents | ||||||||||||
| 98 | # there should always be one line per output | ||||||||||||
| 99 | 2 | $log =~ s/\n//g; | |||||||||||
| 100 | |||||||||||||
| 101 | 2 | $self->_append($path, $self->_format($level, $log)); | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 2 | ▶ | sub _append { | ||||||||||
|
Subroutine
_append: called CC=3 SCAR=11.1
| |||||||||||||
| 105 | 2 | my $self = shift; | |||||||||||
| 106 | 2 | my ($path, $log) = @_; | |||||||||||
| 107 | |||||||||||||
| 108 | 2 | $path .= "." . _get_local_date(); | |||||||||||
| 109 | 2 | ▶ | open HANDLE, ">>", $path or die "Can not open file $path"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | |||||||||||||
| 111 | 2 | flock HANDLE, LOCK_EX; | |||||||||||
| 112 | 2 | ▶ | HANDLE->print(encode('UTF-8', $log)) or die "Can't write to log: $!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 2 | flock HANDLE, LOCK_UN; | |||||||||||
| 114 | |||||||||||||
| 115 | 2 | close HANDLE; | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | 2 | ▶ | sub _format { | ||||||||||
|
Subroutine
_format: called CC=1 SCAR=0.0
| |||||||||||||
| 119 | 2 | my $self = shift; | |||||||||||
| 120 | 2 | return'[' . localtime . '] [' . shift . '] ' . shift . "\n"; | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | 2 | ▶ | sub _get_local_date { | ||||||||||
|
Subroutine
_get_local_date: called CC=1 SCAR=0.0
| |||||||||||||
| 124 | 2 | my ($second, $minute, $hour, $day, $month, $year, $weekday, $yesterday, $is_dst) = localtime; | |||||||||||
| 125 | 2 | return sprintf("%04d%02d%02d", $year + 1900, $month + 1, $day); | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | 1 | 1; | |||||||||||
| 129 | |||||||||||||
| 130 | __END__ | ||||||||||||