ClojureScript

logging via goog.log not visible for optimizations other than none

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 1.10.238
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Environment:
    node: v10.10.0
    clj: 1.9.0.391
    cljs: v1.10.338 (and sha "6eedd0a08c49f7b0d4dcb30977b2fb38c90577bd")

Description

steps to reproduce

Given

deps.edn
{:paths
 ["."]

 :deps
 {org.clojure/clojurescript {:mvn/version "1.10.339"
                             #_#_:git/url "https://github.com/clojure/clojurescript.git"
                             #_#_:sha "6eedd0a08c49f7b0d4dcb30977b2fb38c90577bd"}}}
foo.cljs
(ns foo
  (:require [goog.log :as glog])
  (:import goog.debug.Console))

(def logger
  (glog/getLogger "app"))

(def console (goog.debug.Console.))

(defn workaround! []
  (.setConsole goog.debug.Console js/console))


(defn -main [& args]
  (.setCapturing console true)
  (.setLevel logger goog.debug.Logger.Level.FINEST)

  (when args
    (workaround!))
  (glog/error logger "some error"))

(set! *main-cli-fn* -main)

When I compile with optimization `none` and run, logging does appear:

$ clj -m cljs.main -v -O none -t node -c foo
$ node out/main.js
 [  0.002s] [app] some error

When I compile with optimization `simple` (or `advanced`) and run, logging does not appear.

Expected: optimization simple should have no effect on appearance of logging on Node (just like when compiling for the browser).

various (FWIW)

With simple, `goog.debug.Console.console_` ends up being nil as `goog.global['console']` is nil (https://github.com/google/closure-library/blob/master/closure/goog/debug/console.js#L169).

`println` works in both none and simple.

When targetting browser both none and simple show logging.

A workaround is to setConsole: `(.setConsole goog.debug.Console js/console)`

Activity

Hide
Thomas Heller added a comment -

FWIW the problem in node is that goog.global is not assigned correctly.

In goog/base.js it is assigned to goog.global = this; but this in node it the current Module instance and not the usual window. module.console does not exist so is not assigned correctly. This works differently in development since the nodejs bootstrap explicitly loads the code with global as this [1]. This will not be done in production so the "default" this becomes the module and breaks optimizied code.

I think its fine to just always use the workaround! since it doesn't break anything.

[1] https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/bootstrap_nodejs.js#L114

Show
Thomas Heller added a comment - FWIW the problem in node is that goog.global is not assigned correctly. In goog/base.js it is assigned to goog.global = this; but this in node it the current Module instance and not the usual window. module.console does not exist so is not assigned correctly. This works differently in development since the nodejs bootstrap explicitly loads the code with global as this [1]. This will not be done in production so the "default" this becomes the module and breaks optimizied code. I think its fine to just always use the workaround! since it doesn't break anything. [1] https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/bootstrap_nodejs.js#L114

People

  • Assignee:
    Unassigned
    Reporter:
    Gert
Vote (0)
Watch (0)

Dates

  • Created:
    Updated: