Question about adding timestamp to log message
Closed this issue ยท 8 comments
I'm really stuck with this problem: I already have a message with severity and want to add timestamp in runLoggingT
. Inside runLoggingT
I try to do mapLogMessageM timestamp
on LoggingT message m a
but I do not get it working.
I'm developing Servant application and this is the code with only severity. I am converting custom Servant handler to Servant's default handler. I do not want to add WithTimestamp
to the ApiHandler because then I have add timestamp to each separate log. Can you help me further?
type ApiHandler = ReaderT AppEnv (ExceptT ServantErr (LoggingT (WithSeverity PP.Doc) IO))
toHandler :: AppEnv -> Severity -> (ApiHandler :~> ExceptT ServantErr IO)
toHandler env logLevel = Nat toHandler'
where
toHandler' :: ApiHandler a -> ExceptT ServantErr IO a
toHandler' = ExceptT . doLogging logLevel . runExceptT . flip runReaderT env
doLogging :: Severity
-> LoggingT (WithSeverity PP.Doc) IO (Either ServantErr a)
-> IO (Either ServantErr a)
doLogging minThreshold body =
withFDHandler defaultBatchingOptions stdout 0.4 80 $ \stdoutHandler ->
runLoggingT
body
(\message ->
when (minThreshold >= msgSeverity message) $
(stdoutHandler . renderWithSeverity PP.pretty) message)
Another question/issue. Logging to stdout
goes fine, when I use withFDHandler
with a file handle, no logging appears in log file. Even not when I set flushMaxDelay
to 0
or 1
.
Hmm, maybe this issue is similar as issue #14 .
For the last problem, could you try against master?
I will do that soon.
Will get back to your first question soon!
Thanks! I'm really curious.
For the last problem, could you try against master?
Yes, master fixes the problem ๐
@rikvdkleij if you look at the repo I used for verifying #14 it's using timestamps. I should point out that this isn't my code, it came from Jonathan Lange's template, which given I'm a complete newb, I've found incredibly helpful. Even though a lot of it is a bit beyond me ;)
@filterfish Thanks!
So this is the "trick" is to create this type: type LogM msg m = LoggingT (WithSeverity msg) (LoggingT (WithTimestamp (WithSeverity msg)) m)
Looks a bit weird, nested LoggingT types ๐ I wonder if there is a more simple solution for my case.
Ah, it's this again. There's some previous discussion here: #6 (comment). Does that help clear things up? I wonder if/where this should be added to the documentation.
Yeah, it's the same issue/discussion. It helps but it's still difficult to understand because the types are so "abstract" ๐
About the documentation. I think only examples help with clarification.