Skip to content

Commit

Permalink
Merge pull request #13 from davenverse/adjustDefaultLogLevel
Browse files Browse the repository at this point in the history
Adjust log level, convenience functions
  • Loading branch information
ChristopherDavenport committed Jul 19, 2023
2 parents 0e79341 + d8ed7ff commit 9af3250
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ object ClientMiddleware {
def removedContextKeys(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]) = Set.empty[String]
def additionalContext(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Map[String, String] = Map.empty[String, String]
def logLevel(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Option[LogLevel] =
SharedStructuredLogging.logLevel(request, outcome)
def quietLogLevel(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Option[LogLevel] =
SharedStructuredLogging.quietLogLevel(request, outcome)
SharedStructuredLogging.logLevel(LogLevel.Debug.some)(request, outcome)
def logLevelWithDefault(default: Option[LogLevel])(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Option[LogLevel] =
SharedStructuredLogging.logLevel(default)(request, outcome)
def logMessage(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]], now: FiniteDuration): String =
CommonLog.logMessage(ZoneId.systemDefault(), false, true, true)(request, outcome, now)
}
Expand Down Expand Up @@ -135,6 +135,8 @@ object ClientMiddleware {

def withRemovedContextKeys(removedContextKeys: (Request[Pure], Outcome[Option, Throwable, Response[Pure]]) => Set[String]) =
copy(removedContextKeys = removedContextKeys)
def withStaticRemovedContextKeys(removedContextKeys: Set[String]) =
withRemovedContextKeys((_,_) => removedContextKeys)

def withWillLog(willLog: Request[Pure] => F[Boolean]) =
copy(willLog = willLog)
Expand All @@ -150,26 +152,41 @@ object ClientMiddleware {
copy(requestObserveBody = boolean)
def withObserveResponseBody(boolean: Boolean) =
copy(responseObserveBody = boolean)
def withObserveSharedBody(boolean: Boolean) =
withObserveRequestBody(boolean)
.withObserveResponseBody((boolean))

def withRequestBodyEncoder(encoder: Request[Pure] => Option[String]) =
copy(requestBodyEncoder = encoder)
def withResponseBodyEncoder(encoder: Response[Pure] => Option[String]) =
copy(responseBodyEncoder = encoder)
def withSharedBodyEncoder(encoder: Message[Pure] => Option[String]) =
withRequestBodyEncoder(encoder)
.withResponseBodyEncoder(encoder)

def withRequestBodyMaxSize(l: Long) =
copy(requestBodyMaxSize = l)
def withResponseBodyMaxSize(l: Long) =
copy(responseBodyMaxSize = l)
def withSharedBodyMaxSize(l: Long) =
withRequestBodyMaxSize(l)
.withResponseBodyMaxSize(l)

def withLogLevel(logLevel: (Request[Pure], Outcome[Option, Throwable, Response[Pure]]) => Option[LogLevel]) =
copy(logLevel = logLevel)
def withStaticLogLevel(logLevel: Option[LogLevel]) =
withLogLevel((_,_) => logLevel)

def withLogMessage(logMessage: (Request[Pure], Outcome[Option, Throwable, Response[Pure]], FiniteDuration) => String) =
copy(logMessage = logMessage)

def withAllowedRequestHeaders(reqHeaders: Set[CIString]) =
copy(reqHeaders = reqHeaders)
def withAllowedResponseHeaders(respHeaders: Set[CIString]) =
copy(respHeaders = respHeaders)
def withAllowedSharedHeaders(headers: Set[CIString]) =
withAllowedRequestHeaders(headers)
.withAllowedResponseHeaders(headers)

def client(client: Client[F]): Client[F] =
if (requestObserveBody || responseObserveBody) clientWithBody[F](logger, willLog, routeClassifier, reqHeaders, requestIncludeUrl, requestObserveBody, requestBodyEncoder, requestBodyMaxSize, respHeaders, responseObserveBody, responseBodyEncoder, responseBodyMaxSize, removedContextKeys, additionalContext, logLevel, logMessage)(client)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ object ServerMiddleware {
def removedContextKeys(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]) = Set.empty[String]
def additionalContext(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Map[String, String] = Map.empty[String, String]
def logLevel(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Option[LogLevel] =
SharedStructuredLogging.logLevel(request, outcome)
def quietLogLevel(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Option[LogLevel] =
SharedStructuredLogging.quietLogLevel(request, outcome)
SharedStructuredLogging.logLevel(LogLevel.Debug.some)(request, outcome)
def logLevelWithDefault(default: Option[LogLevel])(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Option[LogLevel] =
SharedStructuredLogging.logLevel(default)(request, outcome)
def logMessage(request: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]], now: FiniteDuration): String =
CommonLog.logMessage(ZoneId.systemDefault(), false, false, false)(request, outcome, now)
}
Expand Down Expand Up @@ -155,16 +155,25 @@ object ServerMiddleware {
copy(requestObserveBody = boolean)
def withObserveResponseBody(boolean: Boolean) =
copy(responseObserveBody = boolean)
def withObserveSharedBody(boolean: Boolean) =
withObserveRequestBody(boolean)
.withObserveResponseBody((boolean))

def withRequestBodyEncoder(encoder: Request[Pure] => Option[String]) =
copy(requestBodyEncoder = encoder)
def withResponseBodyEncoder(encoder: Response[Pure] => Option[String]) =
copy(responseBodyEncoder = encoder)
def withSharedBodyEncoder(encoder: Message[Pure] => Option[String]) =
withRequestBodyEncoder(encoder)
.withResponseBodyEncoder(encoder)

def withRequestBodyMaxSize(l: Long) =
copy(requestBodyMaxSize = l)
def withResponseBodyMaxSize(l: Long) =
copy(responseBodyMaxSize = l)
def withSharedBodyMaxSize(l: Long) =
withRequestBodyMaxSize(l)
.withResponseBodyMaxSize(l)

def withAdditionalContext(additionalContext: (Request[Pure], Outcome[Option, Throwable, Response[Pure]]) => Map[String, String]) =
copy(additionalContext = additionalContext)
Expand All @@ -181,6 +190,9 @@ object ServerMiddleware {
copy(reqHeaders = reqHeaders)
def withAllowedResponseHeaders(respHeaders: Set[CIString]) =
copy(respHeaders = respHeaders)
def withAllowedSharedHeaders(headers: Set[CIString]) =
withAllowedRequestHeaders(headers)
.withAllowedResponseHeaders(headers)

def httpApp(app: HttpApp[F]): HttpApp[F] =
if (requestObserveBody || responseObserveBody) httpAppWithBody[F](logger, willLog, routeClassifier, reqHeaders, requestIncludeUrl, requestObserveBody, requestBodyEncoder, requestBodyMaxSize, respHeaders, responseObserveBody, responseBodyEncoder, responseBodyMaxSize, removedContextKeys, additionalContext, logLevel, logMessage)(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,21 @@ private[contextlog] object SharedStructuredLogging {
private[contextlog] def pureRequest[F[_]](req: Request[F]): Request[Pure] = Request(req.method, req.uri, req.httpVersion, req.headers, Stream.empty, req.attributes)
private[contextlog] def pureResponse[F[_]](resp: Response[F]): Response[Pure] = Response(resp.status, resp.httpVersion, resp.headers, Stream.empty, resp.attributes)

private[contextlog] def logLevel(prelude: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Option[LogLevel] = {
private[contextlog] def logLevel(defaultLevel: Option[LogLevel])(prelude: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Option[LogLevel] = {
val _ = prelude
outcome match {
case Outcome.Succeeded(Some(resp)) =>
resp.status.responseClass match {
case Status.Informational => LogLevel.Info.some
case Status.Successful => LogLevel.Info.some
case Status.Redirection => LogLevel.Info.some
case Status.Informational => defaultLevel
case Status.Successful => defaultLevel
case Status.Redirection => defaultLevel
case Status.ClientError =>
if (resp.status.code === 404) LogLevel.Info.some
if (resp.status.code === 404) defaultLevel
else LogLevel.Warn.some
case Status.ServerError => LogLevel.Error.some

}
case Outcome.Succeeded(None) => LogLevel.Info.some
case Outcome.Canceled() => LogLevel.Warn.some
case Outcome.Errored(_) => LogLevel.Error.some
}
}

private[contextlog] def quietLogLevel(prelude: Request[Pure], outcome: Outcome[Option, Throwable, Response[Pure]]): Option[LogLevel] = {
val _ = prelude
outcome match {
case Outcome.Succeeded(Some(resp)) =>
resp.status.responseClass match {
case Status.Informational => None
case Status.Successful => None
case Status.Redirection => None
case Status.ClientError =>
if (resp.status.code === 404) None
else LogLevel.Warn.some
case Status.ServerError => LogLevel.Error.some

}
case Outcome.Succeeded(None) => None
case Outcome.Succeeded(None) => defaultLevel
case Outcome.Canceled() => LogLevel.Warn.some
case Outcome.Errored(_) => LogLevel.Error.some
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.typelevel.log4cats.testing.StructuredTestingLogger
import org.typelevel.log4cats.extras._
import org.typelevel.log4cats.testing.StructuredTestingLogger.TRACE
import org.typelevel.log4cats.testing.StructuredTestingLogger.DEBUG
import org.typelevel.log4cats.testing.StructuredTestingLogger.INFO
import org.typelevel.log4cats.testing.StructuredTestingLogger.DEBUG
import org.typelevel.log4cats.testing.StructuredTestingLogger.WARN
import org.typelevel.log4cats.testing.StructuredTestingLogger.ERROR
import org.http4s.client.Client
Expand Down Expand Up @@ -44,7 +44,7 @@ class MainSpec extends CatsEffectSuite {
assertEquals(
logged,
Vector(
INFO(
DEBUG(
"Http Server - GET",
None,
Map(
Expand Down Expand Up @@ -84,7 +84,7 @@ class MainSpec extends CatsEffectSuite {
assertEquals(
logged,
Vector(
INFO(
DEBUG(
"Http Server - GET",
None,
Map(
Expand Down Expand Up @@ -132,7 +132,7 @@ class MainSpec extends CatsEffectSuite {
assertEquals(
logged,
Vector(
INFO(
DEBUG(
"Req Body - Hello from Request!\nResp Body - Hello from Response!",
None,
Map(
Expand Down Expand Up @@ -182,7 +182,7 @@ class MainSpec extends CatsEffectSuite {
assertEquals(
logged,
Vector(
INFO(
DEBUG(
"HttpClient - GET",
None,
Map(
Expand Down Expand Up @@ -235,7 +235,7 @@ class MainSpec extends CatsEffectSuite {
assertEquals(
logged,
Vector(
INFO(
DEBUG(
"HttpClient - GET",
None,
Map(
Expand Down

0 comments on commit 9af3250

Please sign in to comment.