Search code examples
javaloggingibm-cloudwebsphere-liberty

JAVA Logging in BlueMix (Liberty for Java)


I have deployed a simple JAVA application on BlueMix (Liberty for Java) that logs messages out to the console using the JAVA Log4j library.

Here the content of the log4j.properties file:

 log4j.rootLogger = DEBUG, STDOUT
 log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
 log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
 log4j.appender.STDOUT.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

I cannot see these logs at somewhere. I tried STDOUT and I even tried the good old.

System.out.println(). 

But when I invoke this CLI command:

cf logs <myApp> --recent

I only see the deployment messages from Bluemix like:

2016-09-08T23:38:32.01+1000 [App/0] OUT[INFO] CWWKF0008I: Feature update completed in 10.583 seconds.
2016-09-08T23:38:32.01+1000 [App/0] OUT [AUDIT] CWWKF0011I: The server defaultServer is ready to run a smarter planet.

The other alternative is to log to a file such as:

log4j.appender.FILE.File=${log}/logs/logging.log 

But I cannot see/access the WebSphere log files either.

Can someone please let me know how I can manage logging on BlueMix for JAVA?


Solution

  • After much reading I finally found a solution that works.

    First of all if someone needs persistent logging, storing logs in a file is not best practice for the BlueMix environment because the files will get deleted as servers shut down or restart.
    A much better solution is to store the logs in a database such as MongoDB or CouchDB: http://logging.apache.org/log4j/2.x/log4j-nosql/index.html

    Here is an example setup for log4j.properties file:

    log4j.logger.MongoDB=INFO,MongoDB
    log4j.appender.MongoDB=org.log4mongo.MongoDbPatternLayoutAppender
    log4j.appender.MongoDB.Threshold=INFO
    log4j.appender.MongoDB.databaseName=<your DB name>
    log4j.appender.MongoDB.collectionName=<your collection name>
    log4j.appender.MongoDB.hostname=<your host IP>
    log4j.appender.MongoDB.port=<your host port>
    log4j.appender.MongoDB.layout=org.log4mongo.MongoDbPatternLayout
    

    The second solution for persistent logging (for max 24 hours) is to use a BlueMix add-on called Monitoring & Analytics. Here is a great video that will guide you through the setup process - https://www.youtube.com/watch?v=HPVd0poTSLc

    If one needs to log to the console, which can be read via the app console under logs in the BlueMix console or via CLI, the solution is to use the logger that comes with the JDK java.util.logging.Logger. One can set the log levels like that:

    import java.util.logging.Level;
    import java.util.logging.Logger;
    Logger logger = Logger.getLogger("MyCustomlogger");
    logger.logp(Level.INFO, "HelloWorld", "method", "INFO level message");
    logger.logp(Level.WARNING, "HelloWorld", "method", "WARNING level message");
    logger.logp(Level.SEVERE, "HelloWorld", "method", "SEVERE level message");
    logger.logp(Level.FINE, "HelloWorld", "method", "FINE level message");
    logger.logp(Level.FINER, "HelloWorld", "method", "FINER level message");
    logger.logp(Level.FINEST, "HelloWorld", "method", "FINEST level message");
    

    Now one can see the logs that will be trailed automatically via CLI

    cf logs <your app name>
    

    I left the log4j.properties in the class path and now it logs out the debug level as well.

    I hope that will help someone in the future.

    Happy coding!