5
5
*/
6
6
package io .flutter .logging ;
7
7
8
+ import com .intellij .openapi .application .ApplicationManager ;
8
9
import com .intellij .openapi .application .PathManager ;
10
+ import com .intellij .openapi .diagnostic .LogLevel ;
9
11
import com .intellij .openapi .diagnostic .Logger ;
12
+ import io .flutter .settings .FlutterSettings ;
10
13
import org .jetbrains .annotations .NotNull ;
11
14
12
15
import java .io .File ;
16
19
17
20
public class PluginLogger {
18
21
private static final String LOG_FILE_NAME = "flutter.log" ;
22
+
23
+ // This handler specifies the logging format and location.
19
24
private static final FileHandler fileHandler ;
25
+
20
26
static {
21
27
final String logPath = PathManager .getLogPath ();
22
28
try {
@@ -30,8 +36,25 @@ public class PluginLogger {
30
36
fileHandler .setFormatter (new SimpleFormatter ());
31
37
}
32
38
39
+ // Add the handler to the root logger so that all classes within `io.flutter` log to the file correctly. We can also update the log level
40
+ // of all classes at once by changing the root logger level.
41
+ private static final java .util .logging .Logger rootLogger = java .util .logging .Logger .getLogger ("io.flutter" );
42
+
43
+ static {
44
+ rootLogger .addHandler (fileHandler );
45
+ // This check prevents trying to access settings in test context.
46
+ if (ApplicationManager .getApplication () != null ) {
47
+ updateLogLevel ();
48
+ FlutterSettings .getInstance ().addListener (PluginLogger ::updateLogLevel );
49
+ }
50
+ }
51
+
52
+ private static void updateLogLevel () {
53
+ final Logger rootLoggerInstance = Logger .getInstance ("io.flutter" );
54
+ rootLoggerInstance .setLevel (FlutterSettings .getInstance ().isVerboseLogging () ? LogLevel .ALL : LogLevel .INFO );
55
+ }
56
+
33
57
public static @ NotNull Logger createLogger (@ NotNull Class <?> logClass ) {
34
- java .util .logging .Logger .getLogger (logClass .getName ()).addHandler (fileHandler );
35
58
return Logger .getInstance (logClass .getName ());
36
59
}
37
60
}
0 commit comments