anchor.diff:  code_change(_OldVsn, State, _Extra) ->
       {ok, State}.
   
  -write({Name, L, FD, Inode}, Level, Msg) ->
  +write(#file{name=Name, fd=FD, inode=Inode, flap=Flap} = File, Level, Msg) ->
       case lager_util:ensure_logfile(Name, FD, Inode, true) of
           {ok, {NewFD, NewInode}} ->
               file:write(NewFD, Msg),
               case Level of
                   _ when Level >= 4 ->
                       %% force a sync on any message at error severity or above
  -                    file:datasync(NewFD);
  -                _ -> ok
  -            end,
  -            {Name, L, NewFD, NewInode};
  -        Error ->
  -            lager:error("Failed to reopen logfile ~s with error ~w", [Name,
  -                    Error]),
  -            undefined
  +                    Flap2 = case file:datasync(NewFD) of
  +                        {error, Reason2} when Flap == false ->
  +                            ?LOG(error, "Failed to write log message to file ~s: ~s", [Name, file:format_error(Reason2)]),
  +                            true;
  +                        ok ->
  +                            false;
  +                        _ ->
  +                            Flap
  +                    end,
  +                    File#file{fd=NewFD, inode=NewInode, flap=Flap2};
  +                _ -> 
  +                    File#file{fd=NewFD, inode=NewInode}
  +            end;
  +        {error, Reason} ->
  +            case Flap of
  +                true ->
  +                    File;
  +                _ ->
  +                    ?LOG(error, "Failed to reopen logfile ~s with error ~w", [Name, file:format_info(Reason)]),
  +                    File#file{flap=true}
  +            end
       end.
   
   -ifdef(TEST).
  
positive.code: 
  -record(state, {files}).
  
  init(LogFiles) ->
      Files = [begin
                  case lager_util:open_logfile(Name, true) of
                      {ok, {FD, Inode}} ->
                          {Name, lager_util:level_to_num(Level), FD, Inode};
                      Error ->
                          lager:error("Failed to open log file ~s with error ~p",
                              [Name, Error]),
                          undefined
                  end
          end ||
          {Name, Level} <- LogFiles],
  
negative.code: get_loglevel_test() ->
      {ok, Level, _} = handle_call(get_loglevel,
          #state{files=[
                  #file{name="foo", level=lager_util:level_to_num(warning), fd=0, inode=0},
                  #file{name="bar", level=lager_util:level_to_num(info), fd=0, inode=0}]}),
      ?assertEqual(Level, lager_util:level_to_num(info)),
      {ok, Level2, _} = handle_call(get_loglevel,
          #state{files=[
                  #file{name="foo", level=lager_util:level_to_num(warning), fd=0, inode=0},
                  #file{name="foo", level=lager_util:level_to_num(critical), fd=0, inode=0},
                  #file{name="bar", level=lager_util:level_to_num(error), fd=0, inode=0}]}),
      ?assertEqual(Level2, lager_util:level_to_num(warning)).
  
  rotation_test() ->
      {ok, {FD, Inode}} = lager_util:open_logfile("test.log", true),
      ?assertMatch(#file{name="test.log", level=0, fd=FD, inode=Inode},
          write(#file{name="test.log", level=0, fd=FD, inode=Inode}, 0, "hello world")),
      file:delete("test.log"),
      Result = write(#file{name="test.log", level=0, fd=FD, inode=Inode}, 0, "hello world"),
      %% assert file has changed
      ?assert(#file{name="test.log", level=0, fd=FD, inode=Inode} =/= Result),
      ?assertMatch(#file{name="test.log", level=0}, Result),
      file:rename("test.log", "test.log.1"),
      Result2 = write(Result, 0, "hello world"),
      %% assert file has changed
      ?assert(Result =/= Result2),
      ?assertMatch(#file{name="test.log", level=0}, Result2),
      ok.
  
  -endif.
  