How To Monitor Your Apache Logs
6 min read
Apache logs are very important to any software developer or anyone who has a web application. Apache plays an important role by giving you necessary information about your web operations from the server—information such as traffic volume, errors, and server performance metrics. The information that you get from the Apache web server can help you improve your website. When you look at the error logs, the error message logs will make troubleshooting your web application easier.
Logs are very important. One use case can be to check traffic volume. Knowing your website traffic can be helpful, as it'll give you information that can help you decide whether or not to scale your website hosting resource. You can use these logs to optimize your website and improve its performance because users love fast websites.
How Can You Monitor Your Apache Logs?
There are many ways you can monitor Apache logs. Some of them are tricky, and you'll need some basic understanding of working with a Linux-based environment. If you don't have a basic understanding of Linux, in this post, I'll give some best options to ease your work.
Using Unix Command-Line Tools
One approach to monitoring your Apache logs from the server is by using Unix command-line tools. Many developers and systems admins commonly use this method. Even though writing Unix commands might not be that easy, using this approach does a great job. Although Unix commands won't give you the graphical representation of the logs, you'll be able to get the desired results. These commands are completely free and ready to use.
You can access Apache logs from var/log/log_type. For example, you can access Apache logs from the Apache Unix/Linux server by looking in the following directories:
/var/log/apache/access.log /var/log/apache2/access.log /etc/httpd/log/access_log (on MacOS) /var/log/apache2/error.log
On a Linux server, you can access Apache error logs from var/log/apache2/error.log. You can then log out the errors from the error log file by writing the following command:
sudo tail -f /var/log/apache2/error.log . When you run this command, you'll be able to view the errors in the terminal as they occur in real-time. The tail command tells the machine to read the file and display the results on the terminal. These errors can be used to monitor the operations on your website and also better troubleshoot all the issues that can occur on the web server.
You may have noted that we're passing -f flag in the command. What this flag does is make sure the tail command outputs additional data from the log file. If this flag is left out, you might not be able to get every detail about the log file.
Just like accessing error logs, you can also monitor access logs by running the same command mentioned previously. Access logs are a list of files that your users or even bots are requesting from your website and all the processed files. Access logs are commonly used for monitoring a website's performance. You can also use them for security reasons because they store all the requests that are sent to the server. You will be able to know what's being requested and where it's coming from.
Apache Log Levels
For better results when logging in to Apache, Apache lets you separate log messages into categories. This will help you receive only the log messages that you need to work with or see. For example, if you want to log information messages only, you can set the level in the Apache configuration file and be able to receive messages at the info level. Here's an example of how that can be done:
sudo nano /etc/apache2/apache2.conf
And you can set the Apache log level to info:
With this configuration in place, you'll be able to receive useful information from the Apache server. If you want to receive serious messages, like errors, just set the LogLevel to error.
Tail, Grep, and Egrep Commands
You can combine two commands to get the best results. When you're using tail and grep, these commands give you the power to monitor a specific type of data from a log file. Now that is awesome—you don't have to see unnecessary messages coming from the server. Here are some scenarios you might need to use tail and grep commands combined.
Specifying an IP Address
If you want to get log messages from a certain IP address, you can simply run this single line of command, and you'll get information from that specific IP address:
tail -f /var/log/apache2/access.log | grep 192.168.206.1
The combination of these commands will match the partner that you want. This way, you'll watch requests from only the IP address that you've specified.
Excluding File Types From the Requests
When you're monitoring Apache logs, you'll be able to see a lot of unwanted files being logged on the console. This can make your job difficult, but there's a way out of this. What you have to do is exclude some of the files you don't need from the log message. If you don't need to watch things like images, you can run a command like this:
tail -f /var/log/apache2/error.log | egrep -v "(.gif|.jpg|.png|.swf|.ico)"
And just as simple as that, you won't be watching unnecessary images. This will give your log messages a cleaner look, making it easier to spot errors in the log messages.
In the same way, if you want to get only a specific file, you can simply run the command like this:
tail -f /var/log/apache2/error.log | grep .png Now, you'll be monitoring only those logs that have .png file types. Using commands and a console to analyze data can be challenging, but there are tools that can help you view data in the UI. Loggly has some integration with AppOptics that can help you analyze data more easily. You can also take a look at Pingdom and see how this platform can help you with logging into your web app.
Using a Dashboard To Monitor Apache Logs
Although using command-line tools to monitor log messages isn't very difficult, it's not easy to read these log messages on a console. It's much better and easier if you can view these log messages in a way that makes it easy to spot the problem from the server. Using software can give you the ability to view all these log messages in a more readable way and alert you, so you can act on what your business needs.
Quite a few platforms offer such services. Loggly helps you monitor Apache logs and can also alert you on time to resolve any error on the website. This means you can act quickly before the business is affected. Loggly provides a variety of ways to quickly visualize data, and the dashboards let you organize data in the most useful ways for detecting and understanding the problems that arise in software and infrastructure. You can sign up here and use Loggly to monitor Apache logs.
You simply can't avoid logs; they provide the best data you can use to offer the best user experience to your website's users. One error can cause a business to lose sums of money. Acting quickly to solve the issue is key, and the only way you can know about these errors before your users do is by logging the error messages—or any other logs that you desire to work with.
This post was first posted on Loggly website. You can find the post here.
Did you find this article valuable?
Support Mathews Musukuma by becoming a sponsor. Any amount is appreciated!