From f1897ebb64f166e66f9f5fa7156acda0dfa16851 Mon Sep 17 00:00:00 2001 From: inhere Date: Tue, 3 Apr 2018 16:50:57 +0800 Subject: [PATCH] update helper method ConsoleUtil::log --- src/Helper/ConsoleUtil.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Helper/ConsoleUtil.php b/src/Helper/ConsoleUtil.php index 5a6f350..bc6eea9 100644 --- a/src/Helper/ConsoleUtil.php +++ b/src/Helper/ConsoleUtil.php @@ -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) : '' ));