1
votes

When should i use below log levels? If there is any example that would be great.

  1. Trace Vs Debug
  2. Warn Vs Error Vs Fatal

WARN VS ERROR Vs FATAL

Will I need to use FATAL in my application code in first place?

I have never seen FATAL logging in any code still now in projects that i worked on till now.

I have read that, in case of FATAL program will end. If this is the case, I wonder how my log statement will execute.

Moreover, I think FATAL can not be used in the case of memory allocation as JVM will throw out of memory exception and exit the program. Hence developer can not log anything. If this is correct then where exactly i will use FATAL?

For ERROR and Warning: In catch block, if I do not have a alternate logic (for error condition) to perform then, I will go and log exception with Error level, the exception will be transformed into user specific and displayed in screen.

At the same time, the Warn will be used when we have alternate flow /path to the exception logic.

For Debug This will be to validate what and where the exception been thrown. What means the data that casued the error. Hence this can be used just before and after the complex logic of the code.

Please let me know if my understanding is correct

example:

class myLogLevel{
     void method1( int empId) 
    {
              log.trace("method1 starting") ;
           try{
               log.info("befor getting data from DB");
            log.debug("executing the value for emp id : " + empId );
                      //DBConnection and code here

          } catch (Exception1 e1) {
    log.warn("record not found. So assigning default value");
           // Code logic to assign default value
          }
          catch (Exception1 e1) {
             // Due to DB connection error. Connection nor established
log.error("DB connection not established");
          }
       log.trace("method1 ending") ;
        }
       }
1
any thoughts on this?sabtharishi

1 Answers

2
votes

In my past experiences, a somewhat common practice is

Always use DEBUG for your debugging purpose. I seldom see people use TRACE.

For stuff which is bad for the system but not necessarily cause problem (i.e. if it's an error depends on the calling context), use WARN; E.g. you could write a function which sometimes return NaN; but NaN might not be an error for the caller depends on your context.

For stuff that's surely an error somewhere in the system or in the caller input data; that definitely needs human involvement (i.e. someone needs to look at it from your production support team), uses ERROR. E.g. you want to write a person's record into database but found the primary key (firstname, lastname) is NULL.

For stuff that would cause the entire system to shut down or cause seriously impact on the system, use FATAL. That means people needs to look at it immediately. Examples include problems that cause startup failure; memory allocation failure; A messaging processing system failed to initialize the messaging layer; etc.

Hope the above helps.