Skip to content

Commit

Permalink
Merge pull request #7 from inhere/master
Browse files Browse the repository at this point in the history
update helper method ConsoleUtil::log
  • Loading branch information
inhere committed Apr 4, 2018
2 parents a738d97 + 34aa14e commit 2cd14ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

Swoft Console Component

# Install
## Install

- composer command

```bash
composer require swoft/console
```

# Document
## Document

Please see [document site](https://doc.swoft.org)

# LICENSE
## LICENSE

The Component is open-sourced software licensed under the [Apache license](LICENSE).
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 2cd14ba

Please sign in to comment.