Debug School

rakesh kumar
rakesh kumar

Posted on

How to examine and troubleshooting in error_log Files using linux command

example of an error_log entry:
search for all occurrences of the string "404 Not Found
view the last 10 lines of the error_log file
extract the timestamp and error message fields from the error_log file
search for all occurrences of the string "Internal Server Error" in the error_log file
search for all occurrences of the string "Internal Server Error" in the error_log file
to search for all PHP errors
to extract the error type and error message columns from the error_log file
to search for all mysql errors

The error_log file is a type of log file that is generated by web servers, such as Apache or Nginx, to record any errors or warnings that occur while the server is running. These errors may include issues such as 404 errors (page not found), server errors, script errors, and more. The error_log file is a useful resource for system administrators and developers to identify and troubleshoot issues with web servers.

Here's an example of an error_log entry:

[Thu Feb 10 14:28:35.982746 2023] [php7:error] [pid 7430] [client 192.168.1.100:34567] PHP Fatal error:  Call to undefined function my_function() in /var/www/html/myscript.php on line 25
Enter fullscreen mode Exit fullscreen mode

Image description

By examining the error_log file, administrators can identify patterns and trends in the errors that are occurring and take corrective action to address them. For example, if the error_log file is filled with 404 errors, it could indicate a problem with the web server configuration, and the administrator would need to investigate the cause and resolve the issue.

error_log logfile using linux pipe command
In Linux, you can use various pipe commands to examine the error_log file and extract useful information. Here are some examples:

1.grep command to search for specific errors:
The grep command can be used to search for specific error messages in the error_log file. For example, to search for all occurrences of the string "404 Not Found" in the error_log file, you can use the following command:

grep "404 Not Found" /var/log/apache2/error_log
Enter fullscreen mode Exit fullscreen mode
grep "not found" /opt/lampp/logs/error_log
Enter fullscreen mode Exit fullscreen mode

Image description
2.tail command to view the latest errors:
The tail command can be used to view the latest entries in the error_log file. For example, to view the last 10 lines of the error_log file, you can use the following command:

tail -n 10 /var/log/apache2/error_log
Enter fullscreen mode Exit fullscreen mode

Image description

more  /opt/lampp/logs/error_log | tail -n 10
cat  /opt/lampp/logs/error_log | tail -n 10

tail -n 10 /opt/lampp/logs/error_log |grep 'not found'
more  /opt/lampp/logs/error_log | grep 'not found'
cat  /opt/lampp/logs/error_log | grep 'not found'


tail -n 10 /opt/lampp/logs/error_log |tail -n 10|grep 'not found'
more  /opt/lampp/logs/error_log |tail -n 10| grep 'not found'
cat  /opt/lampp/logs/error_log |tail -n 10| grep 'not found'
Enter fullscreen mode Exit fullscreen mode

3.awk command to extract specific fields:
The awk command can be used to extract specific fields from the error_log file. For example, to extract the timestamp and error message fields from the error_log file, you can use the following command:

awk '{print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7 " " $8 " " $9 " " $10 " " $11 " " $12 " " $13 " " $14 " " $15 " " $16 " " $17 " " $18 " " $19 " " $20 " " $21 " " $22 " " $23}' /var/log/apache2/error_log



Enter fullscreen mode Exit fullscreen mode

Image description

awk '{print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7 " " $8 " " $9 " " $10 " " $11 " " $12 " " $13 " " $14 " " $15 " " $16 " " $17 " " $18 " " $19 " " $20 " " $21 " " $22 " " $23}' /opt/lampp/logs/error_log


awk '{print $1 " " $2 " " $3 " " $4 " " $NF}' /opt/lampp/logs/error_log

Enter fullscreen mode Exit fullscreen mode

4.grep command to search for specific errors:
The grep command can be used to search for specific error messages in the error_log file. For example, to search for all occurrences of the string "Internal Server Error" in the error_log file, you can use the following command:

grep "Internal Server Error" /var/log/apache2/error_log
Enter fullscreen mode Exit fullscreen mode
grep "not found" /opt/lampp/logs/error_log
Enter fullscreen mode Exit fullscreen mode

This command will print out all the lines that contain the string "Internal Server Error" in the error_log file. You can use this information to identify the cause of the error and take corrective action.
how error_log Files is helpful in troubleshooting script errors using linux pipe commands with examples
5.grep command to search for script:
The grep command can be used to search for specific errors in the error_log file. For example, to search for all PHP errors, you can use the following command:

grep "PHP Error" /var/log/apache2/error_log
Enter fullscreen mode Exit fullscreen mode

Image description

grep "php" /opt/lampp/logs/error_log
Enter fullscreen mode Exit fullscreen mode

6.cut command to extract specific columns:
The cut command can be used to extract specific columns from the error_log file. For example, to extract the error type and error message columns from the error_log file, you can use the following command:

cut -d ' ' -f 3- /var/log/apache2/error_log | cut -d ':' -f 1,4-
Enter fullscreen mode Exit fullscreen mode

Image description

grep "php" /opt/lampp/logs/error_log
Enter fullscreen mode Exit fullscreen mode

how error_log logfile is helpful in troubleshooting MySQL database server error using linux pipe commands with examples

grep "ERROR" /var/log/mysql/error.log
Enter fullscreen mode Exit fullscreen mode
awk '{print $1 " " $2 " " $3 " " $NF}' /var/log/mysql/error.log


 awk '{print $1 " " $2 " " $3 " " $4 " " $NF}' /opt/lampp/logs/error_log


Enter fullscreen mode Exit fullscreen mode

Image description

cut -d ' ' -f 3- /var/log/mysql/error.log | cut -d ':' -f 1,4-
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Top comments (0)