44 * -------------------------------------------------------------------------------------------- */
55
66'use strict' ;
7- import { ExtensionContext , OutputChannel , window } from 'vscode' ;
7+ import { ConfigurationChangeEvent , ExtensionContext , OutputChannel , window } from 'vscode' ;
8+ import { configuration } from './configuration/config' ;
9+ import { defaults , TraceLevel } from './configuration/defaults' ;
810import { constants } from './constants' ;
911
12+ const configTraceLevel = 'general.outputLevel' ;
13+
1014export class Logger {
11- static output : OutputChannel | undefined ;
15+ private static output : OutputChannel | undefined ;
16+ private static _level : TraceLevel = TraceLevel . Verbose ;
1217
13- static initialize ( _context : ExtensionContext ) {
18+ static initialize ( context : ExtensionContext , level ?: TraceLevel ) {
19+ this . level = level ?? this . _level ;
1420 this . output = this . output || window . createOutputChannel ( constants . extensionOutputChannelName ) ;
21+
22+ context . subscriptions . push ( configuration . onDidChange ( this . onConfigurationChanged , this ) ) ;
23+ }
24+
25+ private static onConfigurationChanged ( e : ConfigurationChangeEvent ) {
26+ if ( configuration . changed ( e , configTraceLevel ) ) {
27+ this . level = configuration . getParam ( configTraceLevel ) ?? defaults . general . outputLevel ;
28+ }
29+ }
30+
31+ static get level ( ) {
32+ return this . _level ;
33+ }
34+
35+ static set level ( value : TraceLevel ) {
36+ this . _level = value ;
37+
38+ if ( value === TraceLevel . Silent ) {
39+ if ( this . output != null ) {
40+ this . close ( ) ;
41+ }
42+ } else {
43+ this . output = this . output ?? window . createOutputChannel ( constants . extensionOutputChannelName ) ;
44+ }
1545 }
1646
1747 static enable ( ) {
@@ -22,7 +52,47 @@ export class Logger {
2252 this . output . show ( ) ;
2353 }
2454
55+ static debug ( message : string ) : void {
56+ if ( this . level !== TraceLevel . Debug ) {
57+ return ;
58+ }
59+
60+ if ( this . output !== undefined ) {
61+ this . output . appendLine ( message ) ;
62+ }
63+ }
64+
65+ static error ( err : Error , message ?: string , handled = false ) : void {
66+ console . error ( `${ handled ? 'H' : 'Unh' } andled Error: ${ err . stack || err . message || err . toString ( ) } ` ) ;
67+
68+ if ( this . level === TraceLevel . Silent ) {
69+ return ;
70+ }
71+
72+ if ( ! err ) {
73+ return ;
74+ }
75+
76+ if ( this . output !== undefined ) {
77+ this . output . appendLine (
78+ `${ handled ? 'H' : 'Unh' } andled Error: ${ err . stack || err . message || err . toString ( ) } ` ,
79+ ) ;
80+ }
81+ }
82+
2583 static log ( message : string ) : void {
84+ if ( this . level === TraceLevel . Verbose || this . level === TraceLevel . Debug ) {
85+ if ( this . output !== undefined ) {
86+ this . output . appendLine ( `${ this . level === TraceLevel . Debug ? this . timestamp : '' } ${ message } ` ) ;
87+ }
88+ }
89+ }
90+
91+ static warn ( message : string ) : void {
92+ if ( this . level === TraceLevel . Silent ) {
93+ return ;
94+ }
95+
2696 if ( this . output !== undefined ) {
2797 this . output . appendLine ( message ) ;
2898 }
@@ -35,10 +105,10 @@ export class Logger {
35105 }
36106 }
37107
38- static error ( err : Error , handled = false ) : void {
39- if ( ! err ) {
40- return ;
41- }
42- console . error ( ` ${ handled ? 'H' : 'Unh' } andled Error: ${ err . stack || err . message || err . toString ( ) } ` ) ;
108+ private static get timestamp ( ) : string {
109+ const now = new Date ( ) ;
110+ return `[ ${ now . toISOString ( ) . replace ( / T / , ' ' ) . replace ( / \. . + / , '' ) } : ${ `00 ${ now . getUTCMilliseconds ( ) } ` . slice (
111+ - 3 ,
112+ ) } ]` ;
43113 }
44114}
0 commit comments