4

logging is cluttering my "beautiful" clean simple short code. instead of a simple

def mymethod = dosomething // scala

my code is also having also all these nasty logging statements which and immediately i have to add new lines and curly braces to mymethod. now i don't like AOP it would just make clear code --> unclear. anyway to get over this? I have when simple code turns to less simple, but i also need logging. help.

how to make this code clear simple short and also with logging?

Jas
  • 13,577
  • 23
  • 88
  • 142

2 Answers2

7

A typical approach in functional programming would be to add a higher-order logging combinator.

log :: IO a -> String -> IO a 
log f s = do
    v <- f
    print ("My message: " ++ s)
    return v

Such a wrapper augments evaluation of a function with a log message. The general pattern is \x y -> .. something with y .. return x

Don Stewart
  • 136,266
  • 35
  • 360
  • 464
5

Kestrel combinator is commonly used for this:

def mymethod = dosomething.tap(x => log.info(s"I've done ... and got $x"))

If you need to do this for any method, there is scala virtualized, which allows to overload some of the language concepts, including method calls, I guess. Plus, somebody might propose to look at the macro, but I'm not competent enough to make this assumptions.

Community
  • 1
  • 1
om-nom-nom
  • 61,565
  • 12
  • 180
  • 225