Skip to content
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
Expand Up @@ -40,12 +40,12 @@ final class SelfCheckAlg[F[_]](config: Config)(implicit
} yield ()

private def checkGitBinary: F[Unit] =
logger.attemptLogWarn_(execFailedMessage("git")) {
logger.attemptWarn.log_(execFailedMessage("git")) {
gitAlg.version.flatMap(output => logger.info(s"Using $output"))
}

private def checkScalafmtBinary: F[Unit] =
logger.attemptLogWarn_(execFailedMessage(scalafmtBinary)) {
logger.attemptWarn.log_(execFailedMessage(scalafmtBinary)) {
scalafmtAlg.version.flatMap(output => logger.info(s"Using $output"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ final class StewardAlg[F[_]](config: Config)(implicit
private def steward(repo: Repo): F[Either[Throwable, Unit]] = {
val label = s"Steward ${repo.show}"
logger.infoTotalTime(label) {
logger.attemptLogLabel(util.string.lineLeftRight(label), Some(label)) {
logger.attemptError.bracket(util.string.lineLeftRight(label), Some(label)) {
F.guarantee(
repoCacheAlg.checkCache(repo).flatMap { case (data, fork) =>
pruningAlg.needsAttention(data).flatMap { case (attentionNeeded, updates) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ final class EditAlg[F[_]](implicit
): F[EditAttempt] =
for {
_ <- logger.info(s"Running migration $migration")
result <- logger.attemptLogWarn("Scalafix migration failed")(
result <- logger.attemptWarn.log("Scalafix migration failed")(
buildToolDispatcher.runMigration(repo, config, migration)
)
verb = if (result.isRight) "Applied" else "Failed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class HookExecutor[F[_]](implicit
for {
_ <- logger.info(s"Executing post-update hook for ${hook.groupId}:${hook.artifactId.name}")
repoDir <- workspaceAlg.repoDir(repo)
result <- logger.attemptLogWarn("Post-update hook failed") {
result <- logger.attemptWarn.log("Post-update hook failed") {
processAlg.execMaybeSandboxed(hook.useSandbox)(hook.command, repoDir)
}
maybeCommit <- gitAlg.commitAllIfDirty(repo, hook.commitMessage(update))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,20 @@ final class NurtureAlg[F[_]](config: Config)(implicit
oldNumber: PullRequestNumber,
newNumber: PullRequestNumber
): F[Unit] =
logger.attemptLogWarn_(s"Closing PR #$oldNumber failed") {
logger.attemptWarn.bracket_(
s"Closing obsolete PR ${oldUrl.renderString} for ${oldUpdate.show}"
) {
for {
_ <- logger.info(s"Closing obsolete PR ${oldUrl.renderString} for ${oldUpdate.show}")
_ <- pullRequestRepository.changeState(repo, oldUrl, PullRequestState.Closed)
comment = s"Superseded by ${vcsApiAlg.referencePullRequest(newNumber)}."
_ <- vcsApiAlg.commentPullRequest(repo, oldNumber, comment)
_ <- vcsApiAlg.closePullRequest(repo, oldNumber)
_ <- removeRemoteBranch(repo, git.branchFor(oldUpdate))
_ <- pullRequestRepository.changeState(repo, oldUrl, PullRequestState.Closed)
} yield ()
}

private def removeRemoteBranch(repo: Repo, branch: Branch): F[Unit] =
logger.attemptLogWarn_(s"Removing remote branch ${branch.name} failed") {
logger.attemptWarn.log_(s"Removing remote branch ${branch.name} failed") {
gitAlg.removeBranch(repo, branch)
}

Expand Down
43 changes: 20 additions & 23 deletions modules/core/src/main/scala/org/scalasteward/core/util/logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,31 @@ import org.typelevel.log4cats.Logger
import scala.concurrent.duration.FiniteDuration

object logger {
implicit final class LoggerOps[F[_]](private val logger: Logger[F]) extends AnyVal {
def attemptLogLabel[A](label: String, errorLabel: Option[String] = None)(fa: F[A])(implicit
F: MonadThrow[F]
): F[Either[Throwable, A]] =
logger.info(label) >> attemptLogError(s"${errorLabel.getOrElse(label)} failed")(fa)

def attemptLogWarn[A](message: String)(fa: F[A])(implicit
F: MonadThrow[F]
): F[Either[Throwable, A]] =
attemptLogImpl(fa, logger.warn(_)(message))
final class AttemptLoggerOps[F[_]](
logger: Logger[F],
logThrowable: (Throwable, String) => F[Unit]
)(implicit F: MonadThrow[F]) {
def log[A](msg: String)(fa: F[A]): F[Either[Throwable, A]] =
fa.attempt.flatTap(_.fold(t => logThrowable(t, msg), _ => F.unit))

def attemptLogWarn_[A](message: String)(fa: F[A])(implicit F: MonadThrow[F]): F[Unit] =
attemptLogImpl_(fa, logger.warn(_)(message))
def log_[A](msg: String)(fa: F[A]): F[Unit] =
log(msg)(fa).void

def attemptLogError[A](message: String)(fa: F[A])(implicit
F: MonadThrow[F]
def bracket[A](label: String, errorLabel: Option[String] = None)(
fa: F[A]
): F[Either[Throwable, A]] =
attemptLogImpl(fa, logger.error(_)(message))
logger.info(label) >> log(s"${errorLabel.getOrElse(label)} failed")(fa)

private def attemptLogImpl[A](fa: F[A], log: Throwable => F[Unit])(implicit
F: MonadThrow[F]
): F[Either[Throwable, A]] =
fa.attempt.flatTap(_.fold(log, _ => F.unit))
def bracket_[A](label: String, errorLabel: Option[String] = None)(fa: F[A]): F[Unit] =
bracket(label, errorLabel)(fa).void
}

implicit final class LoggerOps[F[_]](private val logger: Logger[F]) extends AnyVal {
def attemptWarn(implicit F: MonadThrow[F]): AttemptLoggerOps[F] =
new AttemptLoggerOps(logger, logger.warn(_)(_))

private def attemptLogImpl_[A](fa: F[A], log: Throwable => F[Unit])(implicit
F: MonadThrow[F]
): F[Unit] =
fa.attempt.flatMap(_.fold(log, _ => F.unit))
def attemptError(implicit F: MonadThrow[F]): AttemptLoggerOps[F] =
new AttemptLoggerOps(logger, logger.error(_)(_))

def infoTimed[A](msg: FiniteDuration => String)(fa: F[A])(implicit
dateTimeAlg: DateTimeAlg[F],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class VCSRepoAlg[F[_]](config: Config)(implicit
}

private def initSubmodules(repo: Repo): F[Unit] =
logger.attemptLogWarn_("Initializing and cloning submodules failed") {
logger.attemptWarn.log_("Initializing and cloning submodules failed") {
gitAlg.initSubmodules(repo)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import org.scalasteward.core.mock.{MockEff, MockState}
import org.scalasteward.core.util.logger.LoggerOps

class loggerTest extends CatsEffectSuite {
test("attemptLogLabel") {
test("attemptError.bracket_") {
final case class Err(msg: String) extends Throwable(msg)
val err = Err("hmm?")
logger.attemptLogLabel("run")(MockEff.raiseError(err)).runS(MockState.empty).map { state =>
assertEquals(state.trace, Vector(Log("run"), Log((Some(err), "run failed"))))
logger.attemptError.bracket_("run")(MockEff.raiseError(err)).runS(MockState.empty).map {
state =>
assertEquals(state.trace, Vector(Log("run"), Log((Some(err), "run failed"))))
}
}

Expand Down