相当于 PHP error_log 的信息日志?
我使用 error_log
进行日志记录,但我意识到必须有一种更惯用的方法来记录应用程序进度。有info_log
吗?或同等水平?
I use error_log
for my logging, but I'm realizing that there must be a more idiomatic way to log application progress. is there an info_log
? or equivalent ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
error_log
附加到指定文件。请注意,您需要具有 3(消息类型)才能附加到给定文件。
您可以在脚本中尽早创建一个函数来包装此功能:
You can use
error_log
to append to a specified file.Note that you need to have the 3 (message type) in order to append to the given file.
You can create a function early on in your script to wrap this functionality:
相当于 syslog() 和
LOG_INFO
常量:如果你想使用一个文件(这不是一个好主意,因为没有日志轮换并且可能会因为并发而失败),你可以这样做:
The equivalent is syslog() with the
LOG_INFO
constant:If you want to use a file (not a good idea because there is no log rotation and it can fail because of concurrency), you can do:
我建议你使用 Monolog:
https://github.com/Seldaek/monolog
可以通过composer添加依赖:
然后就可以初始化一个文件流日志,对于一个名为
your-app-name
的应用程序,到文件path/to/your.log
中,如下:Monolog非常强大,你可以实现多种类型的处理程序,格式化器和处理器。值得一看:
https://github.com/Seldaek/ monolog/blob/master/doc/02-handlers-formatters-processors.md
最后这样称呼它:
为了使其全局化,我建议您将其添加到您的依赖项中注入器(如果有的话),或者使用带有静态调用的单例。
如果您想了解更多详细信息,请告诉我。
I would recommend you to use Monolog:
https://github.com/Seldaek/monolog
You can add the dependency by composer:
Then you can initialize a file streaming log, for an app called
your-app-name
, into the filepath/to/your.log
, as follow:Monolog is very powerful, you can implement many types of handlers, formatters, and processors. It worth having a look:
https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md
And finally call it like:
In order to make it global, I recommend you to add it to your dependency injector, if you have one, or use a singleton with static calls.
Let me know if you want more details about this.