Permalink
Browse files

Reduce namespace pollution in JS apps

ScalaJS behaves a bit differently when you're a module than it does when
you're an app, and the things you @JSExportTopLevel` can be problematic.
This redesigns the API to do a *lot* fewer top-level exports.
  • Loading branch information...
sarahgerweck committed Feb 6, 2019
1 parent 3e2519e commit c7b64f3ee9d7e59a05d55625664222f0c13da9a6
@@ -83,3 +83,8 @@ changes to how it's coded or built, see the Git history.

* Add Scala support for 2.13.0-M5
* Update Scala.js to 0.6.26
* Reduce top-level exports in Scala.js
* To reduce the likelihood of conflicts in combined apps, `getLogger` &
`Log4s` are now the only top-level exports. If you want to access objects
like `Info`, you should now call `Log4s.Info`.
* Log4s support is still considered experimental.
@@ -4,7 +4,33 @@ package log4sjs
import scala.scalajs.js
import js.annotation._

@JSExportTopLevel("Log4s")
object Log4s {
@JSExportTopLevel("getLogger")
@JSExport("getLogger")
def getLogger(name: String) = org.slf4j.LoggerFactory.getLogger(name)
@JSExport("Config")
val Config = Log4sConfig
@JSExport("MDC")
val MDC = log4sjs.Log4sMDC
@JSExport("LogThreshold")
val LogThreshold = new AnyRef {
@JSExport
val AllThreshold = log4sjs.LogThreshold.AllThreshold
@JSExport
val OffThreshold = log4sjs.LogThreshold.OffThreshold
}

val Level = new AnyRef {
@JSExport
val Trace = org.log4s.Trace
@JSExport
val Debug = org.log4s.Debug
@JSExport
val Info = org.log4s.Info
@JSExport
val Warn = org.log4s.Warn
@JSExport
val Error = org.log4s.Error
}
}
@@ -118,27 +118,22 @@ object Log4sConfig extends Log4sConfig { thisConfig =>
LoggerState.update(parts, updatedState)
}

@JSExportTopLevel("Config.setLoggerThreshold")
def setLoggerThreshold(name: String, threshold: LogThreshold): Unit = {
logger(name, threshold = Some(Option(threshold)))
}

@JSExportTopLevel("Config.setLoggerThreshold")
def setLoggerThreshold(name: String, level: LogLevel): Unit = {
logger(name, threshold = Some(Option(LevelThreshold(level))))
}

@JSExportTopLevel("Config.setLoggerThreshold")
def setLoggerThreshold(name: String, threshold: String): Unit = {
setLoggerThreshold(name, LogThreshold.forName(threshold))
}

@JSExportTopLevel("Config.resetLoggerThreshold")
def resetLoggerThreshold(name: String): Unit = {
logger(name, threshold = Some(None))
}

@JSExportTopLevel("Config.setLoggerAppenders")
def setLoggerAppendersDynamic(name: String, additive: Boolean, appenders: js.Array[Log4sAppender.DynamicType]): Unit = {
setLoggerAppenders(name, additive, appenders.toSeq)
}
@@ -149,7 +144,6 @@ object Log4sConfig extends Log4sConfig { thisConfig =>
}

/** Add an appender for a given logger */
@JSExportTopLevel("Config.addLoggerAppender")
def addLoggerAppenderDynamic(name: String, appender: Log4sAppender.DynamicType): Unit = {
addLoggerAppender(name, appender)
}
@@ -163,7 +157,6 @@ object Log4sConfig extends Log4sConfig { thisConfig =>
LoggerState(parts) = updatedState
}

@JSExportTopLevel("Config.resetLoggerAppenders")
def resetLoggerAppenders(name: String) = {
logger(name, appenders = Some(None))
}
@@ -5,7 +5,6 @@ import scala.scalajs.js
import js.annotation._
import js.JSConverters._

@JSExportTopLevel("MDC")
object Log4sMDC {
@JSExport
def get(key: String): String = MDC.get(key).orNull
@@ -14,6 +14,9 @@ sealed trait LogThreshold extends Any {
def permits(ll: LogLevel): Boolean = this <= LevelThreshold(ll)
}
object LogThreshold {
case object AllThreshold extends LogThreshold
case object OffThreshold extends LogThreshold

implicit val order: Ordering[LogThreshold] = Ordering.by {
case OffThreshold => Int.MaxValue
case LevelThreshold(Error) => 40000
@@ -32,11 +35,6 @@ object LogThreshold {
}
}

@JSExportTopLevel("AllThreshold")
case object AllThreshold extends LogThreshold

@JSExportTopLevel("OffThreshold")
case object OffThreshold extends LogThreshold

object LevelThreshold {
def apply(ll: LogLevel) = new LevelThreshold(ll)
@@ -2,6 +2,6 @@ package org.log4s

trait `package-platform` {
final val Log4sConfig: log4sjs.Log4sConfig = log4sjs.Log4sConfig
final val AllThreshold = log4sjs.AllThreshold
final val OffThreshold = log4sjs.OffThreshold
final val AllThreshold = log4sjs.LogThreshold.AllThreshold
final val OffThreshold = log4sjs.LogThreshold.OffThreshold
}
@@ -31,32 +31,27 @@ object LogLevel {
* An error generally indicates a bug or an environment
* problem that warrants some kind of immediate intervention.
*/
@JSExportTopLevel("Error")
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.
*/
@JSExportTopLevel("Warn")
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.)
*/
@JSExportTopLevel("Info")
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.
*/
@JSExportTopLevel("Debug")
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.
*/
@JSExportTopLevel("Trace")
case object Trace extends LogLevel

0 comments on commit c7b64f3

Please sign in to comment.