Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LoggerFactory typeclass #629

Merged
merged 15 commits into from
Apr 21, 2022
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.typesafe.tools.mima.core._

val Scala213 = "2.13.8"
val Scala212 = "2.12.15"
val Scala3 = "3.0.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2018 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.log4cats.slf4j

import org.typelevel.log4cats.slf4j.internal.GetLoggerMacros

trait LoggerNamePlatform {
hamnis marked this conversation as resolved.
Show resolved Hide resolved
implicit def name: LoggerName = macro GetLoggerMacros.getLoggerName
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2018 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.log4cats.slf4j

import cats.effect.Sync
import org.typelevel.log4cats.SelfAwareStructuredLogger
import org.typelevel.log4cats.slf4j.internal._

trait Slf4jLoggerPlatform {

private[slf4j] def getLogger[F[_]](implicit f: Sync[F]): SelfAwareStructuredLogger[F] =
macro GetLoggerMacros.unsafeCreateImpl[F[_]]

private[slf4j] def create[F[_]](implicit f: Sync[F]): F[SelfAwareStructuredLogger[F]] =
macro GetLoggerMacros.safeCreateImpl[F[_]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,26 @@ private[slf4j] class GetLoggerMacros(val c: blackbox.Context) {

final def unsafeCreateImpl[F](f: c.Expr[F]) = getLoggerImpl[F](f, false)

/** Get a logger by reflecting the enclosing class name. */
private def getLoggerImpl[F](f: c.Expr[F], delayed: Boolean) = {
import c.universe._
val loggerName = getLoggerNameImpl

def loggerByParam(param: c.Tree) = {
val unsafeCreate =
q"_root_.org.typelevel.log4cats.slf4j.Slf4jLogger.getLoggerFromSlf4j(_root_.org.slf4j.LoggerFactory.getLogger(...${List(param)}))($f)"
if (delayed)
q"_root_.cats.effect.Sync.apply($f).delay(...$unsafeCreate)"
else
unsafeCreate
}
loggerByParam(q"$loggerName.value")
}

final def getLoggerName = getLoggerNameImpl

/** Get a logger by reflecting the enclosing class name. */
private def getLoggerNameImpl = {
import c.universe._

@tailrec def findEnclosingClass(sym: c.universe.Symbol): c.universe.Symbol = {
sym match {
Expand All @@ -53,16 +70,7 @@ private[slf4j] class GetLoggerMacros(val c: blackbox.Context) {

assert(cls.isModule || cls.isClass, "Enclosing class is always either a module or a class")

def loggerByParam(param: c.Tree) = {
val unsafeCreate =
q"_root_.org.typelevel.log4cats.slf4j.Slf4jLogger.getLoggerFromSlf4j(_root_.org.slf4j.LoggerFactory.getLogger(...${List(param)}))($f)"
if (delayed)
q"_root_.cats.effect.Sync.apply($f).delay(...$unsafeCreate)"
else
unsafeCreate
}

def loggerBySymbolName(s: Symbol) = {
def nameBySymbol(s: Symbol) = {
def fullName(s: Symbol): String = {
@inline def isPackageObject = (
(s.isModule || s.isModuleClass)
Expand All @@ -81,24 +89,24 @@ private[slf4j] class GetLoggerMacros(val c: blackbox.Context) {
fullName(s.owner)
}
}
loggerByParam(q"${fullName(s)}")
q"new _root_.org.typelevel.log4cats.slf4j.LoggerName(${fullName(s)})"
}

def loggerByType(s: Symbol) = {
def nameByType(s: Symbol) = {
val typeSymbol: ClassSymbol = (if (s.isModule) s.asModule.moduleClass else s).asClass
val typeParams = typeSymbol.typeParams

if (typeParams.isEmpty) {
loggerByParam(q"_root_.scala.Predef.classOf[$typeSymbol]")
q"new _root_.org.typelevel.log4cats.slf4j.LoggerName(_root_.scala.Predef.classOf[$typeSymbol].getName)"
} else {
if (typeParams.exists(_.asType.typeParams.nonEmpty)) {
/* We have at least one higher-kinded type: fall back to by-name logger construction, as
* there's no simple way to declare a higher-kinded type with an "any" parameter. */
loggerBySymbolName(s)
nameBySymbol(s)
} else {
val typeArgs = List.fill(typeParams.length)(WildcardType)
val typeConstructor = tq"$typeSymbol[..${typeArgs}]"
loggerByParam(q"_root_.scala.Predef.classOf[$typeConstructor]")
q"new _root_.org.typelevel.log4cats.slf4j.LoggerName(_root_.scala.Predef.classOf[$typeConstructor].getName)"
}
}
}
Expand All @@ -107,14 +115,14 @@ private[slf4j] class GetLoggerMacros(val c: blackbox.Context) {
s.isClass && !(s.owner.isPackage)

val instanceByName =
Slf4jLoggerInternal.singletonsByName && (cls.isModule || cls.isModuleClass) || cls.isClass && isInnerClass(
(Slf4jLoggerInternal.singletonsByName && cls.isModule || cls.isModuleClass) || (cls.isClass && isInnerClass(
cls
)
))

if (instanceByName) {
loggerBySymbolName(cls)
nameBySymbol(cls)
} else {
loggerByType(cls)
nameByType(cls)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2018 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.log4cats.slf4j

import org.typelevel.log4cats.slf4j.internal.GetLoggerMacros

import scala.quoted.*

trait LoggerNamePlatform {
implicit inline def name: LoggerName =
${ GetLoggerMacros.getLoggerName }
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2018 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.log4cats.slf4j

import cats.effect.Sync
import org.typelevel.log4cats.SelfAwareStructuredLogger
import org.typelevel.log4cats.slf4j.internal._
import org.slf4j.Logger as JLogger
import scala.annotation.nowarn

trait Slf4jLoggerPlatform {

// for binary compability
private[slf4j] inline def create[F[_]](F: Sync[F]): F[SelfAwareStructuredLogger[F]] =
${ GetLoggerMacros.createImpl('F) }
private[slf4j] inline def getLogger[F[_]](using F: Sync[F]): SelfAwareStructuredLogger[F] =
${ GetLoggerMacros.getLoggerImpl('F) }

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,34 @@ package org.typelevel.log4cats.slf4j.internal

import cats.effect.Sync
import org.slf4j.LoggerFactory
import org.typelevel.log4cats.slf4j.Slf4jLogger
import org.typelevel.log4cats.slf4j.{LoggerName, Slf4jLogger}
import org.typelevel.log4cats.SelfAwareStructuredLogger

import scala.annotation.tailrec
import scala.quoted._
import scala.quoted.*

private[slf4j] object GetLoggerMacros {

def getLoggerImpl[F[_]: Type](
F: Expr[Sync[F]]
)(using qctx: Quotes): Expr[SelfAwareStructuredLogger[F]] = {
val name = getLoggerNameImpl
'{ Slf4jLogger.getLoggerFromSlf4j(LoggerFactory.getLogger($name))($F) }
}

def createImpl[F[_]: Type](
F: Expr[Sync[F]]
)(using qctx: Quotes): Expr[F[SelfAwareStructuredLogger[F]]] = {
val logger = getLoggerImpl(F)
'{ $F.delay($logger) }
}

def getLoggerName(using qctx: Quotes): Expr[LoggerName] = {
val name = getLoggerNameImpl
'{ new LoggerName($name) }
}

def getLoggerNameImpl(using qctx: Quotes): Expr[String] = {
import qctx.reflect._

@tailrec def findEnclosingClass(sym: Symbol): Symbol = {
Expand All @@ -42,7 +60,7 @@ private[slf4j] object GetLoggerMacros {
}
}

def logger(s: Symbol): Expr[SelfAwareStructuredLogger[F]] = {
def symbolName(s: Symbol): Expr[String] = {
def fullName(s: Symbol): String = {
val flags = s.flags
if (flags.is(Flags.Package)) {
Expand All @@ -63,18 +81,10 @@ private[slf4j] object GetLoggerMacros {
}
}

val name = Expr(fullName(s))
'{ Slf4jLogger.getLoggerFromSlf4j(LoggerFactory.getLogger($name))($F) }
Expr(fullName(s).stripSuffix("$"))
}

val cls = findEnclosingClass(Symbol.spliceOwner)
logger(cls)
}

def createImpl[F[_]: Type](
F: Expr[Sync[F]]
)(using qctx: Quotes): Expr[F[SelfAwareStructuredLogger[F]]] = {
val logger = getLoggerImpl(F)
'{ $F.delay($logger) }
symbolName(cls)
}
}
Loading