Alex
Alex

Reputation: 8303

hslogger & Duplicate Log Lines

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.

Upvotes: 2

Views: 150

Answers (1)

Shersh
Shersh

Reputation: 9169

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

Colored logging

Upvotes: 3

Related Questions