Skip to content

Commit

Permalink
Add examples & add call new static method
Browse files Browse the repository at this point in the history
  • Loading branch information
mimou78 authored and Mohamed El Mrabet committed Nov 21, 2021
1 parent 94fdb8f commit cb14451
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,38 @@ Add dependency
composer require cleatsquad/logger
```

## Using
## Examples

You can use it in your php class like this

```php
\CleatSquad\Logger::log('test', 'test.log');
\CleatSquad\Logger::log('test', 'test.log', \Monolog\Logger::WARNING);
\CleatSquad\Logger::log(['test' => 'is an array'], 'test.log');
\CleatSquad\Logger::log('Hello!!', 'test.log');
\CleatSquad\Logger::log('Hello!!', 'test.log', \Monolog\Logger::WARNING);
\CleatSquad\Logger::log([22 => 'is an array'], 'test.log');
\CleatSquad\Logger::info(['is an array'], 'test.log');

try {
throw new \Exception('Error message');
} catch (\Exception $exception) {
\CleatSquad\Logger::critical($exception);
}
```
## Results


**test.log**

```
[2021-11-21 14:17:44] logger.DEBUG: Hello!! [] []
[2021-11-21 14:17:44] logger.WARNING: Hello!! [] []
[2021-11-21 14:17:44] logger.DEBUG: Array ( [22] => is an array ) [] []
[2021-11-21 14:17:44] logger.INFO: Array ( [0] => is an array ) [] []
```

```
[2021-11-21 14:22:40] logger.CRITICAL: Exception: Error message in /var/www/html/pub/index.php:37 Stack trace: #0 {main} [] []
```

## Log Levels

Monolog supports the logging levels described by [RFC 5424](http://tools.ietf.org/html/rfc5424).
Expand Down
55 changes: 50 additions & 5 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
*/
class Logger
{
/**
* @param string $filename
* @param int $type
* @return LoggerHandler
* @throws \Exception If a missing directory is not buildable
* @throws \InvalidArgumentException If stream is not a resource or string
*/
private static function prepareLog($filename, $type = LoggerHandler::DEBUG)
{
$logger = new LoggerHandler('logger');
$logger->pushHandler(new StreamHandler(BP . sprintf('/var/log/%s', $filename), $type));
return $logger;
}

/**
* Send log to the defined file in var/log
*
Expand All @@ -28,11 +42,42 @@ class Logger
*/
public static function log($text, $filename = 'logger.log', $type = LoggerHandler::DEBUG)
{
if (is_array($text)) {
$text = print_r($text, true);
if ($text) {
if (is_object($text) && method_exists($text, '__toString')) {
$text = $text->__toString();
}
if (is_array($text) || is_object($text)) {
$text = var_export($text, true);
}

$logger = self::prepareLog($filename, $type);
$logger->log($type, $text);
}
}

/**
* Send log to the defined file in var/log defined type
*
* @param string $name
* @param array $arguments
* @throws \Exception If a missing directory is not buildable
* @throws \InvalidArgumentException If stream is not a resource or string
*/
public static function __callStatic($name, $arguments)
{
if (isset($arguments[0])) {
if (is_object($arguments[0]) && method_exists($arguments[0], '__toString')) {
$arguments[0] = $arguments[0]->__toString();
}
if (is_array($arguments[0]) || is_object($arguments[0])) {
$arguments[0] = print_r($arguments[0], true);
}

if (!isset($arguments[1])) {
$arguments[1] = 'logger.log';
}
$logger = self::prepareLog($arguments[1]);
$logger->$name($arguments[0]);
}
$logger = new LoggerHandler('logger');
$logger->pushHandler(new StreamHandler(BP . sprintf('/var/log/%s', $filename), $type));
$logger->log($type, $text);
}
}

0 comments on commit cb14451

Please # to comment.