Skip to main content

logging

What is logging, what is the purpose of logging?

Logging is all about tracing the flow of execution of the application.

There are many reasons why we need to implement logging in an application.

1. The developers inorder to debug the program they need embedded intermediate logging statements within the program, so that he can easily trace back which parts of the code is not working to fix it.

2. any errors that are creeped up during the execution of the application in the production env, can be traced or identified only through logging.

So from the above we can understand developers greatly rely on logging for identifying the errors/problems reported by the programs during their execution.

There are several third-party libraries are there for logging inter-mediate program output like

1. log4j

2. sl4j

4. common-logging

5. java logging api

Etc..

among the above the one most popular logging api is "log4j"

we are taking array of inputs and sort the array and return the sorted array

class ArrayHelper {

public int[] sort(int[] inArray) {

int swap = 0;

                for(int i=0;i<inArray.length;i++) {

for(int j=0;j<inArray.length-i;j++) {

if(inArray[j] < inArray[j+1]) {

swap = inArray[j];

inArray[j] = inArray[j+1];

inArray[j+1] = swap;

}

}

}

return inArray;

}

}

class Test {

public static void main(args[]) {

ArrayHelper arrayHelper = new ArrayHelper();

int[] sortedArray = arrayHelper.sort(new int[]{12,34, 28, 8, 45, 12});

sop(sortedArray);

}

}


What is logging, what is the purpose of logging?                                                                        

Logging is used at various different stages across the development through the delivery of the application.

#1 development = during the development time, the developers greatly rely on logging for debugging the logical errors that creeps up during the execution of the program. The developers writes intermediate log statements to generate the output while the program is executing, using which they can trace the problems in the program.

2. during the test, stage and production environments

in these environments we cannot run the application under debugging mode to troubleshoot or debug the errors. there could be some errors that might appear or our application run into during its execution within these environments, the only way we can see these errors is through logging. if we never see these errors, we never get a chance to fix them and can never improve the quality of the application.

Typical requirements of logging:

1. The log statements written should be turned on/off depends on the environment in which we are running the application.

2. The log output that is generated should be persisted, so that it can used at later point in time for further analysis and troubleshooting

3. In an application we might want to write the logging into multiple destinations like

1. whole application log destination

2. module level log

3. errors and infos in different log destinations

The log statements should be written to multiple destinations seemlessly. for eg.. on to console, file or to an database

4. The log statements written should be serialized and persisted in the order in which those are emitted/written in the code, even incase of multi-threaded and concurrent environments as well.

5. The log messages should be formatted nicely, so that those are readable


Comments

© 2020 The JavaDS Hub

Designed by Open Themes & Nahuatl.mx.