Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to log in Scala *without* a reference to the logger in *every instance*?
    primarykey
    data
    text
    <p>I've looked at example of logging in Scala, and it usually looks like this:</p> <pre><code>import org.slf4j.LoggerFactory trait Loggable { private lazy val logger = LoggerFactory.getLogger(getClass) protected def debug(msg: =&gt; AnyRef, t: =&gt; Throwable = null): Unit = {...} } </code></pre> <p>This seems independent of the concrete logging framework. While this does the job, it also introduces an extraneous lazy val <em>in every instance</em> that wants to do logging, which might well be every instance of the whole application. This seems much too heavy to me, in particular if you have many "small instances" of some specific type.</p> <p>Is there a way of putting the logger in the object of the concrete class instead, just by using inheritance? If I have to explicitly declare the logger in the object of the class, and explicitly refer to it from the class/trait, then I have written almost as much code as if I had done no reuse at all.</p> <p>Expressed in a non-logging specific context, the problem would be:</p> <p>How do I declare in a trait that the implementing class must have a singleton object of type X, and that this singleton object must be accessible through method def x: X ?</p> <p>I can't simply define an abstract method, because there could only be a single implementation in the class. I want that logging in a super-class gets me the super-class singleton, and logging in the sub-class gets me the sub-class singleton. Or put more simply, I want logging in Scala to work like traditional logging in Java, using static loggers specific to the class doing the logging. My current knowledge of Scala tells me that this is simply not possible without doing it exactly the same way you do in Java, without much if any benefits from using the "better" Scala.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload