1

I'm writing a simple Spring-based web application and deploying it to Websphere Liberty 8.5.5.9; I've gotten past my deployment problems and the application seems to start (according to the Liberty console.log). However, I'm not seeing any console or log output. My application main class, which contains print statements in the main method, is:

@Configuration
public class UserSettingApplication  extends SpringBootServletInitializer implements WebApplicationInitializer {

    ServletContext servletContext;

    private static final LoggerUtils logger = new LoggerUtils( UserSettingApplication.class );

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        builder.sources(UserSettingApplication.class);
        return builder;
    }
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.addListener(RequestContextListener.class);
        this.servletContext=servletContext;
        super.onStartup(servletContext);
    }
    public static void main(String[] args) throws Exception {
        System.out.println( "Entering UserSettingApplication.main" );
        SpringApplicationBuilder applicationBuilder = new UserSettingApplication().configure(new SpringApplicationBuilder());
        applicationBuilder.run(args);
        System.out.println( "Entering UserSettingApplication.main" );
    }
    @Override
    protected WebApplicationContext run(SpringApplication application) {
        WebApplicationContext webApplicationContext = super.run(application);
        Environment env = webApplicationContext.getEnvironment();
        String sessionName = env.getProperty("server.session.cookie.name", "xplore-session-id");
        servletContext.getSessionCookieConfig().setName(sessionName);
        return webApplicationContext;
    }
    @Bean
    protected RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }
  @Bean
  public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {

      logger.info("Let's inspect the beans provided by Spring Boot:");

      String[] beanNames = ctx.getBeanDefinitionNames();
      Arrays.sort(beanNames);
      for (String beanName : beanNames) {
        logger.info(beanName);
      }
    };
  }
}

Shouldn't I be seeing the print statements in the main method in the WASLiberty console.log?

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
Mark Lavin
  • 1,002
  • 1
  • 15
  • 30

2 Answers2

1

Shouldn't I be seeing the print statements in the main method in the WASLiberty console.log?

You will not see any printouts from the main method as it is not executed in the Liberty. The main method is used for standalone apps started from the command line not run from the app server. Put your messages in the configure method like below and you will see it.

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        System.out.println("##############@@@@@@@@@@@@##############Starting app"); 
Gas
  • 17,601
  • 4
  • 46
  • 93
-2

System.out.println() will NOT write into files such as console.log.

Instead it will write into console such as commandline window or eclipse console.

You need to use loggers such as log4j or Java.util.logging for writing into files.

You can find more info here what-is-the-difference-between-java-logger-and-system-out-println

Sundararaj Govindasamy
  • 8,180
  • 5
  • 44
  • 77
  • I *thought* I've seen println output in ...//logs/console.log in the past. Also, I am writing to both stdout and to log files, so I'm still puzzled as to why I don't see my output. – Mark Lavin Apr 19 '18 at 22:47
  • 1
    This answer is not correct, all System.out.printlns go to the `message.log` in the Liberty and to the `console.log` if you started the server with the `run` command instead of `start`. So check your `message.log` file for these entries. – Gas Apr 20 '18 at 07:15
  • Downvoted because this is inaccurate and misleading. Please be aware you can delete your answer and get rep points back. – Nathan Hughes Jun 22 '18 at 14:08
  • @NathanHughes, Thanks for teaching, I am here to learn and this answer is one of the mistakes. Sometimes points are less important than a reminder :-) – Sundararaj Govindasamy Jun 22 '18 at 14:28