anchor.diff:    {ok, State}.
     
   % INTERNAL FUNCTIONS
  -write(Level, Message, #state{log_handle = Fd} = _State) ->
  -  % Check levels here, eventually
  +
  +ensure_logfile_exists(FullFilepath) ->
  +  case (catch file:open(FullFilepath, [append])) of
  +    {ok, F} -> F;
  +    _E -> 
  +      {ok, F} = file:open(FullFilepath, [write]),
  +      F
  +  end.
  +
  +% write(LogLevel, File, Line, io_lib:format(LogFormat, LogArgs), State),
  +write(error, _File, _Line, Message, #state{log_handle = Fd, log_level = debug} = State) ->
  +  Msg = io_lib:format("! [~s] [~s] ~s\r\n", [httpd_util:rfc1123_date(), error, Message]),
  +  write_to_console(Msg),
  +  write_to_file(Fd, Msg),
  +  State;
  +write(debug, File, Line, Message, #state{log_level = debug} = State) ->
  +  Msg = io_lib:format("[~s:~p] [~s] [~s] ~s\r\n", [File, Line, httpd_util:rfc1123_date(), debug, Message]),
  +  write_to_console(Msg),
  +  State;
  +write(Level, _File, _Line, Message, #state{log_handle = Fd} = State) ->
     Msg = io_lib:format("[~s] [~s] ~s\r\n", [httpd_util:rfc1123_date(), Level, Message]),
  -  io:format("~s", [Msg]),     % write to console 
  -  io:format(Fd, "~s", [Msg]). % write to file
  +  write_to_console(Msg),
  +  write_to_file(Fd, Msg),
  +  State.
  +
  +write_to_console(Msg) -> io:format("~s", [Msg]).
  +write_to_file(Fd, Msg) -> io:format(Fd, "~s", [Msg]).
positive.code: %% gen_event:notify/2 or gen_event:sync_notify/2, this function is called for
  %% each installed event handler to handle the event.
  %%--------------------------------------------------------------------
  handle_event({log, LogLevel, LogFormat, LogArgs}, State) ->
    write(LogLevel, io_lib:format(LogFormat, LogArgs), State),
    {ok, State};
  handle_event(_Event, State) ->
    {ok, State}.
  
  
negative.code: 
  -record (state, {
    filename,
    log_level,
    log_handle
  }).
  
  % Log levels
  %   0 - quiet (no logs)
  %   1 - error messages only
  %   2 - verbose
  %   3 - full logs with debugging messages
  %   4 - heavy logging with speed
  
  %%====================================================================
  %% gen_event callbacks
  %%====================================================================
  