Yii2 and Log4PHP

I’m new to the Yii2 framework, coming from CI (and new to Composer), it has a steep learning curve.

I was wondering how you replace Yii2 logging with Log4PHP. I have it installed with Composer, but I don’t know how to use it in Yii2/replace Yii2 logging.

Do you need Log4PHP for something specific, or just because you’re comfortable with it? From what I saw, the latest release for that was late 2012 …

I would highly recommend just using Yii2’s built-in logging instead:


You can implement a custom log target. Check the guide on logging.

The issue is not the log targets. Also the last update for Log4PHP is of little consequence when something runs very well and doesn’t need to be updated.

Log4PHP is more advance, it has appenders and loggers. From what I’ve looked at Yii2 does not. So I can’t create an appender and tell the logger to use it, or what the log level is for that logger. The log level is applied globally. If I set file logging to use error and info anything that has logging of error and info will be logged. Which is fine and all, but in our current project we have some loggers that are at ERROR and some that are at DEBUG, but in both cases they have info methods being called that’s not logged, because we don’t need it most of the time. However, the info logging is left in there so that ERROR can be bumped up to get those info logs if necessary.

Moreover, Yii2 creates a new file at 10MB. I don’t see an option to change the file size, compress the rollover files, change log formatting (or add mapped diagnostic constant), or even change the file name. Though I think the last one would be something you guys take into consideration because it’s obvious, I just don’t see it. Yii2’s logging is basically a slimmed down version of Log4PHP from what I can tell.

I do like that Yii2 stays relatively fast as the file size expands. Something CI’s default logging doesn’t do, the larger the file size the slower CI gets because it does a seek() to the end of the file.

Overall I give Yii2 a 4 out of 5, it just has a steep learning curve and not everything is intuitive or clear.

  • Log4PHP appenders is the same concept as Yii targets.

  • Log4PHP loggers are a bit different but similar result could be achieved by using distinct categories.

You can define a log target and configure it to log certain levels (via "levels") and certain categories (via "categories" and "except") only.

  • File size is configured via "maxFileSize".

  • There’s no option to compress rollover files. That could be implemented so please submit an issue to Yii’s issue tracker.

  • Format could be adjusted via overriding “formatMessage()” but I agree it’s not convenient so that deserves a separate issue as well.

  • File name can be changed via "logFile".

Well thanks for the info! I’ll go ahead and file those two issues soon.

Support here is pretty nice, thanks. :)