0

I try to run log4javascript with ScalaJS and PhantomJS and get an error, with Rhino it works.

I took following ScalaJS example: https://github.com/scala-js/scalajs-tutorial

and log4javascript I took from another ScalaJS example: https://github.com/ochrons/scalajs-spa-tutorial more specificly these 3 files: https://github.com/ochrons/scalajs-spa-tutorial/tree/master/client/src/main/scala/spatutorial/client/logger

I modified TutorialApp to include some logging:

package tutorial.webapp

import scala.scalajs.js.JSApp

import org.scalajs.jquery.jQuery
import tutorial.logger.LoggerFactory

object TutorialApp extends JSApp {
  println("Before getLogger...")
  val log = LoggerFactory.getLogger(getClass().getName)
  log.info("After getLogger...")

  def main(): Unit = {
    jQuery(setupUI _)
  }

  def setupUI(): Unit = {
    jQuery("""<button type="button">Click me!</button>""")
      .click(addClickedMessage _)
      .appendTo(jQuery("body"))
    jQuery("body").append("<p>Hello World</p>")
  }

  def addClickedMessage(): Unit = {
    jQuery("body").append("<p>You clicked the button!</p>")
    log.info("Button clicked...")
  }
}

Modified build.sbt

enablePlugins(ScalaJSPlugin)

name := "Scala.js Tutorial"

scalaVersion := "2.11.7"

libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "0.8.1"
libraryDependencies += "be.doeraene" %%% "scalajs-jquery" % "0.8.0"

jsDependencies += RuntimeDOM

skip in packageJSDependencies := false

// uTest settings
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.3.0" % "test"
testFrameworks += new TestFramework("utest.runner.Framework")

persistLauncher in Compile := true
persistLauncher in Test := false

// Modifications to original tutorial are here:
scalaJSStage in Global := FastOptStage // If NOT commented out: Uses Phantom, if IS commented out: Uses Rhino

libraryDependencies += "org.webjars" % "log4javascript" % "1.4.13"

jsDependencies += "org.webjars" % "log4javascript" % "1.4.13" / "1.4.13/log4javascript.js" 

Logger files are in src/main/scala/tutorial/logger, only package names are changed to get them compile. Modified package.scala:

package tutorial

package object logger {
  private val defaultLogger = LoggerFactory.getLogger("Log")

  def log = defaultLogger
}

Modified LoggerFactory.scala

package tutorial.logger

import scala.annotation.elidable
import scala.annotation.elidable._

trait Logger {
  /*
   * Use @elidable annotation to completely exclude functions from the compiler generated byte-code based on
   * the specified level. In a production build most logging functions will simply disappear with no runtime
   * performance penalty.
   *
   * Specify level as a compiler parameter
   * > scalac -Xelide-below INFO
  */
  @elidable(FINEST) def trace(msg: String, e: Exception): Unit
  @elidable(FINEST) def trace(msg: String): Unit
  @elidable(FINE) def debug(msg: String, e: Exception): Unit
  @elidable(FINE) def debug(msg: String): Unit
  @elidable(INFO) def info(msg: String, e: Exception): Unit
  @elidable(INFO) def info(msg: String): Unit
  @elidable(WARNING) def warn(msg: String, e: Exception): Unit
  @elidable(WARNING) def warn(msg: String): Unit
  @elidable(SEVERE) def error(msg: String, e: Exception): Unit
  @elidable(SEVERE) def error(msg: String): Unit
  @elidable(SEVERE) def fatal(msg: String, e: Exception): Unit
  @elidable(SEVERE) def fatal(msg: String): Unit

  def enableServerLogging(url: String): Unit
  def disableServerLogging(): Unit
}

object LoggerFactory {
  private[logger] def createLogger(name: String) = {}

  lazy val consoleAppender = new BrowserConsoleAppender
  lazy val popupAppender = new PopUpAppender

  /**
   * Create a logger that outputs to browser console
   */
  def getLogger(name: String): Logger = {
    val nativeLogger = Log4JavaScript.log4javascript.getLogger(name)
    nativeLogger.addAppender(consoleAppender)
    new L4JSLogger(nativeLogger)
  }

  /**
   * Create a logger that outputs to a separate popup window
   */
  def getPopUpLogger(name: String): Logger = {
    val nativeLogger = Log4JavaScript.log4javascript.getLogger(name)
    nativeLogger.addAppender(popupAppender)
    new L4JSLogger(nativeLogger)
  }
}

Modified Log4javascript.scala

package tutorial.logger

import scala.scalajs.js
import scala.scalajs.js.annotation.JSName

/**
 * Facade for functions in log4javascript that we need
 */
@js.native
private[logger] trait Log4JavaScript extends js.Object {
  def getLogger(name:js.UndefOr[String]):JSLogger = js.native
  def setEnabled(enabled:Boolean):Unit = js.native
  def isEnabled:Boolean = js.native
}

