dackdive's blog

新米webエンジニアによる技術ブログ。JavaScript(React), Salesforce, Python など

Lightningコンポーネントでのログの出力方法

Trailhead の最後に出てきたので、要点のみメモ。

https://developer.salesforce.com/trailhead/lightning_components/lightning_components_debug

f:id:dackdive:20150330203857p:plain

  • $A.log()PRODPRODDEBUG モードでは 何も出力しない
  • $A.log() 使わなくても 普通に console.log() 使える
  • PRODRPODDEBUG モードで $A.log() を使いたい場合は以下のようにするらしい
({
    sampleControllerAction: function(cmp) {
        // subscribe to severity levels
        $A.logger.subscribe("INFO", logCustom);
        // Following subscriptions not exercised here but shown for completeness
        //$A.logger.subscribe("WARNING", logCustom);
        //$A.logger.subscribe("ASSERT", logCustom);
        //$A.logger.subscribe("ERROR", logCustom);

        $A.log("log one arg");
        $A.log("log two args", {message: "drat and double drat"});

        function logCustom(level, message, error) {
            console.log(getTimestamp(), "logCustom: ", arguments); 
        }

        function getTimestamp() {
            return new Date().toJSON();
        }
    }

})