有没有办法在实时代码中切换代码以进行测试?

发布于 2024-10-09 09:49:52 字数 214 浏览 0 评论 0原文

我有一个 Coldfusion 网站,有时我们会写入日志 - 每当运行某种类型的代码时,就会记录该代码。现在我们想将其取出,但仍然可以选择将其放回原处,因此我们计划每次(可能有数百个)记录此代码时查找并注释掉记录代码。这对我来说似乎很乏味,我想知道是否有某种方法可以将代码标记为测试并通过更改一个地方的设置来自动启用/禁用它......我想我已经在其他语言中看到过类似的功能。这在 ColdFusion 中可能吗?

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... I think I've seen functionality like this in other languages. Is this possible in ColdFusion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘和柠 2024-10-16 09:49:52

我处理这个问题的方法是配置某种全局应用程序变量(例如doLogging),您可以在需要时打开和关闭并进行检查。如果它设置为 true 则记录,否则不记录。

例如,在 Application.cfc 中,您可以设置此变量:

<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>

然后在代码中需要的地方检查它:

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

尽管要使用此新方法,您将必须更改现有代码,但要在未来会更容易。

希望有帮助!

编辑:将变量名称更改为更合理,并添加了使用示例。

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.

昵称有卵用 2024-10-16 09:49:52

调查 cftrace,它生成自己的日志,并且是从 ColdFusion 管理员打开和关闭。关闭时,ColdFusion 会忽略该标签。

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

梦亿 2024-10-16 09:49:52

您可以使用 IsDebugMode() 函数,如果您打开 ColdFusion 调试,该函数将返回 true,如果关闭,则返回 false。

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

扛刀软妹 2024-10-16 09:49:52

如果您有 ColdFusion 9.0.1 并且使用了自定义日志,您可以通过按管理员中的(停止)按钮来停止登录这些日志。除此之外,我认为没有一些调试/生产选项可以在部署时禁用它们。您可以在 Eclipse 中使用正则表达式进行大量搜索/替换,并使用 cfif 包装 cflog 标记,询问它是调试模式还是生产模式,并且您可以在应用程序启动时在应用程序范围中进行设置。

据我所知,cflog 在后台使用 log4j,可能有一些隐藏的方法可以从 cfml 使用 log4j 的功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文