Skip to content

[draft] Add a separate file for plugin logs #8253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/runConfigurations/flutter_intellij__runIde_.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions flutter-idea/src/io/flutter/FlutterInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.flutter.devtools.RemainingDevToolsViewFactory;
import io.flutter.editor.FlutterSaveActionsManager;
import io.flutter.logging.FlutterConsoleLogManager;
import io.flutter.logging.PluginLogger;
import io.flutter.module.FlutterModuleBuilder;
import io.flutter.pub.PubRoot;
import io.flutter.pub.PubRoots;
Expand Down Expand Up @@ -74,8 +75,7 @@
* may run when a project is being imported.
*/
public class FlutterInitializer extends FlutterProjectActivity {
private static final @NotNull Logger LOG = Logger.getInstance(FlutterInitializer.class);

private static final @NotNull Logger LOG = PluginLogger.createLogger(FlutterInitializer.class);
private boolean toolWindowsInitialized = false;

private boolean busSubscribed = false;
Expand All @@ -84,12 +84,15 @@ public class FlutterInitializer extends FlutterProjectActivity {

@Override
public void executeProjectStartup(@NotNull Project project) {
LOG.info("starting executeProjectStartup");
// Disable the 'Migrate Project to Gradle' notification.
FlutterUtils.disableGradleProjectMigrationNotification(project);

LOG.info("Starting watching for devices");
// Start watching for devices.
DeviceService.getInstance(project);

LOG.info("Starting a DevTools server");
// Start a DevTools server
DevToolsService.getInstance(project);

Expand All @@ -108,6 +111,7 @@ public void executeProjectStartup(@NotNull Project project) {
continue;
}

LOG.info("This is a Flutter module");
// Ensure SDKs are configured; needed for clean module import.
FlutterModuleUtils.enableDartSDK(module);

Expand Down
52 changes: 52 additions & 0 deletions flutter-idea/src/io/flutter/logging/PluginLogHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2025 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.logging;

import com.intellij.openapi.application.PathManager;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class PluginLogHandler extends Handler {
private static final String LOG_FILE_NAME = "flutter.log";
public PluginLogHandler() {
}

@Override
public void publish(LogRecord record) {
// Write to a file
final String logPath = PathManager.getLogPath();
System.out.println("Log path: " + logPath);
File logFile = new File(logPath + File.separatorChar + LOG_FILE_NAME);

try {
if (!logFile.exists()) {
logFile.createNewFile(); // Create the file if it doesn't exist
System.out.println("Log file created: " + logFile.getAbsolutePath());
}
FileWriter writer = new FileWriter(logFile, true); // Append to the file

String message = record.getInstant().toString() + " " + record.getLevel() + ": " + record.getMessage() + System.lineSeparator();
writer.write(message); // Write the log message
writer.close(); // Close the writer
} catch (IOException e) {
System.err.println("Error creating or writing to the log file: " + e.getMessage());
}
}

@Override
public void flush() {
System.out.println("LOG FLUSH: ");
}

@Override
public void close() throws SecurityException {
System.out.println("in close of logging");
}
}
16 changes: 16 additions & 0 deletions flutter-idea/src/io/flutter/logging/PluginLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2025 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.logging;

import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NotNull;

public class PluginLogger {
public static Logger createLogger(@NotNull Class logClass) {
java.util.logging.Logger.getLogger(logClass.getName()).addHandler(new PluginLogHandler());
return Logger.getInstance(logClass.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import io.flutter.bazel.Workspace;
import io.flutter.bazel.WorkspaceCache;
import io.flutter.dart.DtdUtils;
import io.flutter.logging.PluginLogHandler;
import io.flutter.logging.PluginLogger;
import io.flutter.sdk.FlutterSdk;
import io.flutter.sdk.FlutterSdkUtil;
import io.flutter.utils.JsonUtils;
Expand All @@ -57,7 +59,7 @@
import java.util.concurrent.atomic.AtomicReference;

class DevToolsServerTask extends Task.Backgroundable {
private @NotNull static final Logger LOG = Logger.getInstance(DevToolsServerTask.class);
private @NotNull static final Logger LOG = PluginLogger.createLogger(DevToolsServerTask.class);
public @NotNull static final String LOCAL_DEVTOOLS_DIR = "flutter.local.devtools.dir";
public @NotNull static final String LOCAL_DEVTOOLS_ARGS = "flutter.local.devtools.args";
private @NotNull final Project project;
Expand All @@ -79,6 +81,7 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
try {
progressIndicator.setFraction(30);
progressIndicator.setText2("Init");
LOG.info("Here we are in DevTools run");

// If DevTools is not supported, start the daemon instead.
final boolean dartDevToolsSupported = dartSdkSupportsDartDevTools();
Expand Down