@js.native
@JSName("log4javascript.Level")
private[logger] trait Level extends js.Object {
  val ALL:Level = js.native
  val TRACE:Level = js.native
  val DEBUG:Level = js.native
  val INFO:Level = js.native
  val WARN:Level = js.native
  val ERROR:Level = js.native
  val FATAL:Level = js.native
}

@js.native
@JSName("log4javascript.Logger")
private[logger] trait JSLogger extends js.Object {
  def addAppender(appender:Appender):Unit = js.native
  def removeAppender(appender:Appender):Unit = js.native
  def removeAllAppenders(appender:Appender):Unit = js.native
  def setLevel(level:Level):Unit = js.native
  def getLevel:Level = js.native
  def trace(msg:String, error:js.UndefOr[js.Error]):Unit = js.native
  def debug(msg:String, error:js.UndefOr[js.Error]):Unit = js.native
  def info(msg:String, error:js.UndefOr[js.Error]):Unit = js.native
  def warn(msg:String, error:js.UndefOr[js.Error]):Unit = js.native
  def error(msg:String, error:js.UndefOr[js.Error]):Unit = js.native
  def fatal(msg:String, error:js.UndefOr[js.Error]):Unit = js.native
  def trace(msg:String):Unit = js.native
  def debug(msg:String):Unit = js.native
  def info(msg:String):Unit = js.native
  def warn(msg:String):Unit = js.native
  def error(msg:String):Unit = js.native
  def fatal(msg:String):Unit = js.native
}

@js.native
@JSName("log4javascript.Layout")
private[logger] trait Layout extends js.Object

@js.native
@JSName("log4javascript.JsonLayout")
private[logger] class JsonLayout extends Layout

@js.native
@JSName("log4javascript.Appender")
private[logger] trait Appender extends js.Object {
  def setLayout(layout:Layout):Unit = js.native
  def setThreshold(level:Level):Unit = js.native
}

@js.native
@JSName("log4javascript.BrowserConsoleAppender")
private[logger] class BrowserConsoleAppender extends Appender

@js.native
@JSName("log4javascript.PopUpAppender")
private[logger] class PopUpAppender extends Appender

@js.native
@JSName("log4javascript.AjaxAppender")
private[logger] class AjaxAppender(url:String) extends Appender {
  def addHeader(header:String, value:String):Unit = js.native
}

@js.native
private[logger] object Log4JavaScript extends js.GlobalScope {
  val log4javascript:Log4JavaScript = js.native
}

class L4JSLogger(jsLogger:JSLogger) extends Logger {

  private var ajaxAppender:AjaxAppender = null

  private def undefOrError(e:Exception):js.UndefOr[js.Error] = {
    if(e == null)
      js.undefined
    else
      e.asInstanceOf[js.Error]
  }

  override def trace(msg: String, e: Exception): Unit = jsLogger.trace(msg, undefOrError(e))
  override def trace(msg: String): Unit = jsLogger.trace(msg)
  override def debug(msg: String, e: Exception): Unit = jsLogger.debug(msg, undefOrError(e))
  override def debug(msg: String): Unit = jsLogger.debug(msg)
  override def info(msg: String, e: Exception): Unit = jsLogger.info(msg, undefOrError(e))
  override def info(msg: String): Unit = jsLogger.info(msg)
  override def warn(msg: String, e: Exception): Unit = jsLogger.warn(msg, undefOrError(e))
  override def warn(msg: String): Unit = jsLogger.warn(msg)
  override def error(msg: String, e: Exception): Unit = jsLogger.error(msg, undefOrError(e))
  override def error(msg: String): Unit = jsLogger.error(msg)
  override def fatal(msg: String, e: Exception): Unit = jsLogger.fatal(msg, undefOrError(e))
  override def fatal(msg: String): Unit = jsLogger.fatal(msg)

  override def enableServerLogging(url: String): Unit = {
    if(ajaxAppender == null) {
      ajaxAppender = new AjaxAppender(url)
      ajaxAppender.addHeader("Content-Type", "application/json")
      ajaxAppender.setLayout(new JsonLayout)
      jsLogger.addAppender(ajaxAppender)

    }
  }

  override def disableServerLogging():Unit = {
    if(ajaxAppender != null) {
      jsLogger.removeAppender(ajaxAppender)
      ajaxAppender = null
    }
  }
}

When I try to sbt run, I get following error messages:

