Spy

com.phasmidsoftware.lascala.fp.Spy
See theSpy companion object
trait Spy

This trait and its related object provide a functional-programming style (if somewhat quick-and-dirty) spy feature for logging (or otherwise peeking at) the values of Scala expressions.

While this mechanism (by default) logs values using SLF4J, it does so in a functional style. You do not have to break up your functional expressions to write logging statements (unless they are the recursive calls of a tail-recursive method).

If you just want to slip in a quick invocation of Spy.spy, you will need to provide an implicit spyFunc. The proper simple way to do this is to declare a logger as follows (you can use any name you like since it's an implicit):

implicit val spyLogger: org.slf4j.Logger = Spy.getLogger(getClass)

There's even an (improper) default logger based on the Spy class itself which you can use simply by adding this: import Spy._

In order to use it with an explicitly defined spyFunc, please see the way it's done in SpySpec.

By default, spying is turned on so, if you want to turn it off for a portion of a run, you must set spying to false, then reset it as it was when you return. This is useful for benchmarking or any time you know that there will be just too much information (TMI).

However, there is another mechanism for turning spying off. For example, you have some code which you are continually working on. You don't like to release it with the calls to Spy intact so you remove them. Then you have to go to a significant amount of trouble to put them back. An alternative is to declare the spyLogger to be null (but then you really must include the type annotation as shown above).

Created by scalaprof on 8/17/16.

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any
In this article