Skip to content

Instantly share code, notes, and snippets.

@brianmed
Last active January 2, 2016 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmed/8309729 to your computer and use it in GitHub Desktop.
Save brianmed/8309729 to your computer and use it in GitHub Desktop.
Attempt to encode the formatted log value given it's destination...
has dest => "file";
sub format {
my ($self, $level, @lines) = @_;
my $txt = '[' . localtime(time) . "] [$level] " . join("\n", @lines, '');
return "file" eq $self->dest ? encode('UTF-8', $txt) : $txt;
}
# ...
sub _message {
my ($self, $level) = (shift, shift);
return unless $self->is_level($level) && (my $handle = $self->handle);
my $max = $self->max_history_size;
my $history = $self->history;
$self->dest("history");
push @$history, $self->format($level, @_);
$self->dest("file");
shift @$history while @$history > $max;
flock $handle, LOCK_EX;
$handle->print($self->format($level, @_)) or croak "Can't write to log: $!";
flock $handle, LOCK_UN;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment