11import type { EventFnSuccess , QueryClause , WhereClause } from '@magnetarjs/types'
2- import { isArray , isFullArray , isNumber } from 'is-what'
2+ import { isAnyObject , isArray , isFullArray , isNumber } from 'is-what'
33
44/**
55 * `Line-height: 2` is to prevent line overlapping on Safari
66 */
77const LOGGER_STYLE =
88 'background: #0e0f15; color: #af98e6; border-radius: 4px; padding: 6px 10px; line-height: 2;'
99
10+ const LOGGER_STYLE_ERROR =
11+ 'background: #b91c1c; color: #ffffff; border-radius: 4px; padding: 6px 10px; line-height: 2;'
12+
1013/**
1114 * Logs to the console with `console.info` and colors.
1215 */
1316export function logWithFlair ( message : string , ...args : any [ ] ) : undefined {
14- console . info ( `%c💫 [magnetar] ${ message } ` , LOGGER_STYLE , ...args )
17+ const isError = args . some ( ( a ) => isAnyObject ( a ) && ! ! a ?. [ 'error' ] )
18+ if ( isError ) {
19+ console . error ( `%c💫 [magnetar] ${ message } ` , LOGGER_STYLE_ERROR , ...args )
20+ } else {
21+ console . info ( `%c💫 [magnetar] ${ message } ` , LOGGER_STYLE , ...args )
22+ }
1523}
1624
1725let lastGroupLogTime = 0
@@ -38,7 +46,7 @@ function shouldLog(params: any, preventLogFor: number) {
3846export function logWithFlairGroup (
3947 title : string ,
4048 nestedMessage : string ,
41- options ?: { preventLogFor : number }
49+ options ?: { preventLogFor : number } ,
4250) : undefined {
4351 if ( options && ! shouldLog ( [ title , nestedMessage ] , options . preventLogFor ) ) return
4452
@@ -51,12 +59,12 @@ function stringifyQueryClause(q: QueryClause): string {
5159 return 'or' in q
5260 ? `or(${ q . or
5361 . map ( ( clause ) =>
54- isArray ( clause ) ? stringifyWhereClause ( clause ) : stringifyQueryClause ( clause )
62+ isArray ( clause ) ? stringifyWhereClause ( clause ) : stringifyQueryClause ( clause ) ,
5563 )
5664 . join ( ', ' ) } )`
5765 : `and(${ q . and
5866 . map ( ( clause ) =>
59- isArray ( clause ) ? stringifyWhereClause ( clause ) : stringifyQueryClause ( clause )
67+ isArray ( clause ) ? stringifyWhereClause ( clause ) : stringifyQueryClause ( clause ) ,
6068 )
6169 . join ( ', ' ) } )`
6270}
0 commit comments