0

I am using test container in my project. I am getting stdout in console of each container by using:

container.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("container"))))

and i am getting output something like this:

[docker-java-stream--1578738495] INFO container - STDOUT:   at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
[docker-java-stream--1578738495] INFO container - STDOUT:   at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
[docker-java-stream--1578738495] INFO container - STDOUT:   at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
[docker-java-stream--1578738495] INFO container - STDOUT:   at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

but i am trying to add stdout into a separate file. I was trying something like this but it's not working.

            PrintStream o = new PrintStream(new File("file.txt"));
            PrintStream console = System.out;
            System.setOut(o);
            System.out.println((container.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("container")))));
            System.setOut(console);

I cannot use log4j because this project will be used as a dependency into another project and log4j might create conflict so i need some solution to print stdout into a file if possible. Thank you

pja
  • 3
  • 4

1 Answers1

0

You can use any Slf4j Logger as the parameter of the Slf4jConsumer constructor, for example, a logger that writes to a file:


Logger logger = LoggerFactory.getLogger(string);

// create fileAppender
// ...
logger.addAppender(fileAppender);

Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOGGER);
container.followOutput(logConsumer);

You can find more information regarding the programmatic configuration of Slf4j loggers and appenders in this SO answer.

Kevin Wittek
  • 1,369
  • 9
  • 26