问题描述
import System.Environment
import System.Log.Logger
import System.Log.Handler(setFormatter)
import System.Log.Handler.Simple(streamHandler)
import System.Log.Formatter $ b $ import System.IO(getLine,stdout)
main = do
stdOutHandler< - streamHandler stdout DEBUG>> = \ lh - >返回$
setFormatter lh(simpleLogFormatter[$ time:$ loggername:$ prio] $ msg)
updateGlobalLoggerLinker(setLevel DEBUG。setHandlers [stdOutHandler])
infoMLinker 你好,世界!
不幸的是,每次我使用 infoM
(或任何日志记录功能),我得到重复的行,例如
infoMLinkerhi there
产生:
hi有
[2016-12-05 20:23:10 GMT:Linker:信息] hi there
我认为 setHandlers
首先删除了其他处理程序。
我只需要格式化的行,而不是常规格式ala putStrLn
等。
我在程序中发现错误。实际上,它是在你的第一个代码中,我只是没有足够的注意它:($ /
所有你需要的是用
updateGlobalLoggerLinker
to
updateGlobalLogger rootLoggerName
这对我有用,我不知道当你没有使用root logger进行初始化时会发生什么,但现在它至少可以工作。
另外,如果你使用 stack
并且不介意使用 github
项目,那么你可能会希望考虑使用我们的日志库(它目前不在hackage中),它是一个围绕 hslogger
的包装器,它增加了一些汁液(比如彩色记录器名称等) p>
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.
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
这篇关于hslogger&重复的日志行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!