Scala's Try
is very useful.
I'd like to use that pattern, but log all exceptions.
How can I do this?
The exception log is a rolling collection of 4 files; when a file is full, data is added to the next file. When the last file has been filled, data starts to be added to the first file, overwriting the previous data there. This cycle of writing continues, ensuring that the newest data is retained.
If you want to log a formatted exception at a log level other than error you must use exc_info=True. @ChrisJohnson - you could simply use logging. exception() in your example...
Don't Log and Throw No matter which approach you take when handling exceptions, log the exception at the same time you handle the exception. If you log the exception and then throw it, there's a chance that the exception will be logged again further up the call stack, resulting in two log events for the same error.
Define the following helper:
import scala.util.{Try, Failure} def LogTry[A](computation: => A): Try[A] = { Try(computation) recoverWith { case e: Throwable => log(e) Failure(e) } }
Then you can use it as you would use Try
, but any exception will be logged through log(e)
.
Starting Scala 2.13
, the chaining operation tap
can be used to apply a side effect (in this case some logging) on any value while returning the original value:
import util.chaining._ val x = Try("aa".toInt).tap(_.failed.foreach(println)) // java.lang.NumberFormatException: For input string: "aa" // x: Try[Int] = Failure(java.lang.NumberFormatException: For input string: "aa")
Or an equivalent pattern matching version:
val x = Try("aa".toInt).tap { case Failure(e) => println(e) case _ => } // java.lang.NumberFormatException: For input string: "aa" // x: Try[Int] = Failure(java.lang.NumberFormatException: For input string: "aa")
The tap
chaining operation applies a side effect (in this case println
or some logging) on a value (in this case a Try
) while returning the original unmodified value on which tap
is applied (the Try
):
def tap[U](f: (A) => U): A
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With