Zend Framework 中 Zend_Log 的相对路径

发布于 2024-10-01 22:14:41 字数 747 浏览 5 评论 0原文

我正在尝试找出配置 Zend_Log_Writer_Stream 实例以写入相对于我的 APPLICATION_PATH 的文件名的最佳方法。

例如,使用以下配置:

resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = "logs/production.log"

初始化记录器的正常方法是使用应用程序引导资源或执行以下操作:

$logger = Zend_Log::factory($config->resources->log);

这样做的问题是 Zend_Log_Writer_Stream::factory 方法将尝试访问相对于当前脚本执行,而不是 APPLICATION_PATH。

因为它几乎总是 /public 内的 index.php 启动,通常这不是一场戏剧,但是当我在单元测试目录中执行单个脚本时,它将使用该目录作为路径的基础。

理想情况下,我希望能够设置:

resources.log.stream.writerParams.stream = APPLICATION_PATH . "../logs/production.log"

以便它始终使用可预测的位置。我宁愿不必破解 Zend_Log 继承类的工厂方法来完成这项工作。

我很想听听其他人如何解决这个问题。

I am trying to work out the best way to configure my Zend_Log_Writer_Stream instance to write to a filename relative to my APPLICATION_PATH.

For example, with the following config:

resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = "logs/production.log"

The normal way to init your loggger would be to either use the application bootstrap resource or do the following:

$logger = Zend_Log::factory($config->resources->log);

The problem with this is that the Zend_Log_Writer_Stream::factory method will try to access the file relative to the current script execution, not the APPLICATION_PATH.

As it's almost always the index.php inside /public that kicks this off usually it's not a drama, however when I execute individual scripts inside my unit testing directories it will use that directory to base the path on.

Ideally I want to be able to set:

resources.log.stream.writerParams.stream = APPLICATION_PATH . "../logs/production.log"

So that it always uses a predictable location. I would rather not have to hack the factory method of a Zend_Log inherited class to make this work.

I would love to hear how others have solved this problem.

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

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

发布评论

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

评论(2

╄→承喏 2024-10-08 22:14:41
resources.log.stream.writerParams.stream = APPLICATION_PATH "../logs/production.log"

应该有效。 parse_ini_file() (由 Zend_Config_Ini 使用)允许使用常量 - 但连接不像 PHP 中那样工作(使用 . 运算符);只需将静态字符串附加到常量(如果 APPLICATION_PATH 不以 / 结尾,您可能需要额外的 /)。

resources.log.stream.writerParams.stream = APPLICATION_PATH "../logs/production.log"

should work. parse_ini_file() (which is used by Zend_Config_Ini) allows for the use of constants - but concatenation doesn't work like in PHP (with the . operator); just append the static string to the constant (you might need an additional / if APPLICATION_PATH doesn't end with a /).

柠檬 2024-10-08 22:14:41

Zend_Config_Ini 使用 parse_ini_file 内部读取application.ini。这意味着,它应该解析常量。请尝试您理想的方法。确保在加载 application.ini 之前定义常量

编辑:另请参阅 Stefan Gehrig 关于要使用的正确格式的注释

Zend_Config_Ini uses parse_ini_file internally to read the application.ini. This means, it should parse constants. Please try with your ideal approach. Make sure the constant is defined prior to loading the application.ini

EDIT: Also see Stefan Gehrig's note about the correct format to use

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