Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
huseyindereli
Active Contributor


Logging is one of the fundamental concepts of software development. It helps developers to solve different types of problems by keeping track of information that is processed during the execution of a program. ABAP development has its own tracing and logging features which are not used so frequently compared with other development environments; however when it comes to java applications, logging is an essential way of collecting information at runtime.


While developers use logging tools for debugging purposes, they also create a monitoring mechanism for a specific application using it. By the help of this mechanism, users can easily access various messages generated at runtime in order to diagnose problems that they are facing. Not only it gives users information to identify the problem, but also it guides them through the process of understanding which steps should be followed next to be able to solve it.

It works by leaving trace messages at some of the key points of our application to get clues about what's going on behind the scenes while the application is running. Whenever something goes wrong, these messages help us to figure out what happened and also depending on the trace message that is logged, we might need to take some actions.

http://www.slf4j.org/images/logos/slf4j-logo.jpg

To take the advantage of logging concept for Hana Cloud Platform applications is pretty straightforward. I will develop a sample java application to show you how to leave some traces easily (create logs). First of all, HCP supports Simple Logging Facade for Java (SLF4J) for this purpose. It writes all the logs to the default trace file of the server and using HCP cockpit you can display these logs anytime.


Cloud applications can directly access SLF4J API without adding any references or packaging the library in the application archive. So we don't need to add any library to be able to use it.









Creating A Sample App

You've got eclipse and hana tools installed. Create a new project by selecting "Dynamic Web Project". Give it a name. In my case it is "Log4HCP". After project is created, right click -> new -> servlet. Give it a name and provide a package name. Respectively Log4HCPServlet and com.yourdomain.log4hcp. We're ready to add logging specific bits of the code.

Add Imports


  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;







Add Logger


  // Logger Class Definition
  private final Logger MYLOGGER = LoggerFactory.getLogger(this.getClass());






Create Log Messages


  // LOGS at different levels
  MYLOGGER.info("INFO Log");
  MYLOGGER.warn("WARN Log");
  MYLOGGER.trace("TRACE Log");
  MYLOGGER.error("ERROR Log");
  MYLOGGER.debug("DEBUG Log");
response.getWriter().println("DONE");






After running this application on localhost, go to Server Logs. You can find it under the server node itself which is located inside the servers view. Or you can simply add view using Eclipse Menu Select Window -> Show View -> Other -> Server Logs. Then double click on default trace file, ljs_trace.log .



And you will just see a message like below at the bottom of the log file. Bold part of the message is inserted by our logger.


2015 12 26 21:57:27#+0200#ERROR#com.yourdomain.log4hcp.Log4HCPServlet##anonymous#http-bio-8080-exec-8#na##dev_default#ERROR Log|









The only message we could find is the error message. This is because of the log level. It is set to errors only.

Let's change it on localhost. Go to "Servers" view. Double click on started local server (JAVA EE 6 Web Profile Server). Select "Loggers" tab below. Type Log4HCP to the filter input box in order to find our class. You will see that Level is set to ERROR. Let's change it to ALL and run the application again.

Now all our logs are getting displayed.




2015 12 26 22:06:37#+0200#INFO#com.yourdomain.log4hcp.Log4HCPServlet##anonymous#http-bio-8080-exec-6#na##dev_default#INFO Log|


2015 12 26 22:06:37#+0200#WARN#com.yourdomain.log4hcp.Log4HCPServlet##anonymous#http-bio-8080-exec-6#na##dev_default#WARN Log|


2015 12 26 22:06:37#+0200#TRACE#com.yourdomain.log4hcp.Log4HCPServlet##anonymous#http-bio-8080-exec-6#na##dev_default#TRACE Log|


2015 12 26 22:06:37#+0200#ERROR#com.yourdomain.log4hcp.Log4HCPServlet##anonymous#http-bio-8080-exec-6#na##dev_default#ERROR Log|


2015 12 26 22:06:37#+0200#DEBUG#com.yourdomain.log4hcp.Log4HCPServlet##anonymous#http-bio-8080-exec-6#na##dev_default#DEBUG Log|








So depending on the log level specified, different log types might appear or not.

Here is the definitions of the log levels on SAP HANA Cloud Documentation.

LevelDescription

ALL

This level has the lowest possible rank and is intended to turn on all logging.

TRACE

This level designates finer-grained informational events than DEBUG.

DEBUG

This level designates fine-grained informational events that are most useful to debug an application.

INFO

This level designates informational messages that highlight the progress of the application at coarse-grained level.

WARN

This level designates potentially harmful situations.

ERROR

This level designates error events that might still allow the application to continue running.

OFF

This level has the highest possible rank and is intended to turn off logging.

It's also good to bear in mind that the overhead of writing a message to the log file comes with the necessity of using it carefully. Having lots of messages for any kind of purpose might cause performance issues. Therefore we shouldn't use low rank levels like ALL unless we really need it.

It's time to try it HANA Cloud Platform. Since our application is already running on localhost, it so easy to deploy it on HCP. After starting the application, you can run it using the application url. Now navigate to the "Logging" section of our java application.

When you open the default trace, you will only get the error message as we experienced on localhost before changing the log level to ALL.

Let's change the log level. At the header of the Default Traces table, you will see a "Configure Loggers" button. After clicking that button, a popup will appear. Just filter out Logger Name which we've created using the name of the class "com.yourdomain.log4hcp" and then set loggers to the ALL level.

Run your application again and you will see all the messages logged and available to analyse. There are 2 error messages. One of them is logged at the first run. Of course you will spot the difference by looking at the time field of the table.


Enabling the SQL Trace

Using this approach might help you in various ways. For example you can trace your sql statements by finding and changing the log level of com.sap.core.persistence.sql.trace. You may find more information about sql trace on HCP here.


Log Retention

I think it is important to keep in mind that logs are only kept for 7 days.


Log records are only kept on the central log server for 7 days. To save a copy of them, you can download them using any of the SAP HANA Cloud Platform tools (Eclipse IDE, console client, cockpit). This rule applies to all kind of log files.







Conclusion

As you figured out, it is so easy to utilise logging features of Java Applications. It might help you at some of the circumstances, especially when you're running applications on cloud :smile:

A few things I want to mention;

  • Always consider performance issues that might occur (for example avoid logging big chunks of data)
  • Don't log confidential data like passwords
  • Don't log the time because it is already being recorded at each step
  • Get yourself a good logging pattern that makes it obvious to identify the problem
  • You can create a central method to log information which gives you the flexibility to add validation or additional parameter without changing all the lines
  • Log critical steps like db connections, http connections, runtime generated urls, user inputs, data received from external systems, data sent to external systems
  • These are only general points that I can think of right now but I'm sure you will find plenty of others for your needs

If you want to learn more about logging, check the help documentation.

If you need to dive into SLF4J API, you can do it here.

6 Comments
Labels in this area