-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic tools I use from project to project
- Loading branch information
0 parents
commit e73b7e3
Showing
6 changed files
with
540 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules/ | ||
*.iml | ||
.idea/ | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "enobrev/tools", | ||
"description": "General Tools", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Mark Armendariz", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"monolog/monolog": "^1.21" | ||
}, | ||
"autoload": { | ||
"psr-0" : { | ||
"Enobrev" : "src" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
namespace Enobrev; | ||
|
||
use Monolog; | ||
use Monolog\Formatter\LineFormatter; | ||
use Monolog\Handler\SyslogHandler; | ||
|
||
class Log { | ||
/** @var Monolog\Logger */ | ||
private static $oLog = null; | ||
private static $sName = null; | ||
|
||
private static function init() { | ||
if (self::$oLog === null) { | ||
if (self::$sName === null) { | ||
throw new \Exception("Please set a Name for the Logger using Enobrev\\Log::setName()"); | ||
} | ||
|
||
self::$oLog = new Monolog\Logger(self::$sName); | ||
// $oFormatter = new LineFormatter("@cee: %context%"); // TODO: Activate @cee logger - will need to use config from build/ideas and add a mapper cron to ensure elasticsearch doesn't use resources trying to index all the response variables | ||
$oFormatter = new LineFormatter("%context%"); | ||
$oSyslog = new SyslogHandler('API'); | ||
$oSyslog->setFormatter($oFormatter); | ||
self::$oLog->pushHandler($oSyslog); | ||
} | ||
|
||
return self::$oLog; | ||
} | ||
|
||
/** | ||
* Adds a log record at the designated level | ||
* | ||
* @param int $iLevel The logging level | ||
* @param string $sMessage The log message | ||
* @param array $aContext The log context | ||
* @return Boolean Whether the record has been processed | ||
*/ | ||
private static function addRecord($iLevel, $sMessage, array $aContext = array()) { | ||
return self::init()->addRecord($iLevel, $sMessage, array_merge(['action' => $sMessage], $aContext)); | ||
} | ||
|
||
/** | ||
* @param string $sName | ||
*/ | ||
public static function setName(string $sName) { | ||
self::$sName = $sName; | ||
} | ||
|
||
/** | ||
* Adds a log record at the DEBUG level. | ||
* | ||
* @param string $sMessage The log message | ||
* @param array $aContext The log context | ||
* @return Boolean Whether the record has been processed | ||
*/ | ||
public static function d($sMessage, array $aContext = array()) { | ||
self::addRecord(Monolog\Logger::DEBUG, $sMessage, $aContext); | ||
} | ||
|
||
/** | ||
* Adds a log record at the INFO level. | ||
* | ||
* @param string $sMessage The log message | ||
* @param array $aContext The log context | ||
* @return Boolean Whether the record has been processed | ||
*/ | ||
public static function i($sMessage, array $aContext = array()) { | ||
self::addRecord(Monolog\Logger::INFO, $sMessage, $aContext); | ||
} | ||
|
||
/** | ||
* Adds a log record at the NOTICE level. | ||
* | ||
* @param string $sMessage The log message | ||
* @param array $aContext The log context | ||
* @return Boolean Whether the record has been processed | ||
*/ | ||
public static function n($sMessage, array $aContext = array()) { | ||
self::addRecord(Monolog\Logger::NOTICE, $sMessage, $aContext); | ||
} | ||
|
||
/** | ||
* Adds a log record at the WARNING level. | ||
* | ||
* @param string $sMessage The log message | ||
* @param array $aContext The log context | ||
* @return Boolean Whether the record has been processed | ||
*/ | ||
public static function w($sMessage, array $aContext = array()) { | ||
self::addRecord(Monolog\Logger::WARNING, $sMessage, $aContext); | ||
} | ||
|
||
/** | ||
* Adds a log record at the ERROR level. | ||
* | ||
* @param string $sMessage The log message | ||
* @param array $aContext The log context | ||
* @return Boolean Whether the record has been processed | ||
*/ | ||
public static function e($sMessage, array $aContext = array()) { | ||
self::addRecord(Monolog\Logger::ERROR, $sMessage, $aContext); | ||
} | ||
|
||
/** | ||
* Adds a log record at the CRITICAL level. | ||
* | ||
* @param string $sMessage The log message | ||
* @param array $aContext The log context | ||
* @return Boolean Whether the record has been processed | ||
*/ | ||
public static function c($sMessage, array $aContext = array()) { | ||
self::addRecord(Monolog\Logger::CRITICAL, $sMessage, $aContext); | ||
} | ||
|
||
/** | ||
* Adds a log record at the ALERT level. | ||
* | ||
* @param string $sMessage The log message | ||
* @param array $aContext The log context | ||
* @return Boolean Whether the record has been processed | ||
*/ | ||
public static function a($sMessage, array $aContext = array()) { | ||
self::addRecord(Monolog\Logger::CRITICAL, $sMessage, $aContext); | ||
} | ||
|
||
/** | ||
* Adds a log record at the EMERGENCY level. | ||
* | ||
* @param string $sMessage The log message | ||
* @param array $aContext The log context | ||
* @return Boolean Whether the record has been processed | ||
*/ | ||
public static function em($sMessage, array $aContext = array()) { | ||
self::addRecord(Monolog\Logger::EMERGENCY, $sMessage, $aContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
namespace Enobrev; | ||
|
||
class Timer { | ||
private $aTimers; | ||
|
||
public function __construct() { | ||
$this->aTimers = array(); | ||
} | ||
|
||
/** | ||
* @param $sLabel | ||
*/ | ||
public function init($sLabel) { | ||
$this->aTimers[$sLabel] = array( | ||
'start' => 0, | ||
'stop' => 0 | ||
); | ||
} | ||
|
||
/** | ||
* @param string $sLabel | ||
* @return array | ||
*/ | ||
public function get($sLabel) { | ||
if (isset($this->aTimers[$sLabel]) | ||
&& isset($this->aTimers[$sLabel]['start'])) { | ||
$this->aTimers[$sLabel]['range'] = $this->aTimers[$sLabel]['stop'] - $this->aTimers[$sLabel]['start']; | ||
$this->aTimers[$sLabel]['range_human'] = sprintf("%01.2f", $this->aTimers[$sLabel]['range']); | ||
|
||
return $this->aTimers[$sLabel]; | ||
} | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getAll() { | ||
$aReturn = array(); | ||
$aReturn['__total__'] = array( | ||
'range' => 0, | ||
'range_human' => "0.00", | ||
'average' => 0 | ||
); | ||
foreach (array_keys($this->aTimers) as $sLabel) { | ||
$aReturn[$sLabel] = $this->get($sLabel); | ||
$aReturn['__total__']['range'] += $aReturn[$sLabel]['range']; | ||
} | ||
|
||
$aReturn['__total__']['range_human'] = sprintf("%01.2f", $aReturn['__total__']['range']); | ||
$aReturn['__total__']['average'] = sprintf("%01.2f", $aReturn['__total__']['range'] / count($this->aTimers)); | ||
|
||
return $aReturn; | ||
} | ||
|
||
/** | ||
* @param string $sLabel | ||
*/ | ||
public function start($sLabel) { | ||
$this->init($sLabel); | ||
$this->aTimers[$sLabel]['start'] = $this->getTime(); | ||
$this->aTimers[$sLabel]['stop'] = 0; | ||
} | ||
|
||
/** | ||
* @param string $sLabel | ||
*/ | ||
public function stop($sLabel) { | ||
$this->aTimers[$sLabel]['stop'] = $this->getTime(); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
private function getTime() { | ||
return microtime(true) * 1000; | ||
} | ||
} | ||
?> |
Oops, something went wrong.