0

Coming from a Java background, I'm used to logging frameworks like log4j, logback and slf4j. However, when doing PHP programming, I find my self using echo and var_dump/print_r and write logging information to (usually) the web pages I'm working with. I'm thinking that there must be a better way to do this in PHP as well? So what is the idiomatic way to do PHP logging?

I've come across log4php, and some places suggest to use error_log also for general logging purposes. What complicates matter further is that the PHP code I write usually are modules in a larger system, so multiple modules may run within the same request, and logging should preferably be isolated per module.

Any input and suggestions are appreciated.

NilsH
  • 13,705
  • 4
  • 41
  • 59
  • https://github.com/Seldaek/monolog is nice. – lsouza Aug 09 '13 at 18:10
  • Seems like it requires PHP 5.3. That's a showstopper. It need to work with 5.2 and up. – NilsH Aug 09 '13 at 18:12
  • Ouch... You should really get to upgrading PHP, at least to 5.4, 5.5 if possible. – Deniz Zoeteman Aug 09 '13 at 18:17
  • Take a look at https://github.com/katzgrau/KLogger – Hashem Qolami Aug 09 '13 at 18:18
  • @gdscei Sure, but I'm creating modules that are available for others to download. And the minimum requirement for the system I'm creating modules for are PHP 5.2, so it's not up to me :) – NilsH Aug 09 '13 at 18:18
  • There is no idiomatic way – Gordon Aug 09 '13 at 18:22
  • I see. That is an answer as well :) But I assume that there are ways that are more widely used than others? – NilsH Aug 09 '13 at 18:25
  • 1
    Hmm, no. The major Frameworks agreed to use https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md but adoption of that is not at a level that I'd call common (even among those frameworks). The aforementioned Monolog is quite popular atm. But since you need PHP < 5.2 support that's not an option. So just pick what you feel comfortable with and use that consistently. – Gordon Aug 09 '13 at 19:40

1 Answers1

1

I've been a long time user of Log4PHP, and I can highly recommend it. :)

But the question about doing proper logging is rather difficult. I use Log4PHP in an enterprise environment where there are multiple applications that all use the same logging, and they also assume that the logger is correctly configured and ready to use. So it's basically a very easy "I assume the same logging is available everywhere" situation.

Developing modules for others to use is a completely different task. You could possibly use your own internal logging, but where do you write those log messages to? It can be considered bad behavior if your module unexpectedly fills the hard drive. It would also be a requirement for your module to log to that target that the rest of the application is logging to.

If your module is small enough, logging might not be a really big issue, because you would be able to communicate any error situations by other means (like throwing exceptions or returning false and offer a dedicated error info function).

There is a definition of a common logger interface that all logging frameworks might eventually implement: PSR-3 But two drawbacks will make this an unavailable solution for you: 1. Log4PHP does not currently implement that interface, and 2. it uses namespaces and so requires PHP 5.3 at least.

The general thought behind it might help you, though: Your module should offer a way to accept a logger that is passed into it from the outside world. If there is no logger, it should not log. And because you might want to test your logging, there should be wrapper that present a uniform interface to your modules and encapsulate the nasty implementation differences of any loggers. Start offering a PSR-3-Wrapper and a Log4PHP wrapper for your modules and react to public demand for other log requirements.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • Thank you. That is an excellent reply! If there were a "standard" logging mechanism used in the system, I would certainly "hook" into it. But as it is not, I have to consider other alternatives. The logging in question is mainly debug logging to trace error situations, and a way for users of the modules to report back these traces (result from called web services etc) to pinpoint the error. Maybe it is better to `die` or return false, but then I would lose a lot of contextual information. But then again, it might just be me that is inexperienced with PHP, and this is possible otherwise. – NilsH Aug 09 '13 at 18:41
  • If it was just me using the modules, it wouldn't be a problem. But the main goal is to make it possible to collect logs/traces from the module users when they experience problems. – NilsH Aug 09 '13 at 18:41
  • Inexperienced users probably wouldn't be able to find the log files anyway. But in such a case you'd probably need a simple "instantly switch on" solution for logging to get background information. – Sven Aug 09 '13 at 18:52
  • Yes, that is what I am seeking. – NilsH Aug 09 '13 at 19:48
  • So the requirements for your module logging are: 1. Default off, 2. Instantly on with some usable output, 3. Pluggable into existing log structures. 1 and 3 are like I described, and 2 would be part of your modules configuration. Also think about logging with different log levels (debug, info, warning, error, fatal)... – Sven Aug 09 '13 at 19:52
  • More or less. As nr 3. doesn't really exist, it's not that much of concern. But there is the requirement of isolation between modules. – NilsH Aug 09 '13 at 19:54