kazu-yamamoto/logger

wai-logger as middleware - openFile: Resource busy (file is locked)

Closed this issue · 1 comments

Hi
I have integrated Wai-logger as middleware and I am using it in context of Scotty.

I am trying to log to a file and the first request is properly logged to a file but afterwards the error openFile: Resource busy (file is locked) is printed for all subsequent requests.

My implementation is:

loggerMiddleWare :: Wai.Middleware
loggerMiddleWare (app::Wai.Application) (req::Request) (fRes::(Response -> IO ResponseReceived)) = 
    app req myResponseFunction
    where 
        myResponseFunction :: Response -> IO ResponseReceived
        myResponseFunction response = do 
            logger' <- logger
            logger' req (Wai.responseStatus response) Nothing
            fRes response

logger :: IO (Logger.ApacheLogger)
logger = createLoggerActions >>= pure . Logger.apacheLogger 
            where 
                createLoggerActions :: IO Logger.ApacheLoggerActions
                createLoggerActions = let 
                    -- 5 MB in bytes
                    mb5InBytes = 5242880
                    -- defaultBufSize From fast-logger default
                    defaultBufSize = 4096 in 
                        Logger.initLogger 
                        Logger.FromFallback 
                        (Logger.LogFile Logger.FileLogSpec {Logger.log_file = "reqLogger", Logger.log_file_size = mb5InBytes, Logger.log_backup_number = 5} defaultBufSize)  
                         ( return $ B.pack "%d/%b/%Y:%T %z")

And it is added as middleware:

                    Web.Scotty.middleware $ loggerMiddleWare

Full source is here: https://github.com/INTO-CPS-Association/utilities_backend/blob/277cd471355ad6dcadba8f08e70a910417a9c732/src/Lib.hs

I hope you can give me a clue on how to solve this, thank you!