Java Logging (part 1)

very medium to enterprise application size prefers to have some sort of logging mechanisms, normally to address below perspectives of application we may need to use logging service.

  • Capture regular application usages
  • Debugging – when application presents an error
  • capturing development stage information
  • Application Behaviours – how an application will behave in case of different inputs and keep its records.
  • in some specific case: can be used as an easy tool integrated and tied to other third-party application to record performance related information.
  • recording timestamps for time sensitive application.

regardless of reasons, in most of our applications, we need logging, and Java provides a standard way to log events for further analysis.

java.util.logging

there is only one LogManager object that manages all the logging process in each individual application, so LogManager scope is application.

LogManager.getLogManager() // using static method

each Logger instances has its own name.

the process of logging in general is as below:

LogManager lm = LogManager.getLogManager();

Logger logger = lm.getLogger(Logger.GLOBAL_LOGGER_NAME);

logger.log(Level.INFO, "first logging message.");

but in real application and pracitical way we writing it like this …

  1. use it static then get the logger.
  2. then the logger instance will be accessible for entire class level.

import java.util.logging.Level;

import java.util.logging.LogManager;

import java.util.logging.Logger;

/** * *@author Navid_ */

public class PracticeStandardLogging {

static final Logger logger = LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME);

public static void main(String[] args) {

logger.log(Level.INFO, "first logging message.");

}

}

LEVEL

in setting the level (setLevel()) of every line in Logger, we can use below tables as reference,.

logger.setLevel(Level.INFO);

this line will capture every logger information that are higher and equals to this level.

Developer Tip:

Normally this value could be read from a properties file to make it dynamic in every regions. for example in production and Certification regions you can set that to INFO, to capture everything from INFO to SEVERE, and in lower regions like DEVELOPMENT and LAB to FINEST to capture everything up to the top.

I bolded ones that we are using frequently in every application.

LEVEL  |Numeric value| DESC 
-------------------------------
OFF    |Integer.MAX_VALUE|logger won't capture anything
SEVERE |1000         |Serious failure 
WARNING|900          |potential problem
INFO   |800          |General info - production regions
CONFIG |700          |Configuration info - normally in Certification Region or LAB
FINE   |500          |General Developer info
FINER  |400          |Detailed Developer info
FINEST |300          |Specialized Developer info - DEV regions
ALL    |Integer.MIN_VALUE |logger captures everthing - DEV regions

another set of methods instead ofusing regular logger.log() method and not passing the level.

    logger.severe("critical issue with the app ... ");
    logger.warning("potential problem");
    logger.info("general info");
    logger.config("configuration info");
    logger.fine("general dev info");
    logger.finer("detailed dev info");
    logger.finest("specialized dev info");

Another method in logger class that wecan use to customize the target and message, instead of using the regular log method and pass the control to the process, we can mention what class and what method mentioned what message.

    logger.logp(Level.SEVERE, "com.navid.Myclass", "myMethod1()",
"there is something is seriously wrong.");

and its output will be like below:

Nov 20, 2016 3:55:34 PM com.navid.Myclass myMethod1()
SEVERE: there is something is seriously wrong.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s