Mallow's Blog

How to integrate HipChat Room With Laravel Application

Debugging is a process of an identifying a problem or the source of a problem then either correcting that problem or defining a way for work on it. It is the part of the software development life cycling. We can easily identify our application problems with the help of debugging.

The debugging is involving in the process of locating an errors. We can track the application process on the input, output, database transactions, logic of the code etc.

Log

We can use the Log to debug our application. Log is an option to record the application errors, the database transaction, application process and errors in the application. We can use the Log to validate the application’s process or behaviour. If we are using the Log, then we can track the status, problems etc., from the application.

Laravel’s Logging

When we are starting a Laravel project, the error and exception handling is already configured in the application. It is monitoring the errors from the application and if any then all are logged and rendered back to the user. The default rendering method is returning the exception back to the browser as a HTTP response.

Here the laravel is using Monolog library to handling the logs. The log files are stored in the storage/logs directory. Laravel is allowing us to save the logs as a single or multiple files. The Log example looks like this,

use Illuminate\Support\Facades\Log;

Log::info(‘Showing user profile name: Laravel‘);

Monolog is a php library to record the triggered errors and desired logs from our application. Monolog has a stack of handlers. We can use one or more handlers based on our need. Example handlers are HipchatHandler, MailHandler, SlackHandler etc.

The monolog is giving a different type of levels to record a log message. They are,

             * debug

             * info

             * notice

             * warning

             * error

            * critical

            * alert

            * emergency

So, please ensure about what type of log’s level is used by our application.

Laravel’s Errors & Logging is explaining more over the how to configure and maintain the monolog log files in the application.

Error Log Notifications

Laravel is providing the log file to debug the application. But, the developer needs to spend his/her precious time on reading this log file(s) and identifying the error(s). Think about it, If the developer getting the notification about the application error at the time of log entry based on the log level then the developer can easily resolve the error based on the severity.

We are mentioned at early the monolog is having the various configuration handlers. From that handlers, we are going to use the Hipchat Handler to get the notification of an error from our application.

The purpose of this configuration, we can easily improve our application quality, the logic of the code and can develop the skill in our coding style or programming language.

We will see about below coming points for how to send a notification to the HipChat room from the laravel application.

Create HipChat Room

First of all, we need a hipchat room for the laravel application. Here we will see about the steps to create a room.

Step 1:-

Open the hipchat account and launch the web app or download any given app from that page based on the our system/mobile. We have a option to ‘create a room’ and enter the further details. For example, the room name, it will look like,

Ex:- hipchat-testing

Step 2:-

Clicking the integration option from the room or web page of hipchat. Now, we can see the integrations page with a option to “select a room” while clicking this it will be list the created rooms name (Note:- If we are an admin person for that room(s).). Now we can select (any one or) the required room from that list. For Example using the “hipchat-testing”.

Now, we can choose “Build your own integration” then need to give a name for our integration in the next page.

Ex:- hipchat-app

Step3:-

On next screen, we can get the URL for our hipchat room. It will look like this,

https://COMPANY_NAME.hipchat.com/v2/room/ROOM_ID/notification?auth_token=AUTH_TOKEN

Configure The Room With Laravel Application

We have a two type of process to connect the room with the laravel application.

Process 1:-

We can configure the hipchat room from the application bootstrap/app.php file. Laravel is documented about the custom monolog configuration.

$app->configureMonologUsing(function ($monolog) {
</span><span class="s1"><span class="Apple-converted-space">   </span>$monolog-&gt;pushHandler(...);
</span><span class="s1">});
</span><span class="s1">return $app; 

This will notify the errors for the configured hipchat room. In this process, we have a disadvantage of can not create as usual log files.

Process 2:-

We have an another process of configuration to get as usual log files and hipchat room notification also. Please make the changes in the above code and locate it in the boot method of AppServiceProvider.php.

use Illuminate\Support\Facades\Log;

</p>
<p class="p3"><span class="s1">Log::getMonoLog()-&gt;pushHandler(new \Monolog\Handler\HipChatHandler(</span></p>
<p class="p3"><span class="s1"><span class="Apple-converted-space">                </span>’AUTH_TOKEN’, ‘ROOM_ID', ‘hipchat-app’, true, \Monolog\Logger::CRITICAL, true, true, ‘text', 'COMPANY_NAME'.hipchat.com, 'v2'</span></p>
<p class="p3"><span class="s1">)); 

Conclusion

So in this post, we have seen in detail about how to configure the hipchat room for log entries from laravel application. The advantage of this configuration is, we can easily debug our application via the notification of hipchat room. I hope this blog is helpful for you while trying to debug your laravel application.

Ramadurai M,
Junior Developer,
Mallow Technologies.

2 Comments

  1. Saravanakumar B

    Nice tutorial. Explanation helps a lot to understand easier.

    1. Gopal

      Thank you !!

Leave a Comment

Your email address will not be published. Required fields are marked *