Skip to content

Instantly share code, notes, and snippets.

@clintongormley
Created February 11, 2011 11:53
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 clintongormley/822246 to your computer and use it in GitHub Desktop.
Save clintongormley/822246 to your computer and use it in GitHub Desktop.
ES logging
#===================================
sub log_request {
#===================================
my $self = shift;
my $log = $self->_log_fh or return;
my $server = shift;
my $params = shift;
my $data = $params->{data};
if ( defined $data ) {
$data =~ s/'/\\u0027/g;
$data = " -d '\n${data}'";
}
else {
$data = '';
}
printf $log (
"# [%s] Protocol: %s, Server: %s\n",
scalar localtime(),
$self->protocol, ${server}
);
my $uri = $self->http_uri( '127.0.0.1:9200', @{$params}{ 'cmd', 'qs' } );
my $method = $params->{method};
print $log "curl -X$method '$uri' ${data}\n\n";
}
#===================================
sub log_response {
#===================================
my $self = shift;
my $log = $self->_log_fh or return;
my $content = shift;
my $out = ref $content ? $self->JSON->encode($content) : $content;
my @lines = split /\n/, $out;
printf $log ( "# [%s] Response:\n", scalar localtime() );
while (@lines) {
my $line = shift @lines;
if ( length $line > 65 ) {
my ($spaces) = ( $line =~ /^(?:> )?(\s*)/ );
$spaces = substr( $spaces, 0, 20 ) if length $spaces > 20;
unshift @lines, '> ' . $spaces . substr( $line, 65 );
$line = substr $line, 0, 65;
}
print $log "# $line\n";
}
print $log "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment