Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.typesafe.scalalogging.slf4j

import org.slf4j.{ Logger => Underlying, MarkerFactory }
import org.slf4j.{ Logger => Underlying, ILoggerFactory, LoggerFactory, MarkerFactory }
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification

Expand Down Expand Up @@ -216,4 +216,23 @@ object Slf4jLoggerSpec extends Specification with Mockito {
there was one(underlying).trace(Marker, Message, Throwable)
}
}

"Creating logger" should {
"provide correct logger name" in {
val ilfSpy = spy(LoggerFactory.getILoggerFactory)
new TestSuperclass(ilfSpy)
there was one(ilfSpy).getLogger("com.typesafe.scalalogging.slf4j.TestSuperclass")

new TestSubclass(ilfSpy)
there was one(ilfSpy).getLogger("com.typesafe.scalalogging.slf4j.TestSubclass")
}
}
}

class TestSuperclass(iLoggerFactory: ILoggerFactory) {
Logger(iLoggerFactory)
}

class TestSubclass(iLoggerFactory: ILoggerFactory) extends TestSuperclass(iLoggerFactory) {
Logger(iLoggerFactory)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
package com.typesafe.scalalogging.slf4j

import language.experimental.macros
import org.slf4j.{ Logger => Underlying, Marker }
import org.slf4j.{ Logger => Underlying, LoggerFactory => UnderlyingFactory, ILoggerFactory => IUnderlyingFactory, Marker }

object Logger {
/**
* Create a [[com.typesafe.scalalogging.slf4j.Logger]] and the underlying `org.slf4j.Logger`.
* Autodetect underlying logger name from class in which it was called
*/
def apply(factory: IUnderlyingFactory = UnderlyingFactory.getILoggerFactory): Logger = macro LoggerMacros.createLogger

/**
* Create a [[com.typesafe.scalalogging.slf4j.Logger]] wrapping the given underlying `org.slf4j.Logger`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@

package com.typesafe.scalalogging.slf4j

import org.slf4j.Marker
import org.slf4j.{ ILoggerFactory => IUnderlyingFactory, Marker }
import scala.reflect.macros.Context

private object LoggerMacros {

type LoggerContext = Context { type PrefixType = Logger }

def createLogger(c: LoggerContext)(factory: c.Expr[IUnderlyingFactory]): c.Expr[Logger] = {
import c.universe._
val enclClass = c.enclosingClass.symbol.asClass
val enclClassLiteral = c.Expr(Literal(Constant(enclClass.fullName)))
c.universe.reify(Logger(factory.splice.getLogger(enclClassLiteral.splice)))
}

// Error

def errorMessage(c: LoggerContext)(message: c.Expr[String]) =
Expand Down