Skip to content

Commit

Permalink
change composer.json and some style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dshovchko committed Nov 13, 2017
1 parent 9f62095 commit 8e54ee0
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 163 deletions.
3 changes: 2 additions & 1 deletion composer.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"php": ">=5.5.27"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^4.8",
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
"psr-4": {
Expand Down
262 changes: 131 additions & 131 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,153 +12,153 @@
namespace Debulog;

abstract class Logger implements LoggerInterface {

/**
* @var string
*/
protected $dir;

/**
* @var string
*/
protected $prefix;

/**
* @var boolean
*/
protected $ondebug;

/**
* @var array
*/
protected $_messages = array();

/**
* @var array
*/
protected $_errors = array();

/**
* @var array
*/
protected $_debugs = array();

/**
* Logger constructor
*
*/
public function __construct($dir, $prefix='my', $debug=false)

/**
* @var string
*/
protected $dir;

/**
* @var string
*/
protected $prefix;

/**
* @var boolean
*/
protected $ondebug;

/**
* @var array
*/
protected $_messages = array();

/**
* @var array
*/
protected $_errors = array();

/**
* @var array
*/
protected $_debugs = array();

/**
* Logger constructor
*
*/
public function __construct($dir, $prefix='my', $debug=false)
{
$this->dir = $dir;
$this->prefix = $prefix;
$this->ondebug = $debug;

if ($this->ondebug === TRUE)
{
$this->dir = $dir;
$this->prefix = $prefix;
$this->ondebug = $debug;

if ($this->ondebug === TRUE)
{
$this->_debugs[] = PHP_EOL . 'start debugging at ' . strftime('%d.%m.%Y %H:%M:%S ') . PHP_EOL;
}

register_shutdown_function(array($this, 'shutdown'));
$this->_debugs[] = PHP_EOL . 'start debugging at ' . strftime('%d.%m.%Y %H:%M:%S ') . PHP_EOL;
}

/**
* Add message to logger buffer
*
* @param string $message Message text
*
*/
public function add($message)

register_shutdown_function(array($this, 'shutdown'));
}

/**
* Add message to logger buffer
*
* @param string $message Message text
*
*/
public function add($message)
{
$this->_messages[] = strftime('%d.%m.%Y %H:%M:%S ') . $message . PHP_EOL;
$this->debug($message);
}

/**
* Add error message to logger buffer
*
* @param string $message Message text
*
*/
public function error($message)
{
$this->_errors[] = strftime('%d.%m.%Y %H:%M:%S ') . $message . PHP_EOL;
$this->debug('ERROR: '.$message);
}

/**
* Add debug message to logger buffer
*
* @param string $message Message to log
*
*/
public function debug($message)
{
if ($this->ondebug === TRUE)
{
$this->_debugs[] = $message . PHP_EOL;
}
}

/**
* Finish and sync all buffers to files
*
*/
public function shutdown()
{
if ($this->ondebug === TRUE)
{
$this->_messages[] = strftime('%d.%m.%Y %H:%M:%S ') . $message . PHP_EOL;
$this->debug($message);
$this->_debugs[] = 'end of debugging at ' . strftime('%d.%m.%Y %H:%M:%S ') . PHP_EOL . PHP_EOL;
}
$this->sync();
}

/**
* Add error message to logger buffer
*
* @param string $message Message text
*
*/
public function error($message)
/**
* Sync all buffers to files
*
*/
public function sync()
{
if (!empty($this->_messages))
{
$this->_errors[] = strftime('%d.%m.%Y %H:%M:%S ') . $message . PHP_EOL;
$this->debug('ERROR: '.$message);
$this->write($this->_messages, $this->dir . $this->prefix . '.log');
$this->_messages = array();
}

/**
* Add debug message to logger buffer
*
* @param string $message Message to log
*
*/
public function debug($message)
if (!empty($this->_debugs))
{
if ($this->ondebug === TRUE)
{
$this->_debugs[] = $message . PHP_EOL;
}
$this->write($this->_debugs, $this->dir . $this->prefix . '_debug.log');
$this->debugs = array();
}

/**
* Finish and sync all buffers to files
*
*/
public function shutdown()

if (!empty($this->_errors))
{
if ($this->ondebug === TRUE)
{
$this->_debugs[] = 'end of debugging at ' . strftime('%d.%m.%Y %H:%M:%S ') . PHP_EOL . PHP_EOL;
}
$this->sync();
$this->write($this->_errors, $this->dir . $this->prefix . '_error.log');
$this->errors = array();
}
}

/**
* Sync all buffers to files
*
*/
public function sync()
/**
* Write text to file
*
* @param string $messages
* @param string $file
*
* @throws Exception
*
*/
protected function write($messages, $file)
{
$f = @fopen($file, 'a');
if ($f === false)
{
if (!empty($this->_messages))
{
$this->write($this->_messages, $this->dir . $this->prefix . '.log');
$this->_messages = array();
}

if (!empty($this->_debugs))
{
$this->write($this->_debugs, $this->dir . $this->prefix . '_debug.log');
$this->debugs = array();
}

if (!empty($this->_errors))
{
$this->write($this->_errors, $this->dir . $this->prefix . '_error.log');
$this->errors = array();
}
throw new \Exception("Logfile $file is not writeable!");
}

/**
* Write text to file
*
* @param string $messages
* @param string $file
*
* @throws Exception
*
*/
protected function write($messages, $file)
foreach($messages as $msg)
{
$f = @fopen($file, 'a');
if ($f === false)
{
throw new \Exception("Logfile $file is not writeable!");
}

foreach($messages as $msg)
{
fwrite($f, $msg);
}

fclose($f);
fwrite($f, $msg);
}

fclose($f);
}
}
60 changes: 30 additions & 30 deletions src/LoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@
namespace Debulog;

interface LoggerInterface {
/**
* Add message to logger buffer
*
* @param string $message Message text
*
*/
public function add($message);

/**
* Add error message to logger buffer
*
* @param string $message Message text
*
*/
public function error($message);

/**
* Add debug message to logger buffer
*
* @param string $message Message to log
*
*/
public function debug($message);

/**
* Sync all buffers to files
*
*/
public function sync();

/**
* Add message to logger buffer
*
* @param string $message Message text
*
*/
public function add($message);

/**
* Add error message to logger buffer
*
* @param string $message Message text
*
*/
public function error($message);

/**
* Add debug message to logger buffer
*
* @param string $message Message to log
*
*/
public function debug($message);

/**
* Sync all buffers to files
*
*/
public function sync();

}
1 change: 0 additions & 1 deletion test/files/add_test.log

This file was deleted.

0 comments on commit 8e54ee0

Please sign in to comment.