Hello,
How do i save logs in PHP? Is there any "magical" function available in php for doing so, or any library? Or should i have to fopen file everytime and dump in it? I want to save my logs in text file.
Thanks in advance :)
Hello,
How do i save logs in PHP? Is there any "magical" function available in php for doing so, or any library? Or should i have to fopen file everytime and dump in it? I want to save my logs in text file.
Thanks in advance :)
All depends what you're trying to log. By default you will have an error_log already which is essentially a plain text file. If you're talking about logging events within your code for debugging or tracking activity within a script then you will need to write your own log handler for this but this is very simple. As another poster says you can push content to the error log using the error_log() function but this would make for some very unmanageable log files imv.
If you do not want to use an own implementation or just do fopen-stuff you cyn use the built in function error_log('string to log'); . This will write the desired string into the error log of your server software.
I wrote a simple class to do this. Maybe you'll find it useful.
class Log
{
public function __construct($log_name,$page_name)
{
if(!file_exists('/your/directory/'.$log_name)){ $log_name='a_default_log.log'; }
$this->log_name=$log_name;
$this->app_id=uniqid();//give each process a unique ID for differentiation
$this->page_name=$page_name;
$this->log_file='/your/directory/'.$this->log_name;
$this->log=fopen($this->log_file,'a');
}
public function log_msg($msg)
{//the action
$log_line=join(' : ', array( date(DATE_RFC822), $this->page_name, $this->app_id, $msg ) );
fwrite($this->log, $log_line."\n");
}
function __destruct()
{//makes sure to close the file and write lines when the process ends.
$this->log_msg("Closing log");
fclose($this->log);
}
}
$log=new Log('file_name','my_php_page');
$log->log_msg('fizzy soda : 45 bubbles remaining per cubic centimeter');
If you're not into using the PHP Error Handling Functions (http://www.php.net/manual/en/ref.errorfunc.php) that the other replies have mentioned, here is a deadly simple Logger class that I've used before. Standard warnings apply, as I have not used it in a high risk application or on a heavily trafficked site (though it should be fine).
<?
class Logger
{
private static function addEntry($str)
{
$handle = fopen('./services.log', 'a');
fwrite($handle, sprintf("%s %s\n", date('c'), $str));
fclose($handle);
}
public static function warn($str)
{
self::addEntry("WARNING $str");
}
public static function info($str)
{
self::addEntry("INFO $str");
}
public static function debug($str)
{
self::addEntry("DEBUG $str");
}
}
?>
Then you can use it like this:
<?php
require('Logger.php');
Logger::debug('test');
Logger::warn('bwah');
Logger::info('omg');
?>
Very simple to add more functions (like Logger::error()), store the file handler so you don't have to keep re-opening it every time you want to log something (ie., store the $handle variable in a private static class scope variable, and have addEntry() check to see if it's set whenever it's run and run fopen() if it isn't), or change the format of how you're logging.
Cheers.