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

Remove static logger. #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 2 additions & 8 deletions src/main/scala/com/rawlabs/utils/core/RawMBeansManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

package com.rawlabs.utils.core

import com.typesafe.scalalogging.StrictLogging

import java.lang.management.ManagementFactory
import javax.management.ObjectName
import scala.util.control.NonFatal
Expand All @@ -31,7 +29,7 @@ trait RawMBean {
def getMBeanName: String
}

object RawMBeansManager extends StrictLogging {
object RawMBeansManager {

private val mbs = ManagementFactory.getPlatformMBeanServer
private var mbeanMap = Map.empty[String, RawMBean]
Expand All @@ -46,9 +44,7 @@ object RawMBeansManager extends StrictLogging {
require(mbean != null, "MBean cannot be null")
require(mbean.getMBeanName != null, "MBean name cannot be null")
mbeanLock.synchronized {
if (mbs.isRegistered(new ObjectName(mbean.getMBeanName))) {
logger.warn(s"MBean ${mbean.getMBeanName} already registered")
} else {
if (!mbs.isRegistered(new ObjectName(mbean.getMBeanName))) {
val objectName = new ObjectName(mbean.getMBeanName)
mbs.registerMBean(mbean, objectName)
mbeanMap += (mbean.getMBeanName -> mbean)
Expand All @@ -68,8 +64,6 @@ object RawMBeansManager extends StrictLogging {
if (mbs.isRegistered(objectName)) {
mbs.unregisterMBean(objectName)
mbeanMap -= name
} else {
logger.warn(s"MBean $name is not registered")
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/com/rawlabs/utils/core/RawService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.rawlabs.utils.core

import com.typesafe.scalalogging.StrictLogging
import com.typesafe.scalalogging.Logger

import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.atomic.AtomicBoolean
Expand All @@ -39,11 +39,13 @@ object RawService {
*
* Used for services (aka. components/modules) that may require centralized stopping.
*/
trait RawService extends StrictLogging {
trait RawService {

import RawService._
protected val stopped = new AtomicBoolean(false)

protected def logger: Logger

logger.debug(s"Adding service: $this")
services.add(this)

Expand Down
15 changes: 9 additions & 6 deletions src/main/scala/com/rawlabs/utils/core/RawSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package com.rawlabs.utils.core

import com.typesafe.config._
import com.typesafe.scalalogging.StrictLogging
import com.typesafe.scalalogging.Logger

import java.time.Duration
import java.util.concurrent.TimeUnit
Expand All @@ -30,9 +30,10 @@ object RawSettings {
}

class RawSettings(
logger: Logger,
protected val lowPriorityConfig: Config = ConfigFactory.load(),
protected val highPriorityConfig: Config = ConfigFactory.empty()
) extends StrictLogging {
) {

import RawSettings._

Expand All @@ -54,8 +55,9 @@ class RawSettings(
}
}

def this(renderAsString: String) = {
def this(logger: Logger, renderAsString: String) = {
this(
logger,
try {
ConfigFactory.parseString(renderAsString)
} catch {
Expand Down Expand Up @@ -105,22 +107,23 @@ class RawSettings(
.withFallback(moduleConfig)
.withFallback(ConfigFactory.defaultReference())
.resolve()
new RawSettings(config, highPriorityConfig)
new RawSettings(logger, config, highPriorityConfig)
}

def withFallback(settings: RawSettings): RawSettings = {
new RawSettings(
logger,
lowPriorityConfig.withFallback(settings.lowPriorityConfig),
highPriorityConfig.withFallback(settings.highPriorityConfig)
)
}

def cloneWith(settings: Map[String, Any]): RawSettings = {
new RawSettings(lowPriorityConfig, ConfigFactory.parseMap(settings.asJava).withFallback(highPriorityConfig))
new RawSettings(logger, lowPriorityConfig, ConfigFactory.parseMap(settings.asJava).withFallback(highPriorityConfig))
}

def cloneWith(settings: String): RawSettings = {
new RawSettings(lowPriorityConfig, ConfigFactory.parseString(settings).withFallback(highPriorityConfig))
new RawSettings(logger, lowPriorityConfig, ConfigFactory.parseString(settings).withFallback(highPriorityConfig))
}

def cloneWith(settings: (String, Any)*): RawSettings = {
Expand Down
34 changes: 13 additions & 21 deletions src/main/scala/com/rawlabs/utils/core/RawUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package com.rawlabs.utils.core

import com.google.common.io.Resources
import com.google.common.util.concurrent.ThreadFactoryBuilder
import com.typesafe.scalalogging.StrictLogging
import com.typesafe.scalalogging.Logger
import org.apache.commons.io.FileUtils

import java.io.{ByteArrayOutputStream, InputStream}
Expand All @@ -31,11 +31,8 @@ import org.apache.commons.text.StringEscapeUtils

/**
* "Random" collection of utility methods.
*
* NOTE that this classes uses the `StrictLogging` trait from the `com.typesafe.scalalogging` package.
* TODO (msb): Remove StrictLogging and make logger a parameter where needed.
*/
object RawUtils extends StrictLogging {
object RawUtils {

/**
* Convert a user string back to its original "intended" representation.
Expand Down Expand Up @@ -71,12 +68,14 @@ object RawUtils extends StrictLogging {
)
}

def newThreadFactory(name: String, daemon: Boolean = true): ThreadFactory = {
def newThreadFactory(name: String, maybeLogger: Option[Logger] = None, daemon: Boolean = true): ThreadFactory = {
new ThreadFactoryBuilder()
.setNameFormat(s"$name-%d")
.setUncaughtExceptionHandler((t: Thread, e: Throwable) => {
logger.warn(s"Uncaught exception on thread: ${t.getName}", e)
})
.setUncaughtExceptionHandler {
case (t: Thread, e: Throwable) => {
maybeLogger.foreach(_.warn(s"Uncaught exception on thread: ${t.getName}", e))
}
}
.setDaemon(daemon)
.build()
}
Expand Down Expand Up @@ -105,26 +104,22 @@ object RawUtils extends StrictLogging {
System.getProperty("os.name").toLowerCase().contains("mac os x")
}

def withSuppressNonFatalException(f: => Unit, silent: Boolean = false): Unit = {
def withSuppressNonFatalException(f: => Unit, maybeLogger: Option[Logger] = None): Unit = {
if (Thread.interrupted()) {
throw new InterruptedException()
}
try {
f
} catch {
case NonFatal(t) => if (!silent) {
logger.warn("Suppressing uncaught exception: ", t)
}
case NonFatal(t) => maybeLogger.foreach(_.warn("Suppressing uncaught exception: ", t))
}
}

def withSuppressNonFatalExceptionUninterruptible(f: => Unit, silent: Boolean = false): Unit = {
def withSuppressNonFatalExceptionUninterruptible(f: => Unit, maybeLogger: Option[Logger] = None): Unit = {
try {
f
} catch {
case NonFatal(t) => if (!silent) {
logger.warn("Suppressing uncaught exception: ", t)
}
case NonFatal(t) => maybeLogger.foreach(_.warn("Suppressing uncaught exception: ", t))
}
}

Expand All @@ -144,10 +139,7 @@ object RawUtils extends StrictLogging {

def deleteTestFile(file: Path): Unit = {
withSuppressNonFatalException {
val deleted = Files.deleteIfExists(file)
if (!deleted) {
logger.warn(s"Could not delete test file: $file")
}
Files.deleteIfExists(file)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.rawlabs.utils.core

import com.typesafe.scalalogging.StrictLogging
import com.typesafe.scalalogging.Logger

/**
* Wraps a Runnable to log any uncaught exceptions.
Expand All @@ -28,7 +28,7 @@ import com.typesafe.scalalogging.StrictLogging
* @param delegate The runnable to wrap.
* @param propagate Whether to rethrow the exception.
*/
class RawVerboseRunnable(delegate: Runnable, propagate: Boolean = false) extends Runnable with StrictLogging {
class RawVerboseRunnable(logger: Logger, delegate: Runnable, propagate: Boolean = false) extends Runnable {
override def run(): Unit = {
try {
delegate.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
package com.rawlabs.utils.core

import com.typesafe.config.ConfigFactory
import com.typesafe.scalalogging.StrictLogging

import scala.collection.JavaConverters._
import scala.collection.mutable

trait SettingsTestContext {
trait SettingsTestContext extends StrictLogging {
protected var properties: mutable.Map[String, Any] = mutable.Map[String, Any]()

implicit def settings: RawSettings = new RawSettings(
logger,
ConfigFactory.load(),
ConfigFactory.parseMap(properties.asJava)
)
Expand Down