Skip to content

Instantly share code, notes, and snippets.

@garrensmith
Created November 8, 2012 09:12
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 garrensmith/4037682 to your computer and use it in GitHub Desktop.
Save garrensmith/4037682 to your computer and use it in GitHub Desktop.
Json Log code
%Replace from line 251 in couch_httpd_misc_handlers.erl
% httpd log handlers
handle_log_req(#httpd{method='GET'}=Req) ->
ok = couch_httpd:verify_is_server_admin(Req),
Bytes = list_to_integer(couch_httpd:qs_value(Req, "bytes", "1000")),
Offset = list_to_integer(couch_httpd:qs_value(Req, "offset", "0")),
Chunk = couch_log:read(Bytes, Offset),
case couch_httpd:qs_value(Req, "format", "text") of
"json" ->
send_json(Req, 200, parse_to_json(list_to_binary(Chunk)));
_ ->
{ok, Resp} = start_chunked_response(Req, 200, [
% send a plaintext response
{"Content-Type", "text/plain; charset=utf-8"},
{"Content-Length", integer_to_list(length(Chunk))}
]),
send_chunk(Resp, Chunk),
last_chunk(Resp)
end;
handle_log_req(#httpd{method='POST'}=Req) ->
{PostBody} = couch_httpd:json_body_obj(Req),
Level = couch_util:get_value(<<"level">>, PostBody),
Message = ?b2l(couch_util:get_value(<<"message">>, PostBody)),
case Level of
<<"debug">> ->
?LOG_DEBUG(Message, []),
send_json(Req, 200, {[{ok, true}]});
<<"info">> ->
?LOG_INFO(Message, []),
send_json(Req, 200, {[{ok, true}]});
<<"error">> ->
?LOG_ERROR(Message, []),
send_json(Req, 200, {[{ok, true}]});
_ ->
send_json(Req, 400, {[{error, ?l2b(io_lib:format("Unrecognized log level '~s'", [Level]))}]})
end;
handle_log_req(Req) ->
send_method_not_allowed(Req, "GET,POST").
parse_to_json(Input) ->
LogLines = binary:split(Input,<<"\n">>, [global]),
GroupedLines = lists:foldl(fun split_log_parts/2, [], LogLines),
lists:foldr(fun format_for_json/2, [], GroupedLines).
split_log_parts(Line, Acc) ->
CleanedLine = binary:replace(Line, [<<" [">>,<<"[">>], <<"">>,[global]),
[ binary:split(CleanedLine, <<"]">>, [global]) | Acc].
format_for_json(LogLine, Acc) when length(LogLine) == 4 ->
[Date, LogLevel, Pid, Text] = LogLine,
[ {[ {date, Date}, {loglevel, LogLevel}, {pid, Pid}, {text, Text} ]} | Acc ];
% Ignore lines that were not fully fetched from logfile
format_for_json(_LogLine, Acc) ->
Acc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment