Logging is one of the easy ways to generate a record of what your application is doing and to provide information in the case of a failure. CakePHP includes an easy to configure and use the logging tool to generate log files, no matter the use case.
- View Offline
- Logging is a pretty common action that we probably need to do more than a few times in our application. We want to log when, say a user changes their password, or when a transaction is recorded if we are an e-commerce application. Let's build in some basic logging into our application, when a user changes their password. So if we need to store a record of this, we can. To begin, let's start by opening up our User Entity Class, which is located at Source, Model, Entity, User.php.
If you recall, we talked about the magic getter and setter methods for the entity objects, where we can have functions called on getting or setting a value to our entity. The magic setter method means that we can record every time a user attempts to change their password. To do this, we're going to scroll down, and add a few blank lines after line 38, and add a method call for protected function _setPassword. Now the setter method takes as a parameter the value of the field being set, so add a parameter of password.
And then we want to log the fact that the password is being set. To do this, we want to use the log trait that CakePHP provides. What is a trait? A trait is a class that allows for horizontal code sharing, as opposed to vertical code sharing, like you have with a normal class inheritance. A trait basically imports a method, or a series of methods, into a class level name space. So the method can be called directly inside of the class. Think of it like basically saying, "Here, include these "couple of methods in this class." That's a trait.
There's a logging trait that CakePHP provides that we can use here. First, we need to import this clause (unintelligible) statements, so let's scroll up to the top, and add a line before line 4 that reads as use Cake\Log\LogTrait. And then on line 21, we can add the code use LogTrait. This is how you import a trait, and then use it in your class.
And now we can use the methods the log trait provides, which is a single log method. So in our set password method, on line 43, we can call this arrow log, and pass a string for what we want to log. In this case, we'll log User changing password. The second parameter to the log method is the log level, in this case we're going to pass it as the string debug. There's a third parameter here, which is a context parameter.
Context essentially allows you to write additional metadata about the log message. Including siloing messages related to a particular action type to different log files. So you could log all your transaction messages to a different series of logs, versus your standard log messages. OK, what now? Well, the magic set methods need to return the value being set. So in this case, we need to return the password. So on line 44, we'll add return password. And now, let's see this in action.
Remembering to start your CakePHP server, navigate to the users index page. Let's click on Editing a User, in this case I'm going to edit Robert, and I want to change their password. In this case, I'm going to change their password to "password," and submit the form. We'll see here that their password has been changed from "testing" to "password." And if I go back to my code editor, and look in the logs directory, and the debug.log file, and scroll all the way to the bottom, I should see a line that says Debug: User changing password.
You could easily update this to record for instance, the user's ID and some other information to create a trackable record of when a user is changing their password. Or use this pattern to keep logs of any sort of information. There are many more features CakePHP logging has available if you need to do something more complex. You can look in the CakePHP book for more information.
CakePHP is a critical framework for PHP developers. It helps them build complex web applications faster and more efficiently. If you want to use CakePHP 3 (the latest version of the framework) in your own development workflow, this is the place to start.
Justin Yost provides an overview of the underlying MVC pattern in CakePHP, and the installation and configuration process for Mac and Windows. He shows how to use the CakePHP shell to build your first basic CakePHP app, and then discusses each application element in depth: controllers, models, views, components, behaviors, helpers, and utilities. At each step, he discusses the relevant new features and enhancements in CakePHP 3, including new components; performance, session management, and ORM improvements; and localization.
In later chapters, the course gets a little more advanced. Watch these tutorials to learn how to send email with CakePHP, extend CakePHP with plugins, and write unit tests to identify and eliminate bugs in your code. Justin also shows how to add security to your CakePHP apps with a basic user authentication system.
- Installing and configuring CakePHP
- Using the CakePHP shell console
- Creating CakePHP controllers
- Saving data in a CakePHP model
- Finding and deleting data
- Working with entity methods
- Creating CakePHP views
- Using and customizing components to share functionality between controllers
- Creating behaviors
- Formatting data with helpers
- Developing faster with CakePHP utilities: hash, collections, and logging
- Sending CakePHP email
- Creating a custom plugin
- Testing CakePHP applications
- Authorizing users of CakePHP applications