The Writer[L, ?]
effect lets the computation emit additional values of type L
as a side-effect of computation. It is commonly
used to functional logging, but you could also view the log value as an appendix or supplimentary information about the
computation.
This exercise is a use-case of functional logging and how "logs-as-values" lets us easily write unit tests around the log output.
-
The effect stack
R
includesWriter[Log]
, whereLog
is a sealed hierarchy of Log events at different levels. -
In
PathScan.scan
, thetell
operator is used to emitLog
values. Becausetell
just emits a log, it has typeEff[R, Unit]
. So an underscore is used on the lefthand-side of the for (eg_ <- tell(x)
). -
In
main
, the interpretation of the Eff program now includes arunWriterUnsafe
step. This is one of several approaches to logging offerred by Eff, where we send a side-effecting handler function (egprintln
) into the interpreter, that logs each event as it is emitted, rather than returning an accumulation. It's not pure, but it has the advantage of not accumulating data in memory during execution.
Run the tests. They now verify not just the program output but the logs. They should fail because they expect log output for each file visited by the scan.
Make the test pass by adding the appropriate tell
statement to the File
case in pathScan
.
run
the scanner on a real directory tree and check the logging works as expected.
-
Measure the time the overall scan took. Take start and end times in
scanReport
, and use atell
to log the elapsed millis. Ensure your calls to the clock are wrapped intaskDelay
effects to ensure the clock timings are taken when the program runs, and not when it is created. -
Try changing the interpretation in main to use the plain
runWriter
. What does the program return now? How can you print the logs?