disabling logging - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Pavan Rikhi

Is it possible to switch between runNoLoggingT & runStderrLoggingT based on some variable?

I have somthing like this:

runStderrLoggingT $ do
    pool <- createPostgresqlPool "dbname=sese-website" $ poolSize env
    runSqlPool (runMigration migrateAll) pool
    return pool

And wanted to switch that runStderrLoggingT call to runNoLoggingT when env == Production. But the fact that runStderrLoggingT :: LoggingT m a -> m a & runNoLoggingT :: NoLoggingT m a -> m a is throwing me off :confused:

Jack Henahan

Though just an if would suffice if you've only got two envs

Ben Kolera

It sounds like you may need to write your application code against a MonadLogger constraint rather than a concrete LoggingT type?

Ben Kolera

(wild arsed guess. Probably wrong. :slight_smile:)

Pavan Rikhi

@Jack Henahan @Ben Kolera Yerp, that's what I pretty much ended up doing:
https://github.com/Southern-Exposure-Seed-Exchange/southernexposure.com/blob/8cd14fa89905c7b6d5fc8e266c89aed38277791f/server/app/Main.hs#L77-L88

Southern Exposure's E-Commerce Website, Built with Haskell & Elm. - Southern-Exposure-Seed-Exchange/southernexposure.com
Pavan Rikhi

Was hoping I could just select between runStderrLoggingT or runNoLoggingT in the case env of ... but that seems OK enough...

Jack Henahan
makePool env = runLogT $ makeSqlPool env
  where runLogT = case env of
    Production -> runNoLoggingT
    Development -> runStderrLoggingT

or something like that oughta work

Pavan Rikhi

Nope :frown: That was my first try but there's no MonadLogger constraint in either of those

Jack Henahan

A richer Environment type might also be nicer to manage. When I see lots of case statements on the same thing, it makes me want to lift those into the type itself

Jack Henahan
data EnvType = Production | Development
data Environment = Env { envType :: EnvType, poolSize :: Int, etc. }
Jack Henahan

And then you can start carrying that around in a Reader or something and not have to refer to it explicitly

Pavan Rikhi

Yeah I was thinking of at least having the library define functions like getHttpLogger : Environment -> ..., getSqlLogger :: Environment -> ..., etc instead of having all those case statements in the exe's Main.

Pavan Rikhi

But most of that is only used in main so it hasn't been worth refactoring considering the time crunch I've been in ;)

Pavan Rikhi

There's only 2 case env of s in the actual app

Jack Henahan

Fair. :slight_smile: I'm just trying to dump thoughts out so I can get to sleep, so I'm probably babbling

Pavan Rikhi

Today was prod launch day so maybe I'll actually get to do some code-quality refactoring instead of "the requirements have changed!"-refactoring soon :P

Pavan Rikhi

All good! Don't let me keep you up :sleeping: