snoyberg / monad-logger

A class of monads which can log messages
MIT License
36 stars 22 forks source link

Format #10

Open dschrempf opened 5 years ago

dschrempf commented 5 years ago

I would like to use the monad-logger framework but find it very difficult to change the format of log messages.

I managed to write my own runFileLoggingT function, which uses not the defaultOutput function, but a custom function preparing the output. The rewrite, however, requires considerable effort and is by no means user-friendly. For example, I had to tinker around with MonadBaseControl.

Further, is it possible to use time stamps in my log messaes without writing my own runFileLoggingT?

Do you think it is possible to provide an easier way to format the log messages? It may well be that I missed how monad-logger should be used. In this case, it would be good to provide some more documentation.

Thank you for your help!

Edit: I also wanted to log to stderr and a file at the same time. Does it also require a custom runLoggingT implementation?

zzantares commented 4 years ago

Hello @dschrempf I'm also interested to logging out to multiple destinations + timestamps, did you managed to do that? any pointers?

dschrempf commented 4 years ago

Hi Julio,

what I do now is define a custom logging transformer that logs to two handles. Super annoying, but it works. Here is the code (ELynx is the package name, which is irrelevant here):

runELynxFileLoggingT :: MonadBaseControl IO m => FilePath -> LoggingT m a -> m a
runELynxFileLoggingT fp logger = bracket
    (liftBase $ openFile fp AppendMode)
    (liftBase . hClose)
    $ \h -> liftBase (hSetBuffering h LineBuffering) >> runLoggingT logger (output2H stderr h)

output2H :: Handle
         -> Handle
         -> Loc
         -> LogSource
         -> LogLevel
         -> LogStr
         -> IO ()
output2H h1 h2 loc src lvl msg = do
  B.hPutStr h1 ls
  B.hPutStr h2 ls
  where
    ls = logStrToBS loc src lvl msg

logStrToBS :: Loc
           -> LogSource
           -> LogLevel
           -> LogStr
           -> B.ByteString
logStrToBS _ _ _ msg = fromLogStr msg <> "\n"

I hope I could help!

Dominik

Julio César notifications@github.com writes:

Hello @dschrempf I'm also interested to logging out to multiple destinations + timestamps, did you managed to do that? any pointers?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

zzantares commented 4 years ago

Thank you! I was about to go down this route but in the end I settled for Katip which has support out of the box for what I needed, timestamps are added by default and writting to multiple targets is done by defining scribes.