> run
[info] Running tutorial.webapp.TutorialApp
Before getLogger...
TypeError: undefined is not an object (evaluating '$g["log4javascript"]["getLogger"]')

  /tmp/phantomjs-launcher8416853343047081941.js:9 in onError


  /tmp/phantomjs-launcher8416853343047081941.js:11 in onError
  file:///home/jk/workspace/scalajs-tutorial-0.6.x/target/scala-2.11/scala-js-tutorial-fastopt.js:1085 (in function "getLogger__T__Ltutorial_logger_Logger")

  /tmp/phantomjs-launcher8416853343047081941.js:13
  file:///home/jk/workspace/scalajs-tutorial-0.6.x/target/scala-2.11/scala-js-tutorial-fastopt.js:1969 (in function "init___")

  /tmp/phantomjs-launcher8416853343047081941.js:13
  file:///home/jk/workspace/scalajs-tutorial-0.6.x/target/scala-2.11/scala-js-tutorial-fastopt.js:2005 (in function "$m_Ltutorial_webapp_TutorialApp$")

  /tmp/phantomjs-launcher8416853343047081941.js:13
  file:///tmp/phantomjs-launcher-webpage6476048362931659173.html:9541

  /tmp/phantomjs-launcher8416853343047081941.js:13
org.scalajs.jsenv.ExternalJSEnv$NonZeroExitException: PhantomJS exited with code 2
    at org.scalajs.jsenv.ExternalJSEnv$AbstractExtRunner.waitForVM(ExternalJSEnv.scala:96)
    at org.scalajs.jsenv.ExternalJSEnv$ExtRunner.run(ExternalJSEnv.scala:143)
    at org.scalajs.sbtplugin.ScalaJSPluginInternal$.org$scalajs$sbtplugin$ScalaJSPluginInternal$$jsRun(ScalaJSPluginInternal.scala:479)
    at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$45$$anonfun$apply$27$$anonfun$apply$28.apply(ScalaJSPluginInternal.scala:539)
    at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$45$$anonfun$apply$27$$anonfun$apply$28.apply(ScalaJSPluginInternal.scala:533)
    at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) org.scalajs.jsenv.ExternalJSEnv$NonZeroExitException: PhantomJS exited with code 2
[error] Total time: 51 s, completed Jan 13, 2016 9:08:34 PM

Also the sbt test will fail with same error message: TypeError: undefined is not an object (evaluating '$g["log4javascript"]["getLogger"]')

If I comment out following line in build.sbt

//scalaJSStage in Global := FastOptStage // If NOT commented out: Uses Phantom, if IS commented out: Uses Rhino

then I get following result from sbt run:

> run
[info] Running tutorial.webapp.TutorialApp
Before getLogger...
After getLogger...
[success] Total time: 4 s, completed Jan 13, 2016 9:16:39 PM

And the script works also on browser, when file scalajs-tutorial-fastopt.html is loaded, button appears and when clicked there is new "You clicked the button" text and "Button clicked..." on console. Also sbt test succeeds.

Phantom version is 2.0.1-development.

What to do to get the code working with Phantom.js?

EDITED: I downgraded Phantom to version 2.0.0 but the error messages remain the same.

user4955663
  • 1,039
  • 2
  • 13
  • 21

1 Answers1

2

I also ran into this issue, following the same tutorial. It turns out that the root cause of the issue was that within the log4javascript code, there are strings with embedded html (with more javascript inside). I have no idea what the reasoning behind that might have been... Anyway, the PhantomJS test runner works by just spitting out all your js code and dependencies together into a html file (within a script tag); when the javascript interpreter sees an unescaped </script> tag, it errors out. (The way I found this out, incidentally, was by finding the phantomjs-launcher-*.html page in my temporary files and opening it in Chrome).

Anyway, after looking at the source for log4javascript and noticing that it's hosted on sourceforge and hasn't been updated for about 9 years (and the source control is CVS...), we decided to go with a different logging framework implementation, as it was important for us to be able to run our tests in PhantomJS.

Very frustrating, as you kind of expect a logging framework to be one of those things that "just works".

frankiesaurus
  • 86
  • 1
  • 5
  • This is good to hear about PhantomJS. Which logging framework did you select and did you manage to get it working with PhantomJS? – user4955663 Jan 18 '16 at 22:46
  • I went with [Log4js](https://github.com/stritti/log4js) in the end, as it seems to be quite a similar concept to log4javascript. There was some work involved in packaging it into a webjar (which you may not be interested in), and in writing a ScalaJS facade for it, which turned out to be relatively straightforward, but I got it working in the end. For a quick fix, if you're not that bothered about logging for now you could just remove log4javascript from your project. – frankiesaurus Jan 20 '16 at 09:38
  • Actually I would like to know what did you have to do to get it working with Phantom. How did you package it? I also looked stritti's log4js briefly but noticed that web site documentation and the actual code matched poorly and there was no "packaged" js file in the zip. I also found another log4js for node: https://github.com/nomiddlename/log4js-node. It is also based on stritti's work but now separated. – user4955663 Jan 22 '16 at 12:13