@@ -8,7 +8,7 @@ import * as os from 'os'
8
8
import * as path from 'path'
9
9
import * as vscode from 'vscode'
10
10
import * as nls from 'vscode-nls'
11
- import { getLogger , Logger , LogLevel } from '.'
11
+ import { Logger , LogLevel } from '.'
12
12
import { extensionSettingsPrefix } from '../constants'
13
13
import { mkdir } from '../filesystem'
14
14
import { fileExists } from '../filesystemUtilities'
@@ -19,113 +19,54 @@ import { WinstonToolkitLogger } from './winstonToolkitLogger'
19
19
20
20
const localize = nls . loadMessageBundle ( )
21
21
22
- const LOG_RELATIVE_PATH : string = path . join ( 'Code' , 'logs' , 'aws_toolkit' )
22
+ const LOG_PATH = path . join ( getLogBasePath ( ) , 'Code' , 'logs' , 'aws_toolkit' , makeLogFilename ( ) )
23
23
const DEFAULT_LOG_LEVEL : LogLevel = 'info'
24
- const DEFAULT_LOG_NAME : string = makeDefaultLogName ( )
25
- const DEFAULT_OUTPUT_CHANNEL : vscode . OutputChannel = vscode . window . createOutputChannel ( 'AWS Toolkit Logs' )
24
+ const LOG_OUTPUT_CHANNEL : vscode . OutputChannel = vscode . window . createOutputChannel ( 'AWS Toolkit Logs' )
26
25
27
26
/**
28
- * logPath is not required (as Winston will work without a file path defined) but will output errors to stderr .
27
+ * Activate Logger functionality for the extension .
29
28
*/
30
- export interface LoggerParams {
31
- outputChannel ?: vscode . OutputChannel
32
- logPath ?: string
33
- logLevel ?: LogLevel
34
- }
29
+ export async function activate ( ) : Promise < void > {
30
+ const outputChannel = LOG_OUTPUT_CHANNEL
31
+ const logPath = LOG_PATH
32
+ const logLevel = getLogLevel ( )
35
33
36
- /**
37
- * @param params: LoggerParams
38
- * Creates the "default logger" (returnable here and through getLogger) using specified parameters or default values.
39
- * Initializing again will create a new default logger
40
- * --however, existing logger objects using the old default logger will be unaffected.
41
- */
42
- export async function initialize ( params ?: LoggerParams ) : Promise < Logger > {
43
- let outputChannel : vscode . OutputChannel | undefined
44
- let logPath : string | undefined
45
-
46
- if ( ! params ) {
47
- outputChannel = DEFAULT_OUTPUT_CHANNEL
48
- logPath = getDefaultLogPath ( )
49
-
50
- const logFolder = path . dirname ( logPath )
51
- if ( ! ( await fileExists ( logFolder ) ) ) {
52
- await mkdir ( logFolder , { recursive : true } )
53
- }
54
-
55
- // TODO : Determine log level here, then createLogger calls in this method can converge
56
- setLogger (
57
- createLogger ( {
58
- outputChannel,
59
- logPath
60
- } )
61
- )
62
- // only the default logger (with default params) gets a registered command
63
- // check list of registered commands to see if aws.viewLogs has already been registered.
64
- // if so, don't register again--this will cause an error visible to the user.
65
- for ( const command of await vscode . commands . getCommands ( true ) ) {
66
- if ( command === 'aws.viewLogs' ) {
67
- return getLogger ( )
68
- }
69
- }
70
- registerCommand ( {
71
- command : 'aws.viewLogs' ,
72
- callback : async ( ) => await vscode . window . showTextDocument ( vscode . Uri . file ( path . normalize ( logPath ! ) ) )
73
- } )
74
- } else {
75
- outputChannel = params . outputChannel
76
- logPath = params . logPath
34
+ await ensureLogFolderExists ( path . dirname ( logPath ) )
77
35
78
- setLogger ( createLogger ( params ) )
79
- }
36
+ setLogger ( makeLogger ( logLevel , logPath , outputChannel ) )
80
37
81
- if ( outputChannel && logPath ) {
82
- outputChannel . appendLine (
83
- localize ( 'AWS.log.fileLocation' , 'Error logs for this session are permanently stored in {0}' , logPath )
84
- )
85
- }
38
+ await registerLoggerCommands ( )
86
39
87
- return getLogger ( )
40
+ outputChannel . appendLine (
41
+ localize ( 'AWS.log.fileLocation' , 'Error logs for this session are permanently stored in {0}' , logPath )
42
+ )
88
43
}
89
44
90
- /**
91
- * @param params: LoggerParams--nothing is required, but a LogPath is highly recommended so Winston doesn't throw errors
92
- *
93
- * Outputs a logger object that isn't stored anywhere--it's up to the caller to keep track of this.
94
- */
95
- export function createLogger ( params : LoggerParams ) : Logger {
96
- // TODO : log level should be a concern of the caller
97
- let level : LogLevel
98
- if ( params . logLevel ) {
99
- level = params . logLevel
100
- } else {
101
- const configuration : SettingsConfiguration = new DefaultSettingsConfiguration ( extensionSettingsPrefix )
102
- const setLevel = configuration . readSetting < string > ( 'logLevel' )
103
- level = setLevel ? ( setLevel as LogLevel ) : DEFAULT_LOG_LEVEL
104
- }
45
+ export function makeLogger ( logLevel : LogLevel , logPath : string , outputChannel : vscode . OutputChannel ) : Logger {
46
+ const logger = new WinstonToolkitLogger ( logLevel )
47
+ logger . logToFile ( logPath )
48
+ logger . logToOutputChannel ( outputChannel )
105
49
106
- const logger = new WinstonToolkitLogger ( level )
107
- if ( params . logPath ) {
108
- logger . logToFile ( params . logPath )
109
- }
50
+ return logger
51
+ }
110
52
111
- if ( params . outputChannel ) {
112
- logger . logToOutputChannel ( params . outputChannel )
113
- }
53
+ function getLogLevel ( ) : LogLevel {
54
+ const configuration : SettingsConfiguration = new DefaultSettingsConfiguration ( extensionSettingsPrefix )
114
55
115
- return logger
56
+ return configuration . readSetting < LogLevel > ( 'logLevel' , DEFAULT_LOG_LEVEL )
116
57
}
117
58
118
- function getDefaultLogPath ( ) : string {
59
+ function getLogBasePath ( ) : string {
119
60
if ( os . platform ( ) === 'win32' ) {
120
- return path . join ( os . homedir ( ) , 'AppData' , 'Roaming' , LOG_RELATIVE_PATH , DEFAULT_LOG_NAME )
61
+ return path . join ( os . homedir ( ) , 'AppData' , 'Roaming' )
121
62
} else if ( os . platform ( ) === 'darwin' ) {
122
- return path . join ( os . homedir ( ) , 'Library' , 'Application Support' , LOG_RELATIVE_PATH , DEFAULT_LOG_NAME )
63
+ return path . join ( os . homedir ( ) , 'Library' , 'Application Support' )
123
64
} else {
124
- return path . join ( os . homedir ( ) , '.config' , LOG_RELATIVE_PATH , DEFAULT_LOG_NAME )
65
+ return path . join ( os . homedir ( ) , '.config' )
125
66
}
126
67
}
127
68
128
- function makeDefaultLogName ( ) : string {
69
+ function makeLogFilename ( ) : string {
129
70
const m = moment ( )
130
71
const date = m . format ( 'YYYYMMDD' )
131
72
const time = m . format ( 'HHmmss' )
@@ -134,3 +75,16 @@ function makeDefaultLogName(): string {
134
75
135
76
return `aws_toolkit_${ datetime } .log`
136
77
}
78
+
79
+ async function ensureLogFolderExists ( logFolder : string ) : Promise < void > {
80
+ if ( ! ( await fileExists ( logFolder ) ) ) {
81
+ await mkdir ( logFolder , { recursive : true } )
82
+ }
83
+ }
84
+
85
+ async function registerLoggerCommands ( ) : Promise < void > {
86
+ registerCommand ( {
87
+ command : 'aws.viewLogs' ,
88
+ callback : async ( ) => await vscode . window . showTextDocument ( vscode . Uri . file ( path . normalize ( LOG_PATH ) ) )
89
+ } )
90
+ }
0 commit comments