1

There are lot of log statements in some important method, they are used to log the progress and some important information.

val result = user.login()
if(result) {
  logger.info("User has logged in successful: " + user.id)
  val questions = user.queryQuestions()
  logger.info("User has " + questions.size + " questions")
  val latestQuestion = questions.headOption
  logger.info("The latest question is: " + latestQuestion)
  ...
} else {
  logger.info("User has logged failed: " + user.id)
}

Suppose all the log information are very important, they need to be logged and audit. But the code looks like Java rather than Scala.

How can we improve it so it looks more functional and less side-effect?

Freewind
  • 193,756
  • 157
  • 432
  • 708
  • 2
    not really sure what's the problem, do you want to get rid of the *if* or do something with the *logger*? Logging by definition produces side-effects. – Mateusz Dymczyk Jul 22 '15 at 01:51
  • If you're looking to log every statement, you may want to take a look at [PR #24 for scala-logging](https://github.com/typesafehub/scala-logging/pull/24) - it contains a logging macro that displays each line. When combined with something like [Scalactic's `snap` method](http://www.scalactic.org/user_guide/Snapshots) you should get exactly what you are looking for. – Sean Vieira Jul 22 '15 at 02:02
  • 2
    The answers lie in scalaz -- [`Writer`](http://eed3si9n.com/learning-scalaz/Writer.html) or [`IO`](http://eed3si9n.com/learning-scalaz/IO+Monad.html) -- if you dare seek them. – Chris Martin Jul 22 '15 at 02:15
  • You might be looking for the kestrel combinator to help a bit? See http://stackoverflow.com/questions/9671620/how-to-keep-return-value-when-logging-in-scala – The Archetypal Paul Jul 22 '15 at 10:07
  • @ChrisMartin, I may have the wrong end of the stick, but is there a problem with Writer/IO when you get an exception between log entries?What does one have to do to ensure all the log entries up to that point get persisted and not lost? – The Archetypal Paul Jul 22 '15 at 10:52
  • @MateuszDymczyk I mean the log part, not the `if`. Logging is by definition produces side-effects, but I'm wondering if there is any way to make it less side-effects. I really don't like the Java-style code, and the log statements are the main reason. – Freewind Jul 22 '15 at 16:00
  • I posted a similar question but have to delete for some security reason. And there is a comment from there, thanks: "I know about bartoszmilewski.com/2014/12/23/kleisli-categories but I am curious to making it practical also. – Recognize Evil as Waste" – Freewind Jul 22 '15 at 16:02
  • @Freewind well the blog post you've linked is treating about something a bit different, there at the beginning you have mutable state (logger), which is the problem. In your example above you only print to some kind of output (that being your side effect) so it's not really the same thing I think. Sure you can try to do something with it as Chris Martin mentioned but overcomplicating such a trivial thing as logging just to be more "pure" is a bit of an overkill in my opinion and just obfuscates the code. But that's just preference I guess. – Mateusz Dymczyk Jul 23 '15 at 01:24

0 Answers0