How to remove logging calls

617 views
Skip to first unread message

Zeno

unread,
Feb 6, 2012, 4:46:39 AM2/6/12
to Closure Compiler Discuss
What is the best way to remove all calls to goog.debug.Logger
(logger.log(), loggger.info(), logger.warn(), etc) from the compiled
code?

I found an old thread here explaining how to do that by rebuilding the
compiler with some hidden options (stripNameSuffixes and
stripTypePrefixes), but I'm wondering if this has now been exposed to
the commandline for easier access, or if there's a better/official way
to achieve this - especially because this looks like a pretty basic/
standard requirement for any production code.

Jan

unread,
Feb 7, 2012, 4:29:03 AM2/7/12
to Closure Compiler Discuss
Hi, use these two files for your custom compiler: The java
implementation and the ant build.xml file


--------------------------------------------
CommandLineRunnerImpl.java------------------------------------------------------
package org.example.closure;

/**
 * Created by IntelliJ IDEA.
 * Date: 4/13/11
 * Time: 9:54 AM
 *
 *
 * The CommandLineRunner from the Google-Closure Compiler parses the
command-
 * line
 */
import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.CommandLineRunner;
import com.google.javascript.jscomp.CompilerOptions;
import java.lang.Override;


public class CommandLineRunnerImpl extends CommandLineRunner {
    protected CommandLineRunnerImpl(String[] args) {
        //super class parses command line
        super(args);
    }

    @Override
    protected CompilerOptions createOptions() {
        CompilerOptions options = super.createOptions();

        //strip Logger from Javascript. Be aware of b'logger', etc.
        options.stripTypePrefixes = ImmutableSet.of("goog.debug",
"goog.asserts", "window.console");
        options.stripNameSuffixes = ImmutableSet.of("logger",
"logger_", "window.console.log");
        return options;
    }

    public static void main(String[] args) {
        CommandLineRunnerImpl runner = new
CommandLineRunnerImpl(args);
        if (runner.shouldRunCompiler()) {
            runner.run();
        } else {
            System.exit(-1);
        }
    }
}

--------------------------------------------
build.xml------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<project name="mycustomclosurecompiler" default="build">
<property name="src.dir" value="${basedir}/src" />
<property name="build.dir" value="${basedir}/build" />
<property name="classes.dir" value="${basedir}/classes" />
<property name="closure-compiler.jar" value="../closure-compiler/
build/compiler.jar" />

<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${classes.dir}" />
</target>

<target name="compile">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}"
classpath="${closure-compiler.jar}"
destdir="${classes.dir}"
includeantruntime="true" />
</target>

<target name="MyCustomClosureCompiler" depends="compile">
<jar destfile="${build.dir}/MyCustomClosureCompiler.jar">
<zipfileset src="${closure-compiler.jar}" excludes="META-
INF/**" />
<fileset dir="${classes.dir}" />
<manifest>
<attribute name="Main-Class"

value="org.example.closure.CommandLineRunnerImpl" />
</manifest>
</jar>
</target>
<target name="build" depends="MyCustomClosureCompiler" />
</project>

------------------------------------------------------------------------------------------------------

e.g.:
Folder: "compilers" should contain both the svn checkout of the
GoogleClosureCompiler (foldername: 'closure-compiler) and a folder for
your custom implementation: 'mycustomclosurecompiler'


.
..
'compilers'
.
..
'closure-compiler'
'mycustomclosurecompiler'



Greets,

Jan

Jan

unread,
Feb 7, 2012, 4:28:44 AM2/7/12
to Closure Compiler Discuss

thanpolas

unread,
May 30, 2012, 3:40:47 PM5/30/12
to closure-comp...@googlegroups.com
I have compiled my own compiler.jar file, using a combination of Jan's script, as well as reading the 3 year old relative thread...

In a plainly simple js script, the logging calls and goog.debug scripts were indeed removed from the resulting compiled file...

However the goog.debug dependencies remained there even though they were never called / used.

So to sum up, in my boilerplate script i had:

goog.require('goog.debug');
goog.require('goog.debug.FancyWindow');
goog.require('goog.debug.Logger');
goog.require('goog.debug.LogManager');

  var debugWindow = new goog.debug.FancyWindow('main');
  debugWindow.setEnabled(true);
  debugWindow.init();
  
  var logger = goog.debug.Logger.getLogger('example.sample2.doStuff');
  logger.info('Hello World!');  


Resulting compiled code size with no stripping: 60,089 bytes
Resulting compiled code size with custom compiler.jar stripping logger.info() and goog.debug.*: 41,408 bytes

Use of --define='goog.DEBUG=false' and relevant if clauses had no effect over the custom compiler.jar resulting size...

Resulting compiled code size with all debugging commented out: 4,809 bytes

I am now directed towards removing all the requirements for goog.debug.*, using a shell script, before i run the custom compiler...

Overall, not too happy with my options...
Reply all
Reply to author
Forward
0 new messages