Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:53:56+00:00 2026-06-09T15:53:56+00:00

How do you use scalaz.WriterT for logging?

  • 0

How do you use scalaz.WriterT for logging?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:53:58+00:00Added an answer on June 9, 2026 at 3:53 pm

    About monad transformers

    This is a very short introduction. You may find more information on haskellwiki or this great slide by @jrwest.

    Monads don’t compose, meaning that if you have a monad A[_] and a monad B[_], then A[B[_]] can not be derived automatically. However in most cases this can be achieved by having a so-called monad transformer for a given monad.

    If we have monad transformer BT for monad B, then we can compose a new monad A[B[_]] for any monad A. That’s right, by using BT, we can put the B inside A.

    Monad transformer usage in scalaz

    The following assumes scalaz 7, since frankly I didn’t use monad transformers with scalaz 6.

    A monad transformer MT takes two type parameters, the first is the wrapper (outside) monad, the second is the actual data type at the bottom of the monad stack. Note: It may take more type parameters, but those are not related to the transformer-ness, but rather specific for that given monad (like the logged type of a Writer, or the error type of a Validation).

    So if we have a List[Option[A]] which we would like to treat as a single composed monad, then we need OptionT[List, A]. If we have Option[List[A]], we need ListT[Option, A].

    How to get there? If we have the non-transformer value, we can usually just wrap it with MT.apply to get the value inside the transformer. To get back from the transformed form to normal, we usually call .run on the transformed value.

    So val a: OptionT[List, Int] = OptionT[List, Int](List(some(1)) and val b: List[Option[Int]] = a.run are the same data, just the representation is different.

    It was suggested by Tony Morris that is best to go into the transformed version as early as possible and use that as long as possible.

    Note: Composing multiple monads using transformers yields a transformer stack with types just the opposite order as the normal data type. So a normal List[Option[Validation[E, A]]] would look something like type ListOptionValidation[+E, +A] = ValidationT[({type l[+a] = OptionT[List, a]})#l, E, A]

    Update: As of scalaz 7.0.0-M2, Validation is (correctly) not a Monad and so ValidationT doesn’t exist. Use EitherT instead.

    Using WriterT for logging

    Based on your need, you can use the WriterT without any particular outer monad (in this case in the background it will use the Id monad which doesn’t do anything), or can put the logging inside a monad, or put a monad inside the logging.

    First case, simple logging

    import scalaz.{Writer}
    import scalaz.std.list.listMonoid
    import scalaz._
    
    def calc1 = Writer(List("doing calc"), 11)
    def calc2 = Writer(List("doing other"), 22)
    
    val r = for {
      a <- calc1
      b <- calc2
    } yield {
      a + b
    }
    
    r.run should be_== (List("doing calc", "doing other"), 33)
    

    We import the listMonoid instance, since it also provides the Semigroup[List] instance. It is needed since WriterT needs the log type to be a semigroup in order to be able to combine the log values.

    Second case, logging inside a monad

    Here we chose the Option monad for simplicity.

    import scalaz.{Writer, WriterT}
    import scalaz.std.list.listMonoid
    import scalaz.std.option.optionInstance
    import scalaz.syntax.pointed._
    
    def calc1 = WriterT((List("doing calc") -> 11).point[Option])
    def calc2 = WriterT((List("doing other") -> 22).point[Option])
    
    val r = for {
      a <- calc1
      b <- calc2
    } yield {
      a + b
    }
    
    r.run should be_== (Some(List("doing calc", "doing other"), 33))
    

    With this approach, since the logging is inside the Option monad, if any of the bound options is None, we would just get a None result without any logs.

    Note: x.point[Option] is the same in effect as Some(x), but may help to generalize the code better. Not lethal just did it that way for now.

    Third option, logging outside of a monad

    import scalaz.{Writer, OptionT}
    import scalaz.std.list.listMonoid
    import scalaz.std.option.optionInstance
    import scalaz.syntax.pointed._
    
    type Logger[+A] = WriterT[scalaz.Id.Id, List[String], A]
    
    def calc1 = OptionT[Logger, Int](Writer(List("doing calc"), Some(11): Option[Int]))
    def calc2 = OptionT[Logger, Int](Writer(List("doing other"), None: Option[Int]))
    
    val r = for {
      a <- calc1
      b <- calc2
    } yield {
      a + b
    }
    
    r.run.run should be_== (List("doing calc", "doing other") -> None)
    

    Here we use OptionT to put the Option monad inside the Writer. One of the calculations is Noneto show that even in this case logs are preserved.

    Final remarks

    In these examples List[String] was used as the log type. However using String is hardly ever the best way, just some convention forced on us by logging frameworks. It would be better to define a custom log ADT for example, and if needed to output, convert it to string as late as possible. This way you could serialize the log’s ADT and easily analyse it later programmatically (instead of parsing strings).

    WriterT has a host of useful methods to work with to ease logging, check out the source. For example given a w: WriterT[...], you may add a new log entry using w :++> List("other event"), or even log using the currently held value using w :++>> ((v) => List("the result is " + v)), etc.

    There are many explicit and longish code (types, calls) in the examples. As always, these are for clarity, refactor them in your code by extracting common types and ops.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to use scalaz library in eclipse ide. I download scalaz-full_2.9.1-6.0.4.jar and go
I'm trying to use scalaz validation in our project and have ran into a
Can anybody point me to any open source project that make use of scalaz
Do any of you know why examples from Scalaz always use this import technique:
Is anyone able to use Scala IDE in Eclipse with syntax highlighting showing different
I'm writing a BloomFilter and wanted to use Scala's default MurmurHash3 implementation: scala.util.MurmurHash3. My
I'd like to use pure scala code (without side effects) as a immutable message
In the QUERY select clause it states that you can use a scalar function
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
In Scala it's possible to use the annotation @BeanProperty to automatically generate getters and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.