Skip to content

Commit

Permalink
Add logging setup
Browse files Browse the repository at this point in the history
  • Loading branch information
eikek committed Feb 22, 2024
1 parent 393ca7a commit ef8573c
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 5 deletions.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
RS_REDIS_HOST = "rsdev";
RS_REDIS_PORT = "6379";
RS_CONTAINER = "rsdev";
RS_LOG_LEVEL = "3";

buildInputs = with pkgs;
with selfPkgs; [
Expand All @@ -85,6 +86,7 @@
RS_REDIS_HOST = "localhost";
RS_REDIS_PORT = "16379";
VM_SSH_PORT = "10022";
RS_LOG_LEVEL = "3";

buildInputs = with pkgs;
with selfPkgs; [
Expand Down
66 changes: 66 additions & 0 deletions modules/commons/src/main/scala/io/renku/logging/LoggingSetup.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024 Swiss Data Science Center (SDSC)
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
* Eidgenössische Technische Hochschule Zürich (ETHZ).
*
* 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.renku.logging

import scribe.Level
import scribe.writer.SystemOutWriter
import scribe.format.Formatter

object LoggingSetup:

def doConfigure(verbosity: Int): Unit =
println(s">> Setting up logging with verbosity=$verbosity")
val root = scribe.Logger.root.clearHandlers().clearModifiers()
verbosity match
case n if n <= 0 =>
()

case 1 =>
root.withMinimumLevel(Level.Warn).replace()
()

case 2 =>
root.withMinimumLevel(Level.Warn).replace()
configureRenkuSearch(Level.Info)

case 3 =>
root.withMinimumLevel(Level.Info).replace()
configureRenkuSearch(Level.Debug)

case 4 =>
root.withMinimumLevel(Level.Info).replace()
configureRenkuSearch(Level.Trace)

case _ =>
root.withMinimumLevel(Level.Debug).replace()
configureRenkuSearch(Level.Trace)

private def configureRenkuSearch(level: Level): Unit = {
scribe
.Logger("io.renku.search")
.clearHandlers()
.withHandler(
formatter = Formatter.classic,
writer = SystemOutWriter,
minimumLevel = Some(level)
)
.replace()

()
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object ConfigValues extends ConfigDecoders:
env(s"${prefix}_$name")

val logLevel: ConfigValue[Effect, Int] =
renv("LOG_LEVEL").default("1").as[Int]
renv("LOG_LEVEL").default("2").as[Int]

val redisConfig: ConfigValue[Effect, RedisConfig] = {
val host = renv("REDIS_HOST").default("localhost").as[RedisHost]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.renku.search.api

import cats.effect.{ExitCode, IO, IOApp}
import io.renku.logging.LoggingSetup

object Microservice extends IOApp:

Expand All @@ -27,6 +28,7 @@ object Microservice extends IOApp:
override def run(args: List[String]): IO[ExitCode] =
for {
config <- loadConfig
_ <- IO(LoggingSetup.doConfigure(config.verbosity))
_ <- HttpApplication[IO](config.solrConfig)
.flatMap(HttpServer.build)
.use(_ => IO.never)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@

package io.renku.search.api

import cats.syntax.all.*
import ciris.{ConfigValue, Effect}
import io.renku.search.config.ConfigValues
import io.renku.solr.client.SolrConfig

final case class SearchApiConfig(
solrConfig: SolrConfig
solrConfig: SolrConfig,
verbosity: Int
)

object SearchApiConfig:
val config: ConfigValue[Effect, SearchApiConfig] =
ConfigValues.solrConfig.map(SearchApiConfig.apply)
(ConfigValues.solrConfig, ConfigValues.logLevel).mapN(SearchApiConfig.apply)
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package io.renku.search.provision
import cats.effect.{ExitCode, IO, IOApp, Temporal}
import io.renku.search.solr.schema.Migrations
import io.renku.solr.client.migration.SchemaMigrator
import io.renku.logging.LoggingSetup
import scribe.Scribe
import scribe.cats.*

Expand All @@ -32,6 +33,7 @@ object Microservice extends IOApp:
override def run(args: List[String]): IO[ExitCode] =
for {
config <- loadConfig
_ <- IO(LoggingSetup.doConfigure(config.verbosity))
_ <- runSolrMigrations(config)
_ <- startProvisioning(config)
} yield ExitCode.Success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ final case class SearchProvisionConfig(
redisConfig: RedisConfig,
queueName: QueueName,
solrConfig: SolrConfig,
retryOnErrorDelay: FiniteDuration
retryOnErrorDelay: FiniteDuration,
verbosity: Int
)

object SearchProvisionConfig:
Expand All @@ -40,5 +41,6 @@ object SearchProvisionConfig:
ConfigValues.redisConfig,
ConfigValues.eventsQueueName,
ConfigValues.solrConfig,
ConfigValues.retryOnErrorDelay
ConfigValues.retryOnErrorDelay,
ConfigValues.logLevel
).mapN(SearchProvisionConfig.apply)

0 comments on commit ef8573c

Please sign in to comment.