So let's continue on the topic of error handling.
So previously in this schema load script, we have thrown this exception explicitly outside the main
logic of the script so that we can see how the errors look like in the terminal in the command line
interface.
So obviously this should not stay here.
But what we can do additionally is in our scripts we can just skip wrapping everything inside.
Try catch blocks.
It probably would simplify our life quite a lot.
So now if the exception is thrown, the execution will just stop.
Nothing else will happen, and you're gonna see an error in the terminal wherever you run this command.
But there is another thing.
So let's go back to the error handler.
We've got a note here that we should additionally log the error into a file so we can review that later,
or maybe someone else can review it later to figure out what has happened.
So the first thing to do here in this handle exception method is to log the error.
We should be calling another method for that.
We can call that log error and we are just passing an exception to it.
So this obviously does not exist.
We need to add it.
This method will be private.
So let me add it somewhere on the bottom.
That's private static function log error.
It accepts a throwable type and it's void doesn't return anything.
And I'd like it to be pretty simple.
We should just format the error message sensible way without any colors whatsoever.
Because in the log files you don't have colors.
This is to be consumed by graphical user interfaces that let you present logs.
There is software that does exactly this, or it needs to be consumed manually by someone reading through
the file.
That's why we should just log this as simple as possible.
But we can reuse some of the code.
Maybe.
Let's begin with this message.
So the first thing is to generate the log message.
And as I've said we don't need colors.
That's why we need to strip this part that turns everything into red.
And that part that resets the color.
So just the message is enough.
So we can use an error log function to store this error.
The first argument is the message we've got the log message variable.
The next one is the message type.
This says where the error should go.
And in the PHP documentation page you can see what are the possible values for it.
I'm just going to say it right away that if you type free, this means please store it in a specific
destination, which is a file.
Now in our case we need to point to this file.
So I'm using the current directory which is inside the core directory.
And I need to move up.
So I'm going one directory up to the logs directory.
Error log.
Our job would be to create this file.
Otherwise we might get an error.
and you add a semicolon.
And at this point I need to create this logs directory.
And inside it I am going to create an empty error log file.
Next time anything goes wrong in our app, whether that's just a notice or an error, we should get
an entry inside this file.
This would be very useful to later review it and see what are the potential problems in our app.
Even if they are not straight on errors, maybe there are notices.
That's something you should always try to fix.
Maybe I'm just going to go back to our command and throw this exception again.
This would be throw new exception saying this should go into the logs.
All right.
At this point, let's run this command.
So again, we just throw an exception here.
Nothing new.
But now let's take a look at the log file.
And this is how it looks like.
All the new errors would be appended to this file with the date, with the time, and with the type
of exception that has happened message and all the other info.
This is super useful to review what has happened in your app, and what were the errors that maybe happened
previously.
Sometimes it just happens that you just figure out the recent issue in your application, and you just
need to go back to see when it started, or maybe what change has caused this issue.
So log files are super useful.
Okay, now I think we should clean up and remove this error.