Created
June 27, 2011 19:47
-
-
Save sharifulin/1049651 to your computer and use it in GitHub Desktop.
Mojo::Log::My
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use common::sense; | |
| use lib 'lib'; | |
| my $log = Mojo::Log::My->new; | |
| $log->warn('sss'); | |
| package Mojo::Log::My; | |
| use base 'Mojo::Log'; | |
| use Fcntl ':flock'; | |
| sub log { | |
| my ($self, $level, @msgs) = @_; | |
| # Check log level | |
| $level = lc $level; | |
| return $self unless $level && $self->is_level($level); | |
| # Caller | |
| my ($pkg, $line) = (caller())[0, 2]; | |
| ($pkg, $line) = (caller(1))[0, 2] if $pkg eq ref $self; | |
| # Lock | |
| my $handle = $self->handle; | |
| flock $handle, LOCK_EX; | |
| # Log messages | |
| my $time = $self->_time2iso; | |
| my $msgs = join "\n", | |
| map { utf8::decode $_ unless utf8::is_utf8 $_; $_ } @msgs; | |
| $handle->syswrite("$time $level $pkg:$line [$$]: $msgs\n"); | |
| # Unlock | |
| flock $handle, LOCK_UN; | |
| $self; | |
| } | |
| sub _time2iso { | |
| shift; | |
| return sprintf "%4d-%02d-%02d %02d:%02d:%02d", $_->[5]+1900, $_->[4]+1, reverse @$_[0..3] for [localtime(shift||time)]; | |
| } | |
| 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment