1212use RuntimeException ;
1313use Toolkit \Cli \Color \Alert ;
1414use Toolkit \Cli \Color \Prompt ;
15+ use Toolkit \Cli \Color \ColorTag ;
1516use Toolkit \Cli \Traits \ReadMessageTrait ;
1617use Toolkit \Cli \Traits \WriteMessageTrait ;
1718use function count ;
2425use function is_numeric ;
2526use function json_encode ;
2627use function preg_replace ;
28+ use function sprintf ;
2729use function stream_isatty ;
2830use function strtoupper ;
2931use function trim ;
@@ -135,23 +137,27 @@ public static function color(string $text, array|string $style = null): string
135137 * @param string $msg
136138 * @param array $data
137139 * @param string $type
138- * @param array $opts
140+ * @param array{writeOpts:array} $labels
139141 * [
140142 * '_category' => 'application',
141143 * 'process' => 'work',
142144 * 'pid' => 234,
143145 * 'coId' => 12,
144146 * ]
145147 */
146- public static function clog (string $ msg , array $ data = [], string $ type = 'info ' , array $ opts = []): void
148+ public static function clog (string $ msg , array $ data = [], string $ type = 'info ' , array $ labels = []): void
147149 {
148150 if (isset (self ::LOG_LEVEL2TAG [$ type ])) {
149151 $ type = ColorTag::add (strtoupper ($ type ), self ::LOG_LEVEL2TAG [$ type ]);
150152 }
151153
152- $ userOpts = [];
154+ $ userOpts = $ writeOpt = [];
155+ if (isset ($ labels ['writeOpts ' ])) {
156+ $ writeOpt = $ labels ['writeOpts ' ];
157+ unset($ labels ['writeOpts ' ]);
158+ }
153159
154- foreach ($ opts as $ n => $ v ) {
160+ foreach ($ labels as $ n => $ v ) {
155161 if (is_numeric ($ n ) || str_starts_with ($ n , '_ ' )) {
156162 $ userOpts [] = "[ $ v] " ;
157163 } else {
@@ -162,7 +168,8 @@ public static function clog(string $msg, array $data = [], string $type = 'info'
162168 $ optString = $ userOpts ? ' ' . implode (' ' , $ userOpts ) : '' ;
163169 $ dataString = $ data ? PHP_EOL . json_encode ($ data , JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) : '' ;
164170
165- self ::writef ("%s [%s]%s %s %s \n" , date ('Y/m/d H:i:s ' ), $ type , $ optString , trim ($ msg ), $ dataString );
171+ $ msg = sprintf ("%s [%s]%s %s %s " , date ('Y/m/d H:i:s ' ), $ type , $ optString , trim ($ msg ), $ dataString );
172+ self ::writeln ($ msg , false , $ writeOpt );
166173 }
167174
168175 /*******************************************************************************
0 commit comments