kazu-yamamoto/logger

IOException of type ResourceBusy (openFile: resource busy (file is locked))

Closed this issue · 3 comments

Greetings.

I have that issue as soon as my code tries to write something to a file (It works when I use LogStdout for instance). The file I use doesn't exist before running my program. It's created properly by the library but it's empty (and got the error mentioned in the title).

OS: OSX Sierra
Library version: 2.4.10
GHC: 8.0.2
GHC OPTIONS: -threaded -rtsopts -with-rtsopts=-N

Here's how I use the code (with logMsg function):

--------------------------------------------------------------------------------
data LogManager =
  LogManager { logCallback :: TimedFastLogger
             , logLevel    :: LogLevel
             , _cleanUp    :: IO ()
             }

--------------------------------------------------------------------------------
data Logger =
  Logger { loggerName      :: Text
         , _loggerCallback :: TimedFastLogger
         , _loggerLevel    :: LogLevel
         }

--------------------------------------------------------------------------------
data LogLevel
  = Debug
  | Info
  | Warn
  | Error
  | Fatal
  deriving (Eq, Ord, Enum, Bounded)

--------------------------------------------------------------------------------
newLogManager :: LoggerSettings -> IO LogManager
newLogManager setts = do
  cache         <- newTimeCache simpleTimeFormat'
  (callback, cleanup) <- newTimedFastLogger cache (loggerType setts)
  return (LogManager callback (loggerLevel setts) cleanup)

--------------------------------------------------------------------------------
closeLogManager :: LogManager -> IO ()
closeLogManager LogManager{..} = _cleanUp

--------------------------------------------------------------------------------
getLogger :: Text -> LogManager -> Logger
getLogger name mgr =
  Logger { loggerName      = name
         , _loggerCallback = logCallback mgr
         , _loggerLevel    = logLevel mgr
         }

--------------------------------------------------------------------------------
logMsg :: MonadIO m => Logger -> LogLevel -> Text -> m ()
logMsg Logger{..} lvl msg
  | lvl < _loggerLevel = return ()
  | otherwise = liftIO $
    _loggerCallback $ \t ->
      toLogStr ("["`mappend` t `mappend`"]") `mappend` " eventstore "
                               `mappend` toLogStr (logLvlTxt lvl)
                               `mappend` toLogStr ("[" `mappend` loggerName `mappend` "] ")
                               `mappend` toLogStr msg
                               `mappend` "\n"

Thanks for your time.

I also tried on Ubuntu 16.04.2 64 bits and got the exact same error. However this time, the file wasn't empty and had all the expected logs message in it.

It seems to me that other part of your program opens the log file. The base library prohibits to open a file two or more times.

@kazu-yamamoto It turns out my testing library was running my test group in parallel hence the reason you mentioned. Thanks for your help and sorry for the noise.