Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by
h2. steps to reproduce



 {org.clojure/clojurescript {:mvn/version "1.10.339"
                             #_#_:git/url "https://github.com/clojure/clojurescript.git"
                             #_#_:sha "6eedd0a08c49f7b0d4dcb30977b2fb38c90577bd"}}}

(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
  (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).

h2. 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)`

2 Answers

0 votes

Comment made by: thheller

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}} (link: 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.

(link: 1) https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/bootstrap_nodejs.js#L114

0 votes
Reference: https://clojure.atlassian.net/browse/CLJS-2930 (reported by alex+import)