12
12
use RuntimeException ;
13
13
use Toolkit \Cli \Color \Alert ;
14
14
use Toolkit \Cli \Color \Prompt ;
15
+ use Toolkit \Cli \Color \ColorTag ;
15
16
use Toolkit \Cli \Traits \ReadMessageTrait ;
16
17
use Toolkit \Cli \Traits \WriteMessageTrait ;
17
18
use function count ;
24
25
use function is_numeric ;
25
26
use function json_encode ;
26
27
use function preg_replace ;
28
+ use function sprintf ;
27
29
use function stream_isatty ;
28
30
use function strtoupper ;
29
31
use function trim ;
@@ -135,23 +137,27 @@ public static function color(string $text, array|string $style = null): string
135
137
* @param string $msg
136
138
* @param array $data
137
139
* @param string $type
138
- * @param array $opts
140
+ * @param array{writeOpts:array} $labels
139
141
* [
140
142
* '_category' => 'application',
141
143
* 'process' => 'work',
142
144
* 'pid' => 234,
143
145
* 'coId' => 12,
144
146
* ]
145
147
*/
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
147
149
{
148
150
if (isset (self ::LOG_LEVEL2TAG [$ type ])) {
149
151
$ type = ColorTag::add (strtoupper ($ type ), self ::LOG_LEVEL2TAG [$ type ]);
150
152
}
151
153
152
- $ userOpts = [];
154
+ $ userOpts = $ writeOpt = [];
155
+ if (isset ($ labels ['writeOpts ' ])) {
156
+ $ writeOpt = $ labels ['writeOpts ' ];
157
+ unset($ labels ['writeOpts ' ]);
158
+ }
153
159
154
- foreach ($ opts as $ n => $ v ) {
160
+ foreach ($ labels as $ n => $ v ) {
155
161
if (is_numeric ($ n ) || str_starts_with ($ n , '_ ' )) {
156
162
$ userOpts [] = "[ $ v] " ;
157
163
} else {
@@ -162,7 +168,8 @@ public static function clog(string $msg, array $data = [], string $type = 'info'
162
168
$ optString = $ userOpts ? ' ' . implode (' ' , $ userOpts ) : '' ;
163
169
$ dataString = $ data ? PHP_EOL . json_encode ($ data , JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) : '' ;
164
170
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 );
166
173
}
167
174
168
175
/*******************************************************************************
0 commit comments