开发者

Is there a way to toggle code in and out of live code for testing?

I have a coldfusion website, and at one point we put in a log - whenever a certain type of code was run, that code was logged. Now we'd like to take it out, but still have the option of putting it back in, so we're planning on finding every time (there are probably hundreds) this code was logged and commenting out the logging code. This seems quite tedious to me and I was wondering if there's some sort of way to mark code as testing and automatically enable/disable it by changing the settings in one place开发者_JAVA技巧... I think I've seen functionality like this in other languages. Is this possible in ColdFusion?


The way I would handle this is by configuring some sort of global application variable (e.g. doLogging) that you can switch on and off and check when needed. If it is set to true then log, otherwise don't.

For example in Application.cfc you could set up this variable:

<cfcomponent displayname="Application" output="false">


 <cfset APPLICATION.doLogging= false />

 <cffunction name="onRequestStart" access="public" output="false"> 

  <cfif structKeyExists(URL, "toggleLogging")>
   <cfif APPLICATION.doLogging>
    <cfset APPLICATION.doLogging = false />
   <cfelse>
    <cfset APPLICATION.doLogging = true />
   </cfif>

  </cfif>

 </cffunction>

</cfcomponent>

And then check for it where needed in your code:

<cfif APPLICATION.doLogging>
    <cflog file="foo" text="bar" />
</cfif> 

You will have to change your existing code though to use this new method, but managing it in future would be easier.

Hope that helps!

EDIT: changed variable names to be more sensible and added an example of use.


Investigate cftrace, which generates its own log, and is turned on and off from the ColdFusion admin. When turned off, ColdFusion ignores the tag.


You can use the IsDebugMode() function which will return true if you have ColdFusion debugging on and false if it is off.


If you have ColdFusion 9.0.1 and you used custom logs, you can stop logging into those logs by pressing (stop) button in Administrator. Apart from that, I don't think there some debug/production option which could disable those when deployed. You could do massive search/replace with regexp in Eclipse and wrap cflog tag with cfif asking is it debug mode or production mode, and that you can set in application scope on app start.

cflog as far as I know uses log4j in background, possibly there's some hidden way to use log4j's functionalities from cfml.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