summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_log.erl
diff options
context:
space:
mode:
authorFilipe David Borba Manana <fdmanana@apache.org>2011-02-07 08:27:30 +0000
committerFilipe David Borba Manana <fdmanana@apache.org>2011-02-07 08:27:30 +0000
commit00d2709afbff6f402d91f5d1dc3e5a5075685c49 (patch)
tree4613116a63a9a0bd7f549ac6d1d9613d9732a693 /src/couchdb/couch_log.erl
parentee2fd9c60d9d06609622dbc8d5c6b0570b7dc7ec (diff)
Merged revision 1067873 from trunk
More efficient logging, closes COUCHDB-1054 git-svn-id: https://svn.apache.org/repos/asf/couchdb/branches/1.1.x@1067877 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb/couch_log.erl')
-rw-r--r--src/couchdb/couch_log.erl52
1 files changed, 44 insertions, 8 deletions
diff --git a/src/couchdb/couch_log.erl b/src/couchdb/couch_log.erl
index 3b6ce97c..9813f812 100644
--- a/src/couchdb/couch_log.erl
+++ b/src/couchdb/couch_log.erl
@@ -14,6 +14,7 @@
-behaviour(gen_event).
-export([start_link/0,stop/0]).
+-export([debug/2, info/2, error/2]).
-export([debug_on/0,info_on/0,get_level/0,get_level_integer/0, set_level/1]).
-export([init/1, handle_event/2, terminate/2, code_change/3, handle_info/2, handle_call/2]).
-export([read/2]).
@@ -23,6 +24,29 @@
-define(LEVEL_DEBUG, 1).
-define(LEVEL_TMI, 0).
+debug(Format, Args) ->
+ case debug_on() of
+ false ->
+ ok;
+ true ->
+ {ConsoleMsg, FileMsg} = get_log_messages(self(), debug, Format, Args),
+ gen_event:sync_notify(error_logger, {couch_debug, ConsoleMsg, FileMsg})
+ end.
+
+info(Format, Args) ->
+ case info_on() of
+ false ->
+ ok;
+ true ->
+ {ConsoleMsg, FileMsg} = get_log_messages(self(), info, Format, Args),
+ gen_event:sync_notify(error_logger, {couch_info, ConsoleMsg, FileMsg})
+ end.
+
+error(Format, Args) ->
+ {ConsoleMsg, FileMsg} = get_log_messages(self(), error, Format, Args),
+ gen_event:sync_notify(error_logger, {couch_error, ConsoleMsg, FileMsg}).
+
+
level_integer(error) -> ?LEVEL_ERROR;
level_integer(info) -> ?LEVEL_INFO;
level_integer(debug) -> ?LEVEL_DEBUG;
@@ -96,24 +120,26 @@ get_level_integer() ->
set_level_integer(Int) ->
gen_event:call(error_logger, couch_log, {set_level_integer, Int}).
-handle_event({Pid, couch_error, {Format, Args}}, {Fd, _LogLevel, _Sasl}=State) ->
- log(Fd, Pid, error, Format, Args),
+handle_event({couch_error, ConMsg, FileMsg}, {Fd, _LogLevel, _Sasl}=State) ->
+ log(Fd, ConMsg, FileMsg),
{ok, State};
-handle_event({Pid, couch_info, {Format, Args}}, {Fd, LogLevel, _Sasl}=State)
+handle_event({couch_info, ConMsg, FileMsg}, {Fd, LogLevel, _Sasl}=State)
when LogLevel =< ?LEVEL_INFO ->
- log(Fd, Pid, info, Format, Args),
+ log(Fd, ConMsg, FileMsg),
{ok, State};
-handle_event({Pid, couch_debug, {Format, Args}}, {Fd, LogLevel, _Sasl}=State)
+handle_event({couch_debug, ConMsg, FileMsg}, {Fd, LogLevel, _Sasl}=State)
when LogLevel =< ?LEVEL_DEBUG ->
- log(Fd, Pid, debug, Format, Args),
+ log(Fd, ConMsg, FileMsg),
{ok, State};
handle_event({error_report, _, {Pid, _, _}}=Event, {Fd, _LogLevel, Sasl}=State)
when Sasl =/= false ->
- log(Fd, Pid, error, "~p", [Event]),
+ {ConMsg, FileMsg} = get_log_messages(Pid, error, "~p", [Event]),
+ log(Fd, ConMsg, FileMsg),
{ok, State};
handle_event({error, _, {Pid, Format, Args}}, {Fd, _LogLevel, Sasl}=State)
when Sasl =/= false ->
- log(Fd, Pid, error, Format, Args),
+ {ConMsg, FileMsg} = get_log_messages(Pid, error, Format, Args),
+ log(Fd, ConMsg, FileMsg),
{ok, State};
handle_event({_, _, {Pid, _, _}}=Event, {Fd, LogLevel, _Sasl}=State)
when LogLevel =< ?LEVEL_TMI ->
@@ -143,6 +169,16 @@ log(Fd, Pid, Level, Format, Args) ->
[global, {return, list}]),
ok = io:format(Fd, "[~s] [~s] [~p] ~s\r~n", [httpd_util:rfc1123_date(), Level, Pid, Msg2]).
+log(Fd, ConsoleMsg, FileMsg) ->
+ ok = io:put_chars(ConsoleMsg),
+ ok = io:put_chars(Fd, FileMsg).
+
+get_log_messages(Pid, Level, Format, Args) ->
+ ConsoleMsg = io_lib:format(
+ "[~s] [~p] " ++ Format ++ "~n", [Level, Pid | Args]),
+ FileMsg = ["[", httpd_util:rfc1123_date(), "] ", ConsoleMsg],
+ {iolist_to_binary(ConsoleMsg), iolist_to_binary(FileMsg)}.
+
read(Bytes, Offset) ->
LogFileName = couch_config:get("log", "file"),
LogFileSize = filelib:file_size(LogFileName),