开发者

a way to free hslogger handler info

I have program where I'm using hlogger next way, I have main thread and arbitrary number of workers thread, wich one should write logs into separate file. So I'm using:

 s <- openlog ..
 updateGlobalLogger ("myapp."++threadId) (addHandler s)

But it seems that this way will consume a log of memory for storing handlers for threads and I've found to way to dispose this information at the end of thread work.

Can somebody adv开发者_JAVA技巧ice better way of storing info or a way to free memory.


If you're concerned about Handles being left open after each thread is finished, you can wrap forkIO in a helper that will create the handler you want, run the thread with the right log name, and then close the handle afterwards:

import Control.Concurrent
import Control.Exception 
import System.Log.Logger 
import System.Log.Handler 
import System.Log.Handler.Simple

forkWithLogName :: (String -> IO ()) -> IO ThreadId
forkWithLogName c = 
  forkIO $ do tid <- myThreadId
              let logName = "myapp." ++ (show tid)
              bracket (fileHandler logName DEBUG)
                      close
                      (\h -> do updateGlobalLogger logName (addHandler h)
                                c logName)

main = do
  forkWithLogName $ \logName -> do
         errorM logName "Foo"
  forkWithLogName $ \logName -> do
         errorM logName "Bar"

Update: now handles exceptions properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