Skip to content

Commit

Permalink
update helper method ConsoleUtil::log
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 3, 2018
1 parent a738d97 commit f1897eb
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Helper/ConsoleUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,37 @@ class ConsoleUtil
* @param string $msg
* @param array $data
* @param string $type
* @param array $opts
* [
* '_category' => 'application',
* 'process' => 'work',
* 'pid' => 234,
* 'coId' => 12,
* ]
*/
public static function log(string $msg, array $data = [], string $type = 'info')
public static function log(string $msg, array $data = [], string $type = 'info', array $opts = [])
{
if (isset(self::LOG_LEVEL2TAG[$type])) {
$type = Style::wrap(\strtoupper($type), self::LOG_LEVEL2TAG[$type]);
}

$userOpts = [];

foreach ($opts as $n => $v) {
if (\is_numeric($n) || $n[0] === '_') {
$userOpts[] = "[$v]";
} else {
$userOpts[] = "[$n:$v]";
}
}

$optString = $userOpts ? ' ' . \implode(' ', $userOpts) : '';

\output()->writeln(\sprintf(
'%s [%s] %s %s',
'%s [%s]%s %s %s',
\date('Y/m/d H:i:s'),
$type,
$optString,
\trim($msg),
$data ? PHP_EOL . \json_encode($data, \JSON_UNESCAPED_SLASHES|\JSON_PRETTY_PRINT) : ''
));
Expand Down

0 comments on commit f1897eb

Please sign in to comment.