1

EDIT This is my batchfile code to run a (.bat) File , and i try to convert the output of the bat into a Logfile ,but i dont want to set the name of the logfile automatically , for every bat i want to run , create a new logfile with a the name of the BatFile. Hope i could explain it well

   private final static Logger LOGGER = LoggerFactory.getLogger(BatchFile.class);

    public void BatchFile(File file) throws IOException, InterruptedException {
        ProcessBuilder processBuilder = new ProcessBuilder(file.getAbsolutePath());
        processBuilder.directory(file.getParentFile());
        Process process = processBuilder.start();
        Scanner scanner = new Scanner(process.getInputStream());
        try (PrintStream out = new PrintStream(new FileOutputStream("C:\\temp\\my_log.log"))) {
            while (scanner.hasNextLine()) {
                String outputLine = scanner.nextLine();
                LOGGER.info(outputLine);
                out.print(outputLine);
            }

            scanner.close();
        }

        System.out.println(process.waitFor());
    }


}

i would like to know if someone had the same problem. I try to use LoggerFactory to create LogFile for my program, for example:

I want to take the output of System.out.printl into a LogFile. Or printing all Errors or messages from console into the LogFile. Is it even possible?

Thanks

 final Logger log = LoggerFactory.getLogger(NameOfClass.class);
KmanTheMan
  • 27
  • 5

1 Answers1

0

Yes this is possible and the purpose of any Logging Framework. I assume you are using Slf4j interfaces. Depending on the implementation have a look at the following getting started guides:

All of them have the option to write your Log Events to files:

markusw
  • 1,975
  • 16
  • 28
  • thanks for the answer but those are no help , they arent working for me , just one of those has an example for creating a file and this isnt working ... – KmanTheMan Dec 06 '18 at 12:37
  • Edited my post. But first you need to understand how a logging framework works in general, thats why you need to know the basics, that are described in the first links provided. – markusw Dec 06 '18 at 14:45