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

Hierarchical Logging Algebras #34

Merged
merged 1 commit into from
May 30, 2018
Merged
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
@@ -1,6 +1,6 @@
package io.chrisdavenport.log4cats

trait MDCLogger[F[_]]{
trait MDCLogger[F[_]] extends Logger[F] {
def trace(ctx: (String, String)*)(msg: => String): F[Unit]
def debug(ctx: (String, String)*)(msg: => String): F[Unit]
def info(ctx: (String, String)*)(msg: => String): F[Unit]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.chrisdavenport.log4cats

trait LogLevelAware[F[_]]{
trait SelfAwareLogger[F[_]] extends Logger[F] {
def isTraceEnabled: F[Boolean]
def isDebugEnabled: F[Boolean]
def isInfoEnabled: F[Boolean]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.chrisdavenport.log4cats

trait SelfAwareMDCLogger[F[_]] extends SelfAwareLogger[F] with MDCLogger[F]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Log4sLogger {
def createByName[F[_]: Sync](name: String) = fromLog4s[F](org.log4s.getLogger(name))
def createByClass[F[_]: Sync](clazz: Class[_]) = fromLog4s[F](org.log4s.getLogger(clazz))

def fromLog4s[F[_]: Sync](logger: Base): Logger[F] with LogLevelAware[F] = new Logger[F] with LogLevelAware[F] {
def fromLog4s[F[_]: Sync](logger: Base): SelfAwareLogger[F] = new SelfAwareLogger[F] {

override def isTraceEnabled: F[Boolean] =
Sync[F].delay(logger.isTraceEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,40 @@
package io.chrisdavenport.log4cats.slf4j

import cats.effect.Sync
import io.chrisdavenport.log4cats.{Logger, LogLevelAware, MDCLogger}
import io.chrisdavenport.log4cats.SelfAwareMDCLogger
import io.chrisdavenport.log4cats.slf4j.internal._
import org.slf4j.{Logger => JLogger}

import language.experimental.macros

object Slf4jLogger {

def create[F[_]: Sync]: F[Logger[F] with LogLevelAware[F] with MDCLogger[F]] =
def create[F[_]: Sync]: F[SelfAwareMDCLogger[F]] =
Sync[F].delay(unsafeCreate)

def fromName[F[_]: Sync](name: String): F[Logger[F] with LogLevelAware[F] with MDCLogger[F]] =
def fromName[F[_]: Sync](name: String): F[SelfAwareMDCLogger[F]] =
Sync[F].delay(unsafeFromName(name))

def fromClass[F[_]: Sync](clazz: Class[_]): F[Logger[F] with LogLevelAware[F] with MDCLogger[F]] =
def fromClass[F[_]: Sync](clazz: Class[_]): F[SelfAwareMDCLogger[F]] =
Sync[F].delay(unsafeFromClass(clazz))

def fromSlf4j[F[_]: Sync](logger: JLogger): F[Logger[F] with LogLevelAware[F] with MDCLogger[F]] =
def fromSlf4j[F[_]: Sync](logger: JLogger): F[SelfAwareMDCLogger[F]] =
Sync[F].delay(unsafeFromSlf4j(logger))

def unsafeCreate[F[_]: Sync]: Logger[F] with LogLevelAware[F] with MDCLogger[F] =
def unsafeCreate[F[_]: Sync]: SelfAwareMDCLogger[F] =
macro LoggerMacros.getLoggerImpl[F[_]]

def unsafeFromName[F[_]: Sync](name: String):Logger[F] with LogLevelAware[F] with MDCLogger[F] =
def unsafeFromName[F[_]: Sync](name: String): SelfAwareMDCLogger[F] =
fromSlf4jLogger(new Slf4jLoggerInternal[F](org.slf4j.LoggerFactory.getLogger(name)))

def unsafeFromClass[F[_]: Sync](clazz: Class[_]): Logger[F] with LogLevelAware[F] with MDCLogger[F] =
def unsafeFromClass[F[_]: Sync](clazz: Class[_]): SelfAwareMDCLogger[F] =
fromSlf4jLogger(new Slf4jLoggerInternal[F](org.slf4j.LoggerFactory.getLogger(clazz)))

def unsafeFromSlf4j[F[_]: Sync](logger: JLogger): Logger[F] with LogLevelAware[F] with MDCLogger[F] =
def unsafeFromSlf4j[F[_]: Sync](logger: JLogger): SelfAwareMDCLogger[F] =
fromSlf4jLogger(new Slf4jLoggerInternal[F](logger))

private def fromSlf4jLogger[F[_]: Sync](s: Slf4jLoggerInternal[F]): Logger[F] with LogLevelAware[F] with MDCLogger[F] =
new Logger[F] with LogLevelAware[F] with MDCLogger[F] {
private def fromSlf4jLogger[F[_]: Sync](s: Slf4jLoggerInternal[F]): SelfAwareMDCLogger[F] =
new SelfAwareMDCLogger[F] {
@inline override def isTraceEnabled: F[Boolean] = s.isTraceEnabled
@inline override def isDebugEnabled: F[Boolean] = s.isDebugEnabled
@inline override def isInfoEnabled: F[Boolean] = s.isInfoEnabled
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.chrisdavenport.log4cats.testing

import io.chrisdavenport.log4cats.{Logger, LogLevelAware}
import io.chrisdavenport.log4cats.{SelfAwareLogger}
import cats.effect.Sync
import cats.implicits._
import java.util.concurrent.atomic.AtomicReference
import scala.annotation.tailrec

trait TestingLogger[F[_]] extends Logger[F] with LogLevelAware[F]{
trait TestingLogger[F[_]] extends SelfAwareLogger[F] {
import TestingLogger.LogMessage
def logged: F[Vector[LogMessage]]
}
Expand Down