Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging in scala

Tags:

logging

scala

In Java, the standard idiom for logging is to create a static variable for a logger object and use that in the various methods.

In Scala, it looks like the idiom is to create a Logging trait with a logger member and mixin the trait in concrete classes. This means that each time an object is created it calls the logging framework to get a logger and also the object is bigger due to the additional reference.

Is there an alternative that allows the ease of use of "with Logging" while still using a per-class logger instance?

EDIT: My question is not about how one can write a logging framework in Scala, but rather how to use an existing one (log4j) without incurring an overhead of performance (getting a reference for each instance) or code complexity. Also, yes, I want to use log4j, simply because I'll use 3rd party libraries written in Java that are likely to use log4j.

like image 454
IttayD Avatar asked Jan 07 '10 06:01

IttayD


People also ask

What is logging in Scala?

Scala-logging is a library that wraps the Simple Logging Facade for Java (SLF4J) in a Scala-friendly library. SLF4J supports many different logging frameworks, including both log4j, which we've covered in an earlier post, and logback a similar framework.

Does Scala use Log4j?

Log4j Scala API is a Scala logging facade based on Log4j 2. Support for Scala versions 2.10, 2.11, and 2.12 is provided, and experimental support for pre-release versions of 2.13 is also provided. Log4j Scala API uses Log4j 2.


1 Answers

I'd just stick to the "with Logging" approach. Clean design wins every time - if you get the boilerplate out the way then chances are that you can find far more useful gains achievable in other areas.

Keep in mind that the logging framework will cache loggers, so you still have one per class, even if every instance of that class happens to hold a (inexpensive) reference.

Without proof that logger references are harming your heap, this smells a lot like premature optimization... Just relax and don't worry about it, unless a profiler tells you otherwise.

On an unrelated note, you might also want to look into using slf4j and logback instead of log4j. slf4j has a cleaner design that fits better with idiomatic scala.

like image 113
Kevin Wright Avatar answered Oct 04 '22 04:10

Kevin Wright