Permalink
Browse files

added basic log4erl support

  • Loading branch information...
1 parent fe7dc1f commit 5096cb55e4f6b803334eea521e38f1ac953eb2e5 ingo committed Jul 5, 2010
Showing with 279 additions and 106 deletions.
  1. +2 −0 README
  2. +13 −1 conf.sh
  3. +1 −0 erl/Makefile
  4. +3 −1 erl/bin/shell
  5. +1 −0 erl/priv/DEPENDENCIES
  6. +3 −3 erl/src/ej_app.erl
  7. +175 −0 erl/src/ej_log.erl
  8. +35 −29 erl/src/ej_srv.erl
  9. +16 −7 erl/src/ej_sup.erl
  10. +0 −49 erl/src/log.erl
  11. +14 −0 erl/src/neo4j.erl
  12. +3 −3 erl/src/neo4j_app.erl
  13. +11 −11 erl/src/neo4j_srv.erl
  14. +2 −2 erl/src/neo4j_sup.erl
View
2 README
@@ -19,6 +19,8 @@ geronimo-jta_1.1_spec-1.1.1.jar
neo4j-kernel-1.1.jar
neo4j-index-1.1.jar
+log4erl-0.9.0
+
NOTE: Dependency management is by hand for now.
Later we may use Ivy or Maven for Java dependencies.
View
14 conf.sh
@@ -6,6 +6,7 @@ SCRIPT_DIR="`( cd \"${SCRIPT_DIR}\" && pwd )`"
JAVA_DIST=$SCRIPT_DIR/java/dist
JAVA_LIB=$SCRIPT_DIR/java/lib
+ERL_LIB=$SCRIPT_DIR/erl/priv
if [ -f conf.cache ]; then
echo "reading some vars from conf.cache"
@@ -100,7 +101,18 @@ do
fi
done
-
+echo "checking Erlang dependencies"
+EDEPS=`cat $ERL_LIB/DEPENDENCIES`
+for DEP in $EDEPS
+do
+ if [ -d $ERL_LIB/$DEP ]; then
+ echo "found $ERL_LIB/$DEP"
+ else
+ echo "ERROR: not found $ERL_LIB/$DEP"
+ echo "------ please copy $DEP into $ERL_LIB and try again"
+ exit 1
+ fi
+done
View
@@ -1,5 +1,6 @@
all:
(cd src;$(MAKE) all)
+ (cd priv/log4erl-0.9.0;$(MAKE))
(cd test;$(MAKE) all)
final: clean
View
@@ -10,8 +10,10 @@ export ERL_HOME
export JAVA_HOME
export ERL_CRASH_DUMP=${SCRIPT_DIR}/erl_crash.dump
+LOG4ERL=../priv/log4erl-0.9.0/ebin
+
ERL=${ERL_HOME}/bin/erl
-ERL_FLAGS="+K true +S 1:1 -pa ${SCRIPT_DIR}/../ebin -setcookie `cat ${SCRIPT_DIR}/cookie`"
+ERL_FLAGS="+K true +S 1:1 -pa ${SCRIPT_DIR}/../ebin -pz ${LOG4ERL} -setcookie `cat ${SCRIPT_DIR}/cookie`"
NAME="shell"
View
@@ -0,0 +1 @@
+log4erl-0.9.0
View
@@ -21,7 +21,6 @@ stop() ->
application:stop(?APPNAME).
start(Type, Args) ->
- log:info(self(), "starting; type: ~p args: ~p", [Type,Args]),
application:set_env(?APPNAME, listeners, sets:new()),
case Type of
normal -> ej_sup:start_link([]);
@@ -30,13 +29,14 @@ start(Type, Args) ->
end.
prep_stop(State) ->
- log:info(self(), "prepare stopping with state: ~p", [State]),
+ ej_log:info("prepare stopping with state: ~p", [State]),
ej_srv:stop(),
timer:sleep(1000),
ok.
stop(State) ->
- log:info(self(), "stopping with state: ~p", [State]),
+ ej_log:info("stopping with state: ~p", [State]),
+ ej_log:stop(),
ok.
getenv(K,Def) ->
View
@@ -0,0 +1,175 @@
+
+%% logging interface
+%%
+%% author: ingo schramm
+
+-module(ej_log).
+-export([fatal/2, err/2, error/2, warn/2, warning/2, info/2, debug/2]).
+% gen_server exports
+-export([start/0, start_link/0, stop/0]).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+
+-export([log/5,set/2]).
+
+-author("Ingo Schramm").
+
+-include("global.hrl").
+-include_lib("eunit/include/eunit.hrl").
+
+-record(state,
+ {verbosity = verbose
+ ,f = fun(Who, Node, Level, Msg, Args) -> print_log({Who,Node}, Level, Msg, Args) end
+ }).
+
+% @doc Write a logging message.
+% @spec log(Who::pid(), Node::node(), L::Level, Msg::string(), Args::list()) -> ok
+% where
+% Level = 'FATAL' + 'ERROR' + 'WARNING' + 'INFO' + 'DEBUG'
+log(Who, Node, Level, Msg, Args) ->
+ gen_server:cast({global,?MODULE}, {Who, Node, Level, Msg, Args}).
+
+log(Level, Msg, Args) ->
+ gen_server:cast({global,?MODULE}, {self(), node(), Level, Msg, Args}).
+
+fatal(Msg, Args) ->
+ log(fatal, Msg, Args).
+
+error(Msg, Args) ->
+ err(Msg, Args).
+err(Msg, Args) ->
+ log(error, Msg, Args).
+
+warning(Msg, Args) ->
+ warn(Msg, Args).
+warn(Msg, Args) ->
+ log(warn, Msg, Args).
+
+info(Msg, Args) ->
+ log(info, Msg, Args).
+
+debug(Msg, Args) ->
+ log(debug, Msg, Args).
+
+% @doc Set a log server property.
+%
+% Avalable properties:
+% <pre>
+% verbosity - silent | verbose
+% </pre>
+%
+% @spec set(Name::atom(), Value::any()) -> ok
+set(verbosity, silent) ->
+ gen_server:cast({global,?MODULE}, {set, {verbosity, silent}});
+set(verbosity, verbose) ->
+ gen_server:cast({global,?MODULE}, {set, {verbosity, verbose}}).
+
+% @doc Start the global log server.
+% @spec start() -> {ok, Pid::pid()} | {error, Reason::any()}
+start() ->
+ start_link().
+
+% @hidden
+start_link() ->
+ gen_server:start_link({global, ?MODULE}, ?MODULE, #state{}, []).
+
+% @doc Stop the global log server.
+% @spec stop() -> ok
+stop() ->
+ gen_server:cast({global, ?MODULE},{'STOP'}),
+ global:unregister_name(?MODULE).
+
+% @hidden
+init(State) ->
+ S1 =
+ case has_log4erl() of
+ true ->
+ log4erl:add_file_appender(file, {"../log", "ej_erl", {size, 100000}, 4, "log", debug}),
+ log4erl:change_format(file1, "[%L] %j %T %l%n"),
+ State#state{f = fun(Who, Node, Level, Msg, Args) -> log4(Who, Node, Level, Msg, Args) end};
+ false -> State
+ end,
+ info("logger is working", []),
+ {ok,S1}.
+
+% @hidden
+handle_call({Who, Level, Msg, Args},_From,State) ->
+ log:print_log(Who, Level, Msg, Args),
+ {noreply, State}.
+
+% @hidden
+handle_cast({Who, Node, Level, Msg, Args}, State) ->
+ case State#state.verbosity of
+ %verbose -> log:print_log({Who,Node}, Level, Msg, Args);
+ verbose -> F = State#state.f,
+ F(Who, Node, Level, Msg, Args);
+ _Any -> ok
+ end,
+ {noreply, State};
+handle_cast({set, {K,V}},State) ->
+ NewState =
+ case K of
+ verbosity -> State#state{verbosity = V};
+ _Any -> State
+ end,
+ {noreply, NewState};
+handle_cast({'STOP'}, State) ->
+ log:info(self(),"stopping with state: ~p", [State]),
+ {stop, normal, State};
+handle_cast(Msg,State) ->
+ log:print_log(self(), 'ERROR', "log_srv cannot understand: ~p", [Msg]),
+ {noreply, State}.
+
+% @hidden
+handle_info(_Msg,State) ->
+ {noreply, State}.
+
+% @hidden
+terminate(_Reason,State) ->
+ {noreply, State}.
+
+% @hidden
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+
+%% ----- PRIVATE PARTS -----
+
+print_log(Who, Level, Msg, Args) ->
+ {Year, Month, Day} = erlang:date(),
+ {Hour, Minute, Second} = erlang:time(),
+ {_Megas, _Secs, Micros} = erlang:now(),
+ List = [Level,Year,Month,Day,Hour,Minute,Second,Micros,Who|Args],
+ io:format("~p ~p/~p/~p-~p:~p:~p.~p ~p -> " ++ Msg ++ "~n", List).
+
+log4(Who, Node, Level, Msg, Args) ->
+ List = [{Who, Node}|Args],
+ log4erl:log(Level, " ~p -> " ++ Msg, List).
+
+has_log4erl() ->
+ case lists:keyfind(log4erl, 1, application:which_applications()) of
+ false -> false;
+ {log4erl, _, _} -> true
+ end.
+
+%% ----- TESTS -----
+
+start_stop_test() ->
+ start(),
+ timer:sleep(100),
+ stop(),
+ start(),
+ timer:sleep(100),
+ stop().
+
+
+
+
+
+
+
+
+
+
+
+
+
Oops, something went wrong.

0 comments on commit 5096cb5

Please sign in to comment.