In this post i will explain what Graylog2 is and how to use it with magento.
By default magento stores exceptions and other information in log files which is fine for development but in production you never see what happens in the dark. If you run a magento store in multi server environment you also have the problem that each webserver has it’s own log files. I thought it would be very nice to have GUI where i can see all log messages of all webservers in one central place.
What is Graylog2?
Graylog2 is an open source log management solution that stores your logs in a ElasticSearch database.
It consists of a server written in Java that accepts your syslog messages via TCP, UDP or AMQP and stores it in the database.
It also come with a nice web interface that allows you to manage the log messages from your web browser.
Integration in Magento
Before we can start using Graylog2 with our magento store we need a running Graylog2 instance.
You can install the Graylog2 Server + Graylog2 Web Interface by yourself or you can use our AMI for the Amazon EC2 Cloud.
A detailed instruction how to install and configure Graylog2 by yourself can be found on the Graylog2 website.
Step 1: Creating a Amazon EC2 Instance for Graylog2
Login into Amazon EC2 Management Console and launch instance. Afterwards click on “Community AMIs” and search for: ami-2767cc4e
Select your instance type: micro, small or medium and create the instance
Go to Network & Security and Allocate New IP Address to your EC2 instance
Login into your EC2 instance via SSH (make sure SSH and UDP is allowed in your security group)
ssh mgt@75.101.167.230 password: mgt (change your password with "passwd")
Edit Vhost and set your ServerName
sudo nano /etc/apache2/sites-enabled/graylog2
<VirtualHost *:80> ServerName graylog2-demo.mgt-commerce.com ServerAdmin you@example.com DocumentRoot /var/www/graylog2-web-interface-0.9.6p1/public <Directory /var/www/graylog2-web-interface-0.9.6p1/public> Allow from all Options -MultiViews </Directory> ErrorLog /var/log/apache2/error.log LogLevel warn CustomLog /var/log/apache2/access.log combined </VirtualHost>
Restart apache2
sudo /etc/init.d/apache2 restart
Go to your home directory /home/mgt and start Graylog
./start.sh
Open Graylog: http://graylog2-demo.mgt-commerce.com and login with
username: mgt password: graylog2
Step 2: Graylog2 Extension for Magento
We developed a magento extension for graylog2 which logs all errors and log messages in your graylog2.
Download the Graylog2 Magento Extension
Installation
- copy all files to your magento installation
Open app/Mage.php and add the following line
public static function run($code = '', $type = 'store', $options = array()) { ...... } catch (Exception $e) { if (self::isInstalled() || self::$_isDownloader) { //add this line self::dispatchEvent('mage_run_exception',array('exception' => $e)); //----------------------------------------------------------------- self::printException($e); exit(); } }
- Clear the cache in Admin -> System -> Cache Management
- Logout and Login to reload the ACL
- Go to Admin -> System -> Configuration -> MGT-COMMERCE.COM -> Graylog -> Settings -> Active -> Yes
- Enter your Graylog2 host
- Have fun and give Feedback