When production servers are overwhelmed by a flood of log messages, debugging becomes difficult, especially without a structured log design.
Namings
We could name this tool Advanced Log, ABC Log, XYZ Log, TSI Log, or any other names. For this post, I’ll refer to it here as AdvancedLog.
Objective
While a long-term, perfect solution could be ideal, my focus here is on an implementation that’s quick and straightforward, yet still useful enough to support effective debugging in production. This approach is meant to bridge the gap, offering immediate value without requiring extensive setup.
Core Design Overview
Here is the pseudo-implementation:
class AdvancedLog {
private $groups;
private $keywords;
public function __construct(string[] $groups);
public function setGroups(string[] $groups);
public function setKeywords(string[] $keywords);
public function log(string $message);
}
Groups operate at a class-wide level and can be toggled on or off using flags stored in the database, allowing us to activate or deactivate specific log categories based on our debugging needs in production. Keywords make searching easier, especially when using command-line tools like grep
.
Usage Example:
$log = new AdvancedLog(['Shape', 'Pricing']);
// Code to calculate an item's price and shape for customer ID 'TI123456'
$log->addKeywords('TI123456');
$log->log("The price is successfully set to be {$price}, blah blah");
Keywords are cleared after each log call, as they are intended for temporary use only.
Example Log Output:
[2024-11-07 14:51:47] local.DEBUG: [Pricing,Shape][TI123456] App\Cart\Services\CartService: The price is successfully set to be 9.99, blah blah
Note: Groups are sorted alphabetically, making searches more efficient.
Using GREP in the Command Terminal:
To search for logs related to specific groups:
grep -a "Pricing,Shape" laravel-2024-11-06.log
grep -a "Pricing.*Shape" laravel-2024-11-06.log
These commands retrieve all results from the specified groups within a large log file.
For a more precise search with specific keywords:
grep -a "Pricing.*Shape.*TI123456" laravel-2024-11-06.log
Additional Debugging Tips
We can use grep
flags to further assist in our debugging process:
grep -a -A 50 "Pricing,Shape" laravel-2024-11-06.log
This command retrieves the lines containing “Pricing,Shape” and the following 50 lines after them, providing more context around the matched entries.
grep -a -B 50 "Pricing,Shape" laravel-2024-11-06.log
This command retrieves the lines containing “Pricing,Shape” along with the 50 lines before them, which can help identify what led up to the logged message.
grep -a -C 25 "Pricing,Shape" laravel-2024-11-06.log
This command returns 25 lines before and 25 lines after each match, giving a balanced context around the log entries for better insight.
grep -a -i "pricing,shape" laravel-2024-11-06.log
This command performs a case-insensitive search, ensuring that matches for “Pricing,Shape” are found regardless of letter case, making the search more flexible.
This is such a good post