@@ -15,7 +15,9 @@ export interface LogChannelProvider {
1515 readonly name : string ;
1616 createChannel ( name : string ) : LogChannel ;
1717 toLoggable ?( o : unknown ) : string | undefined ;
18- sanitize ?: ( key : string , value : any ) => any ;
18+
19+ sanitizeKeys ?: Set < string > ;
20+ sanitizer ?: ( key : string , value : unknown ) => unknown ;
1921}
2022
2123export interface LogChannel {
@@ -25,17 +27,21 @@ export interface LogChannel {
2527 show ?( preserveFocus ?: boolean ) : void ;
2628}
2729
28- const sanitizedKeys = new Set < string > ( [ 'accessToken' , 'password' , 'token' ] ) ;
29- const defaultSanitize = function ( key : string , value : any ) : any {
30- return sanitizedKeys . has ( key ) ? `<${ value } >` : value ;
31- } ;
30+ const defaultSanitizeKeys = [ 'accessToken' , 'password' , 'token' ] ;
3231
3332export const Logger = new ( class Logger {
3433 private output : LogChannel | undefined ;
35- private provider : LogChannelProvider | undefined ;
34+ private provider : RequireSome < LogChannelProvider , 'sanitizeKeys' > | undefined ;
3635
3736 configure ( provider : LogChannelProvider , logLevel : LogLevel , debugging : boolean = false ) {
38- this . provider = provider ;
37+ if ( provider . sanitizeKeys != null ) {
38+ for ( const key of defaultSanitizeKeys ) {
39+ provider . sanitizeKeys . add ( key ) ;
40+ }
41+ } else {
42+ provider . sanitizeKeys = new Set ( defaultSanitizeKeys ) ;
43+ }
44+ this . provider = provider as RequireSome < LogChannelProvider , 'sanitizeKeys' > ;
3945
4046 this . _isDebugging = debugging ;
4147 this . logLevel = logLevel ;
@@ -197,21 +203,30 @@ export const Logger = new (class Logger {
197203 this . output ?. show ?.( preserveFocus ) ;
198204 }
199205
200- toLoggable ( o : any , sanitize ?: ( ( key : string , value : any ) => any ) | undefined ) : string {
206+ toLoggable ( o : any , sanitizer ?: ( ( key : string , value : unknown ) => unknown ) | undefined ) : string {
201207 if ( typeof o !== 'object' ) return String ( o ) ;
202208
203- sanitize ??= this . provider ! . sanitize ?? defaultSanitize ;
204-
205209 if ( Array . isArray ( o ) ) {
206- return `[${ o . map ( i => this . toLoggable ( i , sanitize ) ) . join ( ', ' ) } ]` ;
210+ return `[${ o . map ( i => this . toLoggable ( i , sanitizer ) ) . join ( ', ' ) } ]` ;
207211 }
208212
209213 const loggable = this . provider ! . toLoggable ?.( o ) ;
210214 if ( loggable != null ) return loggable ;
211215
212216 try {
213- return JSON . stringify ( o , sanitize ) ;
217+ return JSON . stringify ( o , ( key : string , value : unknown ) : unknown => {
218+ if ( this . provider ! . sanitizeKeys . has ( key ) ) return `<${ key } >` ;
219+
220+ if ( sanitizer != null ) {
221+ value = sanitizer ( key , value ) ;
222+ }
223+ if ( this . provider ?. sanitizer != null ) {
224+ value = this . provider . sanitizer ( key , value ) ;
225+ }
226+ return value ;
227+ } ) ;
214228 } catch {
229+ debugger ;
215230 return '<error>' ;
216231 }
217232 }
0 commit comments