diff options
Diffstat (limited to 'src/couchdb/couch_log.erl')
-rw-r--r-- | src/couchdb/couch_log.erl | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/src/couchdb/couch_log.erl b/src/couchdb/couch_log.erl index 5a45c207..dfd2d178 100644 --- a/src/couchdb/couch_log.erl +++ b/src/couchdb/couch_log.erl @@ -50,14 +50,23 @@ init([]) -> fun("log", "file") -> ?MODULE:stop(); ("log", "level") -> + ?MODULE:stop(); + ("log", "include_sasl") -> ?MODULE:stop() end), Filename = couch_config:get("log", "file", "couchdb.log"), - Level = couch_config:get("log", "level", "info"), + Level = level_integer(list_to_atom(couch_config:get("log", "level", "info"))), + Sasl = list_to_atom(couch_config:get("log", "include_sasl", "true")), + + case ets:info(?MODULE) of + undefined -> ets:new(?MODULE, [named_table]); + _ -> ok + end, + ets:insert(?MODULE, {level, Level}), {ok, Fd} = file:open(Filename, [append]), - {ok, {Fd, level_integer(list_to_atom(Level))}}. + {ok, {Fd, Level, Sasl}}. debug_on() -> get_level_integer() =< ?LEVEL_DEBUG. @@ -72,29 +81,35 @@ get_level() -> level_atom(get_level_integer()). get_level_integer() -> - catch gen_event:call(error_logger, couch_log, get_level_integer). + try + ets:lookup_element(?MODULE, level, 2) + catch error:badarg -> + ?LEVEL_ERROR + end. set_level_integer(Int) -> gen_event:call(error_logger, couch_log, {set_level_integer, Int}). -handle_event({error_report, _, {Pid, couch_error, {Format, Args}}}, {Fd, _LogLevel}=State) -> - log(Fd, Pid, error, Format, Args), - {ok, State}; -handle_event({error_report, _, {Pid, _, _}}=Event, {Fd, _LogLevel}=State) -> - log(Fd, Pid, error, "~p", [Event]), - {ok, State}; -handle_event({error, _, {Pid, Format, Args}}, {Fd, _LogLevel}=State) -> +handle_event({Pid, couch_error, {Format, Args}}, {Fd, _LogLevel, _Sasl}=State) -> log(Fd, Pid, error, Format, Args), {ok, State}; -handle_event({info_report, _, {Pid, couch_info, {Format, Args}}}, {Fd, LogLevel}=State) +handle_event({Pid, couch_info, {Format, Args}}, {Fd, LogLevel, _Sasl}=State) when LogLevel =< ?LEVEL_INFO -> log(Fd, Pid, info, Format, Args), {ok, State}; -handle_event({info_report, _, {Pid, couch_debug, {Format, Args}}}, {Fd, LogLevel}=State) +handle_event({Pid, couch_debug, {Format, Args}}, {Fd, LogLevel, _Sasl}=State) when LogLevel =< ?LEVEL_DEBUG -> log(Fd, Pid, debug, Format, Args), {ok, State}; -handle_event({_, _, {Pid, _, _}}=Event, {Fd, LogLevel}=State) +handle_event({error_report, _, {Pid, _, _}}=Event, {Fd, _LogLevel, Sasl}=State) +when Sasl =/= false -> + log(Fd, Pid, error, "~p", [Event]), + {ok, State}; +handle_event({error, _, {Pid, Format, Args}}, {Fd, _LogLevel, Sasl}=State) +when Sasl =/= false -> + log(Fd, Pid, error, Format, Args), + {ok, State}; +handle_event({_, _, {Pid, _, _}}=Event, {Fd, LogLevel, _Sasl}=State) when LogLevel =< ?LEVEL_TMI -> % log every remaining event if tmi! log(Fd, Pid, tmi, "~p", [Event]), @@ -102,10 +117,9 @@ when LogLevel =< ?LEVEL_TMI -> handle_event(_Event, State) -> {ok, State}. -handle_call(get_level_integer, {_Fd, LogLevel}=State) -> - {ok, LogLevel, State}; -handle_call({set_level_integer, NewLevel}, {Fd, _LogLevel}) -> - {ok, ok, {Fd, NewLevel}}. +handle_call({set_level_integer, NewLevel}, {Fd, _LogLevel, Sasl}) -> + ets:insert(?MODULE, {level, NewLevel}), + {ok, ok, {Fd, NewLevel, Sasl}}. handle_info(_Info, State) -> {ok, State}. @@ -113,7 +127,7 @@ handle_info(_Info, State) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. -terminate(_Arg, {Fd, _LoggingLevel}) -> +terminate(_Arg, {Fd, _LoggingLevel, _Sasl}) -> file:close(Fd). log(Fd, Pid, Level, Format, Args) -> @@ -121,11 +135,11 @@ log(Fd, Pid, Level, Format, Args) -> ok = io:format("[~s] [~p] ~s~n", [Level, Pid, Msg]), % dump to console too Msg2 = re:replace(lists:flatten(Msg),"\\r\\n|\\r|\\n", "\r\n", [global, {return, list}]), - ok = io:format(Fd, "[~s] [~s] [~p] ~s\r~n\r~n", [httpd_util:rfc1123_date(), Level, Pid, Msg2]). + ok = io:format(Fd, "[~s] [~s] [~p] ~s\r~n", [httpd_util:rfc1123_date(), Level, Pid, Msg2]). read(Bytes, Offset) -> LogFileName = couch_config:get("log", "file"), - LogFileSize = couch_util:file_read_size(LogFileName), + LogFileSize = filelib:file_size(LogFileName), {ok, Fd} = file:open(LogFileName, [read]), Start = lists:max([LogFileSize - Bytes, 0]) + Offset, |