How to create a LogFile and path with LoggerFactory

1.4k Views Asked by At

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);

1

There are 1 best solutions below

2
On

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: