Skip to content

Commit

Permalink
getCallerName() moved to Message class
Browse files Browse the repository at this point in the history
nicer Console logs
  • Loading branch information
Vitexus committed Sep 14, 2023
1 parent 31b4c8e commit 0d0f7f1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
24 changes: 22 additions & 2 deletions src/Ease/Logger/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,29 @@ public static function getTypeUnicodeSymbol($type, $color = true)
default: // Squared Question
$symbol = '🯄';
break;

}
}
return $symbol;
}
}

/**
* Obtain object name from caller object
*
* @param object|string $caller
*
* @return string
*/
public static function getCallerName($caller)
{
if (is_object($caller)) {
if (method_exists($caller, 'getObjectName')) {
$callerName = $caller->getObjectName();
} else {
$callerName = get_class($caller);
}
} else {
$callerName = strval($caller);
}
return $callerName;
}
}
27 changes: 11 additions & 16 deletions src/Ease/Logger/ToConsole.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

declare(strict_types=1);

/**
* Class to Log messages to Console.
*
* @author Vitex <[email protected]>
* @copyright 2016 [email protected] (G)
* @copyright 2016-2023 [email protected] (G)
*/

namespace Ease\Logger;
Expand Down Expand Up @@ -90,8 +89,7 @@ public static function set($str, $color)
{
$colorAttrs = explode("+", $color);
$ansi_str = "";
foreach ($colorAttrs as $attr)
{
foreach ($colorAttrs as $attr) {
$ansi_str .= "\033[" . self::$ansiCodes[$attr] . "m";
}
$ansi_str .= $str . "\033[" . self::$ansiCodes["off"] . "m";
Expand All @@ -109,14 +107,10 @@ public static function set($str, $color)
*/
public function addToLog($caller, $message, $type = 'message')
{
$ansiMessage = $this->set(
' ' . Message::getTypeUnicodeSymbol($type) . ' ' . strip_tags(strval($message)),
self::getTypeColor($type)
);
$logLine = strftime("%D %T") . '' . (is_object($caller) ? (method_exists($caller, 'getObjectName') ? $caller->getObjectName() : get_class($caller) ) : $caller) . '' . $ansiMessage;
$ansiMessage = $this->set(strip_tags(strval($message)), self::getTypeColor($type));
$logLine = strftime("%D %T") . ' ' . Message::getTypeUnicodeSymbol($type) . '' . Message::getCallerName($caller) . '' . $ansiMessage;
$written = 0;
switch ($type)
{
switch ($type) {
case 'error':
$written += fputs($this->stderr, $logLine . "\n");
break;
Expand All @@ -130,12 +124,11 @@ public function addToLog($caller, $message, $type = 'message')
/**
* Get color code for given message
*
* @param string $type mail|warning|error|debug|success
* @param string $type mail|warning|error|debug|success|info
*/
public static function getTypeColor($type)
{
switch ($type)
{
switch ($type) {
case 'mail': // Envelope
$color = 'blue';
break;
Expand All @@ -151,6 +144,9 @@ public static function getTypeColor($type)
case 'success': // Kytička
$color = 'green';
break;
case 'info': // Kytička
$color = 'blue';
break;
default: // i v kroužku
$color = 'white';
break;
Expand All @@ -173,5 +169,4 @@ public static function singleton()
}
return self::$instance;
}

}
}
24 changes: 2 additions & 22 deletions src/Ease/Logger/ToStd.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Log to stdout/stderr
*
* @author Vitex <[email protected]>
* @copyright 2009-2021 [email protected] (G)
* @copyright 2009-2023 [email protected] (G)
*/

namespace Ease\Logger;
Expand Down Expand Up @@ -73,26 +73,6 @@ public static function singleton()
return self::$instance;
}

/**
* Obtain object name from caller object
*
* @param object|string $caller
*
* @return string
*/
public static function getCallerName($caller) {
if (is_object($caller)) {
if (method_exists($caller, 'getObjectName')) {
$callerName = $caller->getObjectName();
} else {
$callerName = get_class($caller);
}
} else {
$callerName = strval($caller);
}
return $callerName;
}

/**
* Zapise zapravu do logu.
*
Expand All @@ -117,7 +97,7 @@ public function addToLog($caller, $message, $type = 'message')
} else {
$person = $user->getObjectName();
}
$caller = $person . ' ' . self::getCallerName($caller);
$caller = $person . ' ' . Message::getCallerName($caller);
}

$logLine = ' `' . $caller . '` ' . str_replace(
Expand Down

0 comments on commit 0d0f7f1

Please sign in to comment.