Search code examples
intellij-idea

Is there a way to find output of previous runs in IntelliJ?


Every time I run some program, the output of previous run of some other program seems to be overwritten by the new output. Is there a way to see the output of previously run programs?


Solution

  • There is not. In the Run/Debug configuration, on the log tab, you can turn on the option "Save console output to file". (You can set this under the "Defaults" configuration so that it always there by default. That however only applies to the current project. To set as a default for all future projects as well, from the Welcome screen (i.e. close all projects) go to Configure > Project Defaults > Run Configurations) Unfortunately, that output file is (also) overwritten on each new run. To the best of my knowledge, there is no way to add a timestamp to the file name. You can open a feature request asking they enhance this to retain the last x number of runs or allow a timestamp.

    In the meantime, there are two options you can use for a workaround.

    Option 1 - Logging Framework

    The first option is to use a logging framework. This won't include the start up information or any System.out/err that the console has (unless you use something like sysout-over-slf4j). On the same "Logs" tab in the Run/Debug configuration, you can set to have the log file shown in the console tool window. The nice thing is that IntelliJ IDEA only shows new output for that run. But the output from previous runs is still in the log file (assuming your logger is set up to append and not overwrite on new runs). When specifying the log name, you can use wild characters (to deal with things like a date stamp in the log). It use Ant fileset pattern notation. Click the help button on "Edit Log Files Aliases" dialog for more information. By default, IDEA only shows the latest log, unless you select the "Show all files coverable by pattern" on the "Edit Log Files Aliases".

    Option 2 - Use a Script

    Another option would be to use the "Save console file output to file" option. Then write an external script or Ant task that moves/renamed that output. (You could make it fancy so it only saves the last x runs, rolling the previous ones names.) Then in the run/debug configuration, use either the "Run External tool" or "Run Ant Target" option in the "Before Launch" section to run your script. (Again, you can set this on the default configurations so it is there automatically.) This way your script will run, renaming the file, before the new output is created and overwrites the old.