njuptxiaot / json-template

Automatically exported from code.google.com/p/json-template
0 stars 0 forks source link

configurable log function (javascript) #18

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
json-template Javascript defines a 'log' function and makes it be the
system 'log' function if it exists, otherwise a function that does nothing.
This clobbers the system 'log' function.

It's sometimes nice to be able to turn off logging in json-template
entirely - an application that *uses* json-template may want to do its own
logging and having the output from json-template appear in for instance
firebug is rather distracting.

The simplest way (though somewhat ugly as we need to modify the source
code) is to go into json-template.js and do:

var log = function() {};

but since you redefine the system 'log' here, *all* logging in any
javascript code is disabled.

If instead we'd use a log function with a different name (say, _log) in
json-template code, we could globally disable json-template logging with a
single edit.

I'd be nicer still if we could somehow configure json-template to log or
not log, and configure which logging function to use. An essential first
step in accomplishing that is still not clobbering the system 'log', though.

Original issue reported on code.google.com by martijn....@gmail.com on 20 Apr 2009 at 12:56

GoogleCodeExporter commented 8 years ago
log isn't defined in json-template.js.  It's only defined in test-helpers.js.  
Are
you using an old version?

Original comment by gtempacc...@yahoo.com on 20 Apr 2009 at 7:01

GoogleCodeExporter commented 8 years ago
I refer to you to line 23 on the current trunk.

To quote it:

// This is predefined in tests, shouldn't be defined anywhere else.  TODO: Do
// something nicer.
var log = log || function() {};

Original comment by martijn....@gmail.com on 20 Apr 2009 at 9:01

GoogleCodeExporter commented 8 years ago
Oh, perhaps you don't consider this a definition of 'log'. It's just 
frustrating if
you want to change this and make log messages from json-template somewhere 
else, as
you'd write:

var log = function() {};

which would result in logs being turned off for *everything*.

Any log configuration system that json-template adopts to change whether you 
want to
log or not will need to modify the behavior of log, if you refer to it under 
this
global name.

Original comment by martijn....@gmail.com on 20 Apr 2009 at 9:05

GoogleCodeExporter commented 8 years ago
It doesn't "clobber" the log function -- it uses the one that's there if it 
*already*
exists.

I agree that customizing the log function would be useful... if you have a 
scheme
that works for you please send in a patch.

Original comment by gtempacc...@yahoo.com on 20 Apr 2009 at 9:15

GoogleCodeExporter commented 8 years ago
It does write to 'log', just will write the same value back.  I'm
fine to retract the word 'clobber'. :)

Whatever it does, this approach makes it impossible to do something
*else* with that line like shut down json-template's voluminous
logging (while being interested in other logging by other js code), as the 
global name 'log' is in use throughout the json-template code. Perhaps there's 
some other way to selectively shut down logging from particular javascript 
modules?

In our copy of json-template we now renamed the uses of log to log2 so
we can shut down logging in json-template specifically when we want
to. That's hardly ideal. I'll look into a approach that does work
where json-template uses _log instead, adding an api to manipulate the logging 
situation.

Original comment by martijn....@gmail.com on 21 Apr 2009 at 7:56

GoogleCodeExporter commented 8 years ago

Original comment by gtempacc...@yahoo.com on 21 Apr 2009 at 4:51

GoogleCodeExporter commented 8 years ago
I recommend dropping the globally-visible logging hook in favor of a 
locally-scoped
one.  So, define a "log()" function on one of the json-template classes, and 
provide
examples of how the user can attach it to the global "log()" function or a
user-defined function.  By default, leave this local "log()" function defined 
as a
no-op for best performance.

Original comment by Robert.David.Williams on 14 May 2009 at 7:50

GoogleCodeExporter commented 8 years ago
Yes, I agree with that approach.  log() shouldn't be in the global namespace.

Original comment by gtempacc...@yahoo.com on 15 May 2009 at 5:52