Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
n-for-all committed Jul 12, 2023
1 parent fcef8a1 commit bf06bbf
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 64 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
Empty file modified src/Format/Default.php
100644 → 100755
Empty file.
Empty file modified src/Handler/Base.php
100644 → 100755
Empty file.
Empty file modified src/Handler/Event.php
100644 → 100755
Empty file.
170 changes: 106 additions & 64 deletions src/Handler/Stream.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

use Ajaxy\Logger\HandlerInterface;

class Stream extends Base implements HandlerInterface{
class Stream extends Base implements HandlerInterface
{

/** @var resource|null */
/** @var resource[]|null */
protected $streams;

/** @var string|null */
/** @var string[]|null */
protected $dirs;

/** @var string|null */
/** @var array|null */
protected $files;

/** @var string|null */
/** @var string[]|null */
private $errors = [];

/** @var string|null */
/** @var string[]|null */
private $tags = null;

private $format = null;
Expand All @@ -29,57 +30,59 @@ class Stream extends Base implements HandlerInterface{
/** @var string|null */
protected $status = HandlerInterface::PENDING;

/** @var string|null */
protected $errorMessage = null;

/**
* @param array of resources|strings $streams
* @param array $tags An array of tags to handle, keep empty for any tag, ex: ['cron' => [], 'mail' => [Logger::ERROR]]
* @param int $level The minimum logging level at which this handler will be triggered
* @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
* @param Boolean $useLocking Try to lock log file before doing any writes
* @param array|strings $streams
* @param array $tags An array of tags to handle, keep empty for any tag, ex: ['cron' => [], 'mail' => [Logger::ERROR]]
* @param int $format The log format to use
* @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
* @param Boolean $useLocking Try to lock log file before doing any writes
*/

public function __construct($streams, $tags = null, $format = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", $filePermission = 775, $useLocking = false)
public function __construct($streams, $tags = null, $format = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", $filePermission = 0775, $useLocking = false)
{
if (is_resource($streams)) {
$this->streams[] = $streams;
} elseif (is_string($streams)) {
if($this->createDir($streams)){
if ($this->createDir($streams)) {
$this->dirs[] = rtrim($streams, DIRECTORY_SEPARATOR);
}
} elseif (is_array($streams)) {
foreach($streams as $stream){
foreach ($streams as $stream) {
if (is_resource($stream)) {
$this->streams[] = $stream;
} elseif (is_string($stream)) {
//Show error if the logger can't create or save its files
if($this->createDir($stream)){
if ($this->createDir($stream)) {
$this->dirs[] = rtrim($stream, DIRECTORY_SEPARATOR);
}
}
}
}
if(empty($this->dirs) && empty($this->streams)){
if (empty($this->dirs) && empty($this->streams)) {
//nothing to write to
$this->errors[] = 'None of the streams provided could be initialized';
$this->status = HandlerInterface::ERROR;
}else{
} else {
$this->status = HandlerInterface::READY;
}
if(is_string($format)){
$this->format = function($tag, $record) use ($format){
if (is_string($format)) {
$this->format = function ($tag, $record) use ($format) {
$value = $format;
foreach($record as $key => $replace){
if(is_array($replace)){
if(empty($replace)){
foreach ($record as $key => $replace) {
if (is_array($replace)) {
if (empty($replace)) {
$replace = '';
}else{
} else {
$replace = json_encode($replace);
}
}
$value = str_replace('%'.$key.'%', $replace, $value);
$value = str_replace('%' . $key . '%', $replace, $value);
}
return $value;
};
}elseif(is_callable($format)){
} elseif (is_callable($format)) {
$this->format = $format;
}
$this->tags = $tags;
Expand All @@ -106,44 +109,55 @@ public function getDirs()
return $this->dirs;
}
/**
* {@inheritdoc}
* Write the stream content to the directories
*
* @param string $tag
* @param array $record
*
* @return void
*/
protected function writeToDirs($tag, array $record)
{
if(!empty($this->dirs)){
foreach($this->dirs as &$dir){
if (!empty($this->dirs)) {
foreach ($this->dirs as &$dir) {
if (null === $dir || '' === $dir) {
continue;
}
$filename = $dir.DIRECTORY_SEPARATOR.$tag.".log";
$filename = $dir . DIRECTORY_SEPARATOR . $tag . ".log";
//if the directory was converted to a resource
if (isset($this->files[$filename]) && is_resource($this->files[$filename])) {
$this->streamWrite($this->files[$filename], null, $record);
continue;
}
set_error_handler([$this, 'customErrorHandler']);
$stream = fopen($dir.DIRECTORY_SEPARATOR.$tag.".log", 'a');
$stream = fopen($filename, 'a');
if ($this->filePermission !== null) {
@chmod($dir, $this->filePermission);
@chmod($filename, $this->filePermission);
}
restore_error_handler();
if (is_resource($stream)) {
$this->files[$filename] = $stream;
$this->streamWrite($this->files[$filename], null, $record);
}else{
trigger_error("Logging is inactive\n".implode("\n", $this->errors), E_USER_NOTICE);
} else {
trigger_error("Logging is inactive\n" . implode("\n", $this->errors), E_USER_NOTICE);
}
}
}
return $this;
}

/**
* {@inheritdoc}
* Write the stream content to another stream
*
* @param string $tag
* @param array $record
*
* @return void
*/
protected function writeToStreams($tag, array $record)
{
if(!empty($this->streams)){
foreach($this->streams as $stream){
if (!empty($this->streams)) {
foreach ($this->streams as $stream) {
if ($this->useLocking) {
flock($stream, LOCK_EX);
}
Expand All @@ -158,21 +172,25 @@ protected function writeToStreams($tag, array $record)

/**
* Write to stream
*
* @param resource $stream
* @param string $tag
* @param array $record
*
* @return self
*/
protected function streamWrite($stream, $tag, array $record)
{
$record = call_user_func($this->format, $tag, $record);
$record = ($tag ? $tag ." - ": ''). (string) $record;
$record = ($tag ? $tag . " - " : '') . (string) $record;
fwrite($stream, $record);
return $this;
}


private function customErrorHandler($code, $msg)
{
$this->errors[] = $code ." - ". $msg;
$this->errors[] = $code . " - " . $msg;
}

/**
Expand All @@ -195,41 +213,62 @@ private function createDir($dir)
{
$dir = $this->getDirFromUrl($dir);
if (null !== $dir) {
if(is_dir($dir)){
return true;
}

$this->errorMessage = null;
$status = mkdir($dir, 0775, true);
return $status;
if (!is_dir($dir)) {
$umask = umask(0);

if (!@mkdir($dir, 0755, true)) {
$this->errorMessage = error_get_last();
}

umask($umask);
clearstatcache(false, $dir);

if (!is_dir($dir) || !is_readable($dir)) {
throw new \Exception(sprintf('Failed to create the log directory "%s". %s', $dir, $this->errorMessage));
}
}
return true;
}
return false;
}

/**
* {@inheritdoc}
* Handle the stream content
*
* @param string $tag
* @param array $record
*
* @return self
*/
public function handle($tag, array $record)
{
if(!empty($this->dirs)){
if (!empty($this->dirs)) {
$this->writeToDirs($tag, $record);
}
if(!empty($this->streams)){
if (!empty($this->streams)) {
$this->writeToStreams($tag, $record);
}
return $this;
}

/**
* {@inheritdoc}
* Check if the stream can handle the given tag
*
* @param string $tag
* @param integer $debug_level
*
* @return boolean
*/
public function canHandle($tag, $debug_level)
{
if(!$this->tags){
if (!$this->tags) {
return true;
}
foreach($this->tags as $key => $levels){
if(strtolower($key) == strtolower($tag)){
if(in_array($debug_level, $levels)){
foreach ($this->tags as $key => $levels) {
if (strtolower($key) == strtolower($tag)) {
if (in_array($debug_level, (array)$levels)) {
return true;
}
}
Expand All @@ -238,34 +277,40 @@ public function canHandle($tag, $debug_level)
}

/**
* {@inheritdoc}
* Get all errors
*
* @return array
*/
public function getErrors()
{
return $this->errors;
}

/**
* {@inheritdoc}
* Get status code
*
* @return integer
*/
public function getStatus()
{
return $this->status;
}

/**
* {@inheritdoc}
* End the stream
*
* @return self
*/
public function end()
{
if(!empty($this->files)){
foreach($this->files as $file){
if(is_resource($file)) @fclose($file);
if (!empty($this->files)) {
foreach ($this->files as $file) {
is_resource($file) && @fclose($file);
}
}
if(!empty($this->streams)){
foreach($this->streams as $file){
if(is_resource($file)) @fclose($file);
if (!empty($this->streams)) {
foreach ($this->streams as $file) {
is_resource($file) && @fclose($file);
}
}
$this->streams = null;
Expand All @@ -275,6 +320,3 @@ public function end()
return $this;
}
}


?>
Empty file modified src/HandlerInterface.php
100644 → 100755
Empty file.
Empty file modified src/Logger.php
100644 → 100755
Empty file.

0 comments on commit bf06bbf

Please sign in to comment.