hslogger & Duplicate Log Lines

混江龙づ霸主 提交于 2020-01-04 07:01:59

问题


I've configured logging like so:

import System.Environment
import System.Log.Logger
import System.Log.Handler (setFormatter)
import System.Log.Handler.Simple (streamHandler)
import System.Log.Formatter
import System.IO (getLine, stdout)
main = do
stdOutHandler <- streamHandler stdout DEBUG >>= \lh -> return $
            setFormatter lh (simpleLogFormatter "[$time : $loggername : $prio] $msg")
  updateGlobalLogger "Linker" (setLevel DEBUG . setHandlers [stdOutHandler])
  infoM "Linker" "Hello world!"

Unfortunately, every time I use infoM (or any logging function), I get duplicate lines, e.g.

infoM "Linker" "hi there"

produces:

hi there
[2016-12-05 20:23:10 GMT : Linker : INFO] hi there

I thought setHandlers removed other handlers first.

I want just the lines that are formatted, not the "normal" format ala putStrLn etc.


回答1:


I found error in your program. Actually, it was in your first code, I just didn't pay enough attention to it :(

All you need is to replace logger name with rootLoggerName in

updateGlobalLogger "Linker"

to

updateGlobalLogger rootLoggerName

This did the trick for me. I don't know what happens when you're not initializing with root logger but now it will at least work.

Also, if you're using stack and don't mind using github projects then you may wish to consider using our logging library (it is not currently on hackage) which is a wrapper around hslogger which adds some juice to it (like coloured logger names and more):

https://github.com/serokell/log-warper



来源:https://stackoverflow.com/questions/40982746/hslogger-duplicate-log-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!