Big impact with little effort
Making a small change could impact thousands in the open source world
With 500K+ current installations, the Laravel/Lumen framework is one of the most popular open source PHP micro-frameworks for rapid API development. We use it in several of our projects and we try to take advantage of all of its features. One of the super cool and easy to use tools is Logging. You can log everything within your codebase with just a line of code:
You can configure where to write messages: to a file, via e-mail, Slack, or almost any web service where you can review information easily. It uses Monolog behind the scenes which is a powerful PHP library for sending log messages. One of the advanced features is adding context information about the message:
Log::error('Something went wrong!', ['exception' => $exception]);
This code will automatically format the
$exception into readable text. It has all details about exception message, class, line of the originating exception, and full stack-trace by default.
About the Exception handler
Even if you don’t use it, Lumen is taking advantage of this. At its core, every time your application throws an exception, it logs it for your convenience. This happens in its Exception handler class. It takes care of reporting and rendering the exception to the end-user. If the end-user is a developer you want to show detailed information about the error. For a customer, you need an understandable message that helps them work with the application. It will hide any code-related data. You can read more about proper error handling here. When your application is published, you can review your logs occasionally. You can trigger an alarm when you receive an error so you can improve your application.
Sending messages to Microsoft Teams
For one of our projects, we wanted to send all warning or error messages to a channel in Microsoft Teams. We handled it with margatampu/laravel-teams-logging. There was a problem with receiving all information. We started debugging the laravel teams library and found some errors. Because it was not maintained for a while we’ve forked it. In the end, we’ve written some tests and fixed all issues we had in our own library osi-open-source/laravel-teams-logging. Then, for an error message using the Logger, it showed right away. Strangely, when an unknown exception was rendered in our production environment it did not produce any message in Teams. This is how we came down to look into the framework. We found that there was an error in the Handler.php class:
Our Pull request
Instead of fixing directly, extending the class, or working around the issue, we made a Pull request. It was accepted and merged into the new version a day later. As a result, we spend a small time on the issue but made a big impact. Your normal work involved working with Teams and we were able to see when an error occurred right away.
Our code was improved and became simpler. Our reports show that we’ve decreased our lines of code by 50, for instance. This is a benefit – less code, fewer problems.
Contributions are always a great idea and have only positive outcomes. With public contributions, people can see your work and assess your qualities. They impact other people that use the code in their projects. I definitely like my impact on the year 2020’s open source community:
Most people directly address issues rather than support the open source world
Some people just use the tools without any extra thought. Just because most of it is free are willing to ignore a bug or two. Or look for an alternative if they have a blocker. Some even write their own functionality wasting many hours in developing something that many developers already solved. There are too many codebases out there. Instead of reinventing some standard logic all over again by ourselves, it’s better to help some existing libraries and maintain a repository.
October is a popular time for contributions as there is a campaign by DigitalOcean in partnership with GitHub and Twilio to promote open source code. Even if you don’t participate, next time you have a problem with a library, try to fix it yourself. As a result, many people could benefit from that as well as you can benefit from others. After all, a single person could not build a large complex system by themselves. The cheapest way to do it is by a strong and supportive open source community.
Passionate Web Developer with strong PHP experience. Interested in Informatics, data science, ml, and good programming habits.