Skip to content

Commit

Permalink
Merge pull request #33 from ChristopherDavenport/alternativeIOLog4sPr…
Browse files Browse the repository at this point in the history
…oposal

Alternative : Alternative io log4s proposal
  • Loading branch information
ChristopherDavenport authored May 30, 2018
2 parents 6628788 + 586c37b commit 642c3cd
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 109 deletions.
14 changes: 12 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lazy val log4cats = project.in(file("."))
scribeJVM,
scribeJS,
slf4j,
`slf4j-internal`,
log4sJVM,
log4sJS,
docs
Expand Down Expand Up @@ -58,13 +59,21 @@ lazy val log4s = crossProject(JSPlatform, JVMPlatform).in(file("log4s"))

lazy val slf4j = project.in(file("slf4j"))
.settings(commonSettings, releaseSettings)
.dependsOn(`slf4j-internal`)
.settings(
name := "log4cats-slf4j"
)

lazy val `slf4j-internal` = project.in(file("slf4j-internal"))
.settings(commonSettings, releaseSettings)
.dependsOn(core.jvm)
.settings(
name := "log4cats-slf4j",
name := "log4cats-slf4j-internal",
libraryDependencies ++= Seq(
"org.slf4j" % "slf4j-api" % "1.7.25"
)
)

lazy val log4sJVM = log4s.jvm
lazy val log4sJS = log4s.js

Expand All @@ -82,7 +91,8 @@ lazy val scribeJVM = scribe.jvm
lazy val scribeJS = scribe.js

lazy val contributors = Seq(
"ChristopherDavenport" -> "Christopher Davenport"
"ChristopherDavenport" -> "Christopher Davenport",
"lorandszakacs" -> "Loránd Szakács"
)

// check for library updates whenever the project is [re]load
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.chrisdavenport.log4cats

trait LogLevelAware[F[_]]{
def isTraceEnabled: F[Boolean]
def isDebugEnabled: F[Boolean]
def isInfoEnabled: F[Boolean]
def isWarnEnabled: F[Boolean]
def isErrorEnabled: F[Boolean]
}
11 changes: 0 additions & 11 deletions core/shared/src/main/scala/io/chrisdavenport/log4cats/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ package io.chrisdavenport.log4cats
trait Logger[F[_]]{
import Logger.{withModifiedString => wMS}

def isTraceEnabled: F[Boolean]
def isDebugEnabled: F[Boolean]
def isInfoEnabled: F[Boolean]
def isWarnEnabled: F[Boolean]
def isErrorEnabled: F[Boolean]

def error(message: => String): F[Unit]
def error(t: Throwable)(message: => String): F[Unit]
def warn(message: => String): F[Unit]
Expand All @@ -27,11 +21,6 @@ object Logger {
def apply[F[_]](implicit ev: Logger[F]) = ev

private def withModifiedString[F[_]](l: Logger[F], f: String => String): Logger[F] = new Logger[F]{
def isTraceEnabled: F[Boolean] = l.isTraceEnabled
def isDebugEnabled: F[Boolean] = l.isDebugEnabled
def isInfoEnabled: F[Boolean] = l.isInfoEnabled
def isWarnEnabled: F[Boolean] = l.isWarnEnabled
def isErrorEnabled: F[Boolean] = l.isErrorEnabled
def error(message: => String): F[Unit] = l.error(f(message))
def error(t: Throwable)(message: => String): F[Unit] = l.error(t)(f(message))
def warn(message: => String): F[Unit] = l.warn(f(message))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.chrisdavenport.log4cats

trait MDCLogger[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]
def warn(ctx: (String, String)*)(msg: => String): F[Unit]
def error(ctx: (String, String)*)(msg: => String): F[Unit]
}
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] = new Logger[F]{
def fromLog4s[F[_]: Sync](logger: Base): Logger[F] with LogLevelAware[F] = new Logger[F] with LogLevelAware[F] {

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

import io.chrisdavenport.log4cats.Logger
import _root_.scribe.{Logger => Base, Level}
import _root_.scribe.{Logger => Base}
import cats.effect.Sync

object ScribeLogger {
Expand All @@ -12,27 +12,6 @@ object ScribeLogger {

def fromLogger[F[_]: Sync](logger: Base): Logger[F] = new Logger[F]{

def isTraceEnabled: F[Boolean] =
Sync[F].delay(
checkLogLevelEnabled(logger, Level.Trace)
)
def isDebugEnabled: F[Boolean] =
Sync[F].delay(
checkLogLevelEnabled(logger, Level.Debug)
)
def isInfoEnabled: F[Boolean] =
Sync[F].delay(
checkLogLevelEnabled(logger, Level.Info)
)
def isWarnEnabled: F[Boolean] =
Sync[F].delay(
checkLogLevelEnabled(logger, Level.Warn)
)
def isErrorEnabled: F[Boolean] =
Sync[F].delay(
checkLogLevelEnabled(logger, Level.Error)
)

def error(message: => String): F[Unit] =
Sync[F].delay(logger.error(message))
def error(t: Throwable)(message: => String): F[Unit] =
Expand All @@ -55,13 +34,4 @@ object ScribeLogger {
Sync[F].delay(logger.trace(message, t))
}

/**
* Almost Certain this Behavior is incorrectly in place
* @param l Underlying Scribe Logger
* @param level Level to Check if it is enabled
*/
private[scribe] def checkLogLevelEnabled(l: Base, level: Level): Boolean = {
l.handlers.forall(_.modifiers.forall(_.priority.value <= level.value))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright 2013-2017 Sarah Gerweck
* see: https://github.com/Log4s/log4s
*
* Modifications copyright (C) 2018 Christopher Davenport
*
* 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 io.chrisdavenport.log4cats.slf4j.internal

/** A severity level that can be assigned to log statements. */
private[slf4j] sealed trait LogLevel {

/** The name of this log level. It is spelled with initial capitals */
def name: String = this.toString

/** The name of the SLF4J method that does logging at this level */
private[slf4j] def methodName = name.toLowerCase
}

private[slf4j] object LogLevel {

def forName(name: String): LogLevel = {
name.toLowerCase match {
case "trace" => Trace
case "debug" => Debug
case "info" => Info
case "warn" => Warn
case "error" => Error
case _ =>
throw new IllegalArgumentException(s"No log level named $name")
}
}
/** The highest logging severity. This generally indicates an
* application or system error that causes undesired outcomes.
* An error generally indicates a bug or an environment
* problem that warrants some kind of immediate intervention.
*/
case object Error extends LogLevel

/** Generally indicates something is not expected but the system is
* able to continue operating. This generally indicates a bug or
* environment problem that does not require urgent intervention.
*/
case object Warn extends LogLevel

/** Indicates normal high-level activity. Generally a single user– or
* system-initiated activity will trigger one or two info-level statements.
* (E.g., one when starting and one when finishing for complex requests.)
*/
case object Info extends LogLevel

/** Log statements that provide the ability to trace the progress and
* behavior involved in tracking a single activity. These are useful for
* debugging general issues, identifying how modules are interacting, etc.
*/
case object Debug extends LogLevel

/** Highly localized log statements useful for tracking the decisions made
* inside a single unit of code. These may occur at a very high frequency.
*/
case object Trace extends LogLevel
}


Loading

0 comments on commit 642c3cd

Please sign in to comment.