1
1
import { List , LogSpan } from "effect" ;
2
2
import * as Cause from "effect/Cause" ;
3
3
import * as FiberId from "effect/FiberId" ;
4
- import { pipe } from "effect/Function" ;
4
+ import { identity , pipe } from "effect/Function" ;
5
5
import * as HashMap from "effect/HashMap" ;
6
6
import * as Layer from "effect/Layer" ;
7
7
import * as LogLevel from "effect/LogLevel" ;
@@ -10,6 +10,36 @@ import * as ReadonlyArray from "effect/ReadonlyArray";
10
10
11
11
import { serializeUnknown } from "effect-log/internal" ;
12
12
13
+ const SEVERITY_TO_COLOR : Record <
14
+ LogLevel . LogLevel [ "_tag" ] ,
15
+ ( c : ColorService ) => ( s : string ) => string
16
+ > = {
17
+ All : ( c : ColorService ) => c . white ,
18
+ None : ( c : ColorService ) => c . white ,
19
+ Info : ( c : ColorService ) => c . green ,
20
+ Debug : ( c : ColorService ) => c . blue ,
21
+ Error : ( c : ColorService ) => c . red ,
22
+ Fatal : ( c : ColorService ) => c . boldRed ,
23
+ Trace : ( c : ColorService ) => c . dimWhite ,
24
+ Warning : ( c : ColorService ) => c . yellow ,
25
+ } ;
26
+
27
+ interface ColorService {
28
+ bold ( text : string ) : string ;
29
+ dim ( text : string ) : string ;
30
+ italic ( text : string ) : string ;
31
+
32
+ red ( text : string ) : string ;
33
+ green ( text : string ) : string ;
34
+ yellow ( text : string ) : string ;
35
+ blue ( text : string ) : string ;
36
+ white ( text : string ) : string ;
37
+
38
+ dimItalic ( text : string ) : string ;
39
+ dimWhite ( text : string ) : string ;
40
+ boldRed ( text : string ) : string ;
41
+ }
42
+
13
43
const RESET = "\x1b[0m" ;
14
44
const BOLD = "\x1b[1m" ;
15
45
const DIM = "\x1b[2m" ;
@@ -21,34 +51,57 @@ const YELLOW = "\x1b[33m";
21
51
const BLUE = "\x1b[34m" ;
22
52
const WHITE = "\x1b[37m" ;
23
53
24
- const SEVERITY_TO_COLOR : Record < LogLevel . LogLevel [ "_tag" ] , string > = {
25
- All : WHITE ,
26
- None : WHITE ,
27
- Info : GREEN ,
28
- Debug : BLUE ,
29
- Error : RED ,
30
- Fatal : BOLD + RED ,
31
- Trace : DIM + WHITE ,
32
- Warning : YELLOW ,
54
+ const enabledColorService : ColorService = {
55
+ bold : ( text : string ) => `${ BOLD } ${ text } ${ RESET } ` ,
56
+ dim : ( text : string ) => `${ DIM } ${ text } ${ RESET } ` ,
57
+ italic : ( text : string ) => `${ ITALIC } ${ text } ${ RESET } ` ,
58
+
59
+ red : ( text : string ) => `${ RED } ${ text } ${ RESET } ` ,
60
+ green : ( text : string ) => `${ GREEN } ${ text } ${ RESET } ` ,
61
+ yellow : ( text : string ) => `${ YELLOW } ${ text } ${ RESET } ` ,
62
+ blue : ( text : string ) => `${ BLUE } ${ text } ${ RESET } ` ,
63
+ white : ( text : string ) => `${ WHITE } ${ text } ${ RESET } ` ,
64
+
65
+ dimItalic : ( text : string ) => `${ DIM } ${ ITALIC } ${ text } ${ RESET } ` ,
66
+ dimWhite : ( text : string ) => `${ DIM } ${ WHITE } ${ text } ${ RESET } ` ,
67
+ boldRed : ( text : string ) => `${ BOLD } ${ RED } ${ text } ${ RESET } ` ,
68
+ } ;
69
+
70
+ const disabledColorService : ColorService = {
71
+ bold : identity ,
72
+ dim : identity ,
73
+ italic : identity ,
74
+
75
+ red : identity ,
76
+ green : identity ,
77
+ yellow : identity ,
78
+ blue : identity ,
79
+ white : identity ,
80
+
81
+ dimItalic : identity ,
82
+ dimWhite : identity ,
83
+ boldRed : identity ,
33
84
} ;
34
85
35
86
export interface PrettyLoggerOptions {
36
87
showFiberId : boolean ;
37
88
showTime : boolean ;
38
89
showSpans : boolean ;
90
+ enableColors : boolean ;
39
91
}
40
92
41
93
const defaultOptions : PrettyLoggerOptions = {
42
94
showFiberId : true ,
43
95
showTime : true ,
44
96
showSpans : true ,
97
+ enableColors : true ,
45
98
} ;
46
99
47
- const createTimeString = ( date : Date ) => {
100
+ const createTimeString = ( colorService : ColorService , date : Date ) => {
48
101
const hoursText = date . getHours ( ) . toString ( ) . padStart ( 2 , "0" ) ;
49
102
const minutesText = date . getMinutes ( ) . toString ( ) . padStart ( 2 , "0" ) ;
50
103
const secondsText = date . getSeconds ( ) . toString ( ) . padStart ( 2 , "0" ) ;
51
- return `${ YELLOW } ${ hoursText } :${ minutesText } :${ secondsText } ${ RESET } ` ;
104
+ return colorService . yellow ( `${ hoursText } :${ minutesText } :${ secondsText } ` ) ;
52
105
} ;
53
106
54
107
const createCauseMessage = ( cause : Cause . Cause < unknown > ) => {
@@ -58,31 +111,40 @@ const createCauseMessage = (cause: Cause.Cause<unknown>) => {
58
111
return Cause . pretty ( cause ) ;
59
112
} ;
60
113
61
- const createLogLevelString = ( logLevel : LogLevel . LogLevel ) => {
62
- const logLevelColor = SEVERITY_TO_COLOR [ logLevel . _tag ] ;
63
- const logLevelText = logLevel . label . padEnd ( 5 , " " ) ;
64
- return `${ logLevelColor } ${ logLevelText } ${ RESET } ` ;
114
+ const createLogLevelString = (
115
+ colorService : ColorService ,
116
+ logLevel : LogLevel . LogLevel ,
117
+ ) => {
118
+ const logLevelColor = SEVERITY_TO_COLOR [ logLevel . _tag ] ( colorService ) ;
119
+ return logLevelColor ( logLevel . label . padEnd ( 5 , " " ) ) ;
65
120
} ;
66
121
67
- const messageText = ( message : unknown ) => {
122
+ const messageText = ( colorService : ColorService , message : unknown ) => {
68
123
if ( message === undefined ) {
69
- return ` ${ DIM } undefined${ RESET } ` ;
124
+ return colorService . dim ( " undefined" ) ;
70
125
} else if ( message === null ) {
71
- return ` ${ DIM } null${ RESET } ` ;
126
+ return colorService . dim ( " null" ) ;
72
127
} else if ( message === "" ) {
73
- return ` ${ DIM } <empty message>${ RESET } ` ;
128
+ return colorService . dim ( " <empty message>" ) ;
74
129
}
75
130
return serializeUnknown ( message ) ;
76
131
} ;
77
132
78
- const createText = ( message : unknown , cause : Cause . Cause < unknown > ) =>
133
+ const createText = (
134
+ colorService : ColorService ,
135
+ message : unknown ,
136
+ cause : Cause . Cause < unknown > ,
137
+ ) =>
79
138
pipe (
80
- [ createCauseMessage ( cause ) , messageText ( message ) ] ,
139
+ [ createCauseMessage ( cause ) , messageText ( colorService , message ) ] ,
81
140
ReadonlyArray . filter ( ( i ) => i !== "" ) ,
82
141
ReadonlyArray . join ( " " ) ,
83
142
) ;
84
143
85
- const createSpanText = ( spans : List . List < LogSpan . LogSpan > ) => {
144
+ const createSpanText = (
145
+ colorService : ColorService ,
146
+ spans : List . List < LogSpan . LogSpan > ,
147
+ ) => {
86
148
if ( List . isNil ( spans ) ) {
87
149
return "" ;
88
150
}
@@ -93,23 +155,30 @@ const createSpanText = (spans: List.List<LogSpan.LogSpan>) => {
93
155
( acc , span ) => `${ acc } -> ${ span . label } ` ,
94
156
) ;
95
157
96
- return ` ${ DIM } ${ ITALIC } ${ text } ${ RESET } ` ;
158
+ return ` ${ colorService . dimItalic ( text ) } ` ;
97
159
} ;
98
160
99
161
export const make = ( options ?: Partial < PrettyLoggerOptions > ) =>
100
162
Logger . make (
101
163
( { fiberId, logLevel, message, annotations, cause, date, spans } ) => {
102
164
const _options = { ...defaultOptions , ...options } ;
165
+ const colorService = _options . enableColors
166
+ ? enabledColorService
167
+ : disabledColorService ;
103
168
104
- const logLevelStr = createLogLevelString ( logLevel ) ;
105
- const timeText = _options . showTime ? `${ createTimeString ( date ) } ` : "" ;
169
+ const logLevelStr = createLogLevelString ( colorService , logLevel ) ;
170
+ const timeText = _options . showTime
171
+ ? `${ createTimeString ( colorService , date ) } `
172
+ : "" ;
106
173
const fiberText = _options . showFiberId
107
- ? ` ${ DIM } (Fiber ${ FiberId . threadName ( fiberId ) } )${ RESET } `
174
+ ? colorService . dim ( ` (Fiber ${ FiberId . threadName ( fiberId ) } ) ` )
108
175
: "" ;
109
176
110
- const text = createText ( message , cause ) ;
177
+ const text = createText ( colorService , message , cause ) ;
111
178
112
- const spansText = _options . showSpans ? createSpanText ( spans ) : "" ;
179
+ const spansText = _options . showSpans
180
+ ? createSpanText ( colorService , spans )
181
+ : "" ;
113
182
114
183
console . log ( `${ timeText } ${ fiberText } ${ logLevelStr } ${ spansText } ${ text } ` ) ;
115
184
@@ -119,10 +188,12 @@ export const make = (options?: Partial<PrettyLoggerOptions>) =>
119
188
[ ] as string [ ] ,
120
189
( acc , v , k ) => [
121
190
...acc ,
122
- ` ${ WHITE } "${ k } "${ RESET } : ${ serializeUnknown ( v ) } `,
191
+ colorService . white ( ` "${ k } "` ) + ` : ${ serializeUnknown ( v ) } `,
123
192
] ,
124
193
) ;
125
- console . log ( `ᐉ ${ DIM } {${ RESET } ${ text . join ( ", " ) } ${ DIM } }${ RESET } ` ) ;
194
+ console . log (
195
+ `ᐉ ${ colorService . dim ( "{" ) } ${ text . join ( ", " ) } ${ colorService . dim ( "}" ) } ` ,
196
+ ) ;
126
197
}
127
198
} ,
128
199
) ;
0 commit comments