Because Laravel is built on top of Symfony , they share the same core logging mechanism—although the configuration differs between the two frameworks.
Logging in Symfony and Laravel are both done using Monolog, a third-party PHP logging library that can be used to create and store logs in a large number of ways. So, what exactly do PHP logs look like? In most instances, PHP logs follow a fairly predictable format:. In a nutshell, the log line above can be broken up into four parts: the date, the hostname, the process, and the error message.
Whenever an error is encountered or an uncaught exception is thrown, the error message is printed along with the date, hostname, and process metadata to help pinpoint what happened, where it happened, and when it happened.
It is important to note that, in PHP, there are a handful of log levels that can be squashed or raised. While these log levels are determined by PHP itself, understanding what they are and mean is a crucial step towards being able to diagnose problems as they happen. This method accepts an integer value that tells PHP which errors to display, and which ones to ignore.
Here are a few of the most common log levels. For more information about log levels there are quite a few of them , take a look at PHP's official documentation. Guide PHP. Monolog is one of the most widely used PHP logging libraries. You can easily differentiate logs within a single application by categorizing them in channels , and you can send your logs to databases, message queues, and external collaboration tools. If you already have Composer installed, all you need to do is issue this command to add Monolog to your project:.
Monolog uses channels to differentiate logs that have been routed to the same destination but that contain data about different categories of events. Each time you create a logger, you need to provide a channel name.
You can create multiple loggers within your application and use each one to log events related to a category of activity, such as purchases or user accounts. Numerous other handlers are available so you can easily send logs to the service of your choice.
For each handler you create, you provide information about how it should route the log e. By pushing multiple handlers onto a logger, you can use it to log different types of events to different destinations. Monolog allows you to define a custom log format, or you can choose an existing formatter to determine how your log messages appear.
Monolog formatters are available to meet different logging requirements, and you can choose the one that best suits your needs. This can make it easy to store multi-line errors in a single log line. You can also store information unique to each session by logging the PHP session array.
To isolate these logs from those created by other loggers in your application, you can use a log management solution to filter your data and view only logs from the transactions channel. Notice that Monolog automatically adds two arrays to this log— context and extra. The context and extra arrays give you options for easily adding metadata to each log. Processors make it easy to include the same information consistently across all the logs created by a single logger.
The following example defines a Monolog processor to include context and extra data:. You can also pass context array data as an argument to the method you use to create the log. The example below illustrates passing the log message and context data in a single call:. Monolog supports eight different log levels —the same ones defined in the syslog protocol —so that each log carries metadata that conveys the severity of the event being logged.
This way, you can log the types of events e. Instead you can configure your application to log events of all levels, and use a log management solution to filter logs downstream to isolate certain kinds of events. Because PHP logging is flexible, you have options in how much to log and how to handle your logs. Centralize and organize your PHP logging for easier analysis with Datadog. Like many other languages, PHP uses exceptions to accommodate unintended behavior by your application.
Exceptions should be caught when they occur—governed by code that addresses the exceptional case. PHP does not automatically log exceptions when they are thrown, so you should create exception handlers that log useful information about the exception.
The code below shows an example of a basic exception handling strategy. The checkUsername function validates the length of the string passed to it, then throws exceptions under certain conditions. The function is called from within a try block, and a catch block handles any exceptions and logs the details.
The exception object contains properties, such as the file and lines of code that have caused the unintended state, that describe the state of the application. Luckily, upon installation, the folders for the log files of these two web servers are already writable.
But the tool goes beyond monitoring the performance of your application. It allows you to do centralize logging , perform code profiling , and stay on top of server and application metrics. One of the great features of Retrace is its ability to automatically collect all unhandled errors in your PHP application. Our PHP extension will catch the exception and forward them automatically to Retrace. With Retrace, all the errors are automatically logged and formatted so that it can be viewed easily.
Retrace will not only show what errors have occurred but also analyzes where and what caused it. The time and number of occurrences are also shown in the logs so that it would be a lot easier to focus on which issue to prioritize. To learn more about Retrace, please check out the Retrace feature page on error tracking. Prefix works with. Click here to read more about the acquisition. Try Our Free Code Profiler. Try Our Code Profiler. By Role.
By Technology. By Language. Documentation Support Ideas Portal Menu. Start Free Trial. Tip: Find application errors and performance problems instantly with Stackify Retrace. Troubleshooting and optimizing your code is easy with integrated errors, logs and code level performance insights.
About the Author Latest Posts. Get In Touch. Facebook Twitter Youtube Linkedin.
0コメント