Features
Logger can be used to log PHP errors and problems instead of showing them to the visitors. This way it's easieer for you to detect problemens in a website/application and solve them. And your visitors won't be bothered with ugly error messages.
There are 4 types of logging possible:
- Print errors to screen
- Email errors to your mailbox
- Log errors to a file
- Send errors to a logging server
Use
1. Include
The first thing you need to do is including the class in your index.php file. If the class file(logger.php) is in the same directory as your current script you can use the following line of code. The next thing you need to do is create an instance of the class. The first parameter you add is the type of messages you want to log.
- LOGGER_ALL = Log all problems and errors
- LOGGER_ERROR = Log only errors
- LOGGER_WARNING = Log errors and warnings
- LOGGER_PARSE = Log parse problems, errors and warnings
- LOGGER_NOTICE = Log notices, parse problems, errors and warnings
The second parameter is the manner of logging.
- METHOD_LOG = Log to file
- METHOD_MAIL = Send logs to email
- METHOD_PRINT = Print logs to screen
- METHOD_SERVER = Send logs to logserver
Add this code to the top of your index.php file.
include("logger.php");
$handler = new Logger(LOGGER_ALL, METHOD_PRINT);
This code above make a logger that prints all the errors to the screen.
If you use the METHOD_LOG there will be created a directory log in the same directory as the logger.php file where all the logs will be stored.
Don't forget to upload the logger.php file in the same directory as your index.php file.
2. Send emails
To send an email when there is an error you use the following code:
include("logger.php");
$handler = new Logger(LOGGER_WARNING, METHOD_EMAIL);
$handler->SetEmail("info@mymail.be");
Replace info@mymail.be in the code above with your own email address.
If there is an error in the website there will be send an email to the email address info@mymail.be with more information about the error.
It's recommended that you use LOGGER_WARNING or LOGGER_ERROR when using the mail log. Otherwise it's possible that you will be overwhelmed with mails.
3. Logger server
This function is particular handy when you have a lot of clients that you want to monitor for errors. If there is an error on one of your client websites there will be send a message to a central server. On this server you can view all the errors from all your clients.
First whe gonna install the Logger server. Open the file logserver/server.php in a texteditor and change the variable $logserver_psk to some random string you can choose. The key is needed to secure the server so that no unauthorized clients are allowed to send messages to our server. Copy the complete "logserver" directory to your own server.
The code you need to install on the clients websites is the following. Add this code to the top of the index.php file.
include("logger.php");
$handler = new Logger(LOGGER_WARNING, METHOD_SERVER);
$handler->SetServer("http://www.mywebsite.be/logserver/server.php");
$handler->SetServerPsk("the same string as $logserver_psk");