• Logging In Liferay 7 Using Loggers

    Logging In Liferay 7 Using Loggers

    Logging In Liferay 7, Liferay Uses Log4J Library for Logging Java Classes, In this blog lets see the Liferay Log Configuration  In Liferay, Liferay Portlet Logging and Log Levels in detail.

    Before Going Into The actual Logging and Loggers in liferay, Lets answer few questions on Logging.

    What is a Log?
    In our Java Code we were using System.out.println("message"); to log (print) the information end error messages, These logs will be printed in the Console and log files, These logs are used to debug the code.

    Why do need a Logging System using Loggers?
    The Logs printed using the System.out.println (sysout) are not more informative, when code snippet System.out.println("message"); gets executed, the only thing it will print on the console screen and log file is message. No other informations.
    There will be NO information about
    1. From Which Class the Log is Generated
    2. At What Time The Log Is Generated
    3. What is the Severity of the message

    If We Use the Loggers to Log the Messages In Java Classes, The log message will holds the information, From Which Class The Log Is Generated, At What Time The Log Is Generated and What is the Severity of the message. Which is not available in sysout.
    Logging In Liferay 7 Using Loggers

    What are the Log Levels Supported By Liferay?
    There are 6 log Levels supported by Liferay, you can decide the level based on the log severity.
    INFO : To Log the useful  information.
    DEBUG : To Log Debug information, use it to debug the code.
    WARN : To Log the warning messages, which are not critical
    ERROR : To Log the error message which blocks a behavior (functionality) being
    FATAL : To Log the messages which can be a severe or vital issue that harms the complete system.
    TRACE : To Log the exception call traces of the code

    Enabling  And Disabling Log Level For A Class
    We can define and configure the Log levels for Each and Every Java Class In the Server Administration section of Control Panel.

    Control Panel  >> Configuration >> Server Administration >> Log Levels

    Liferay 7 Loggers Server Administration

    If we Set The Log Level to ALL, Logs with all Levels will be printed.
    If we Set The Log Level to OFF, No Logs will be printed at all for the specified class.
    If we Set The Log Level to To Info, Only Logs printed using Info Level will be printed, Other Log Levels In Same Class Will not be printed.

    Creating a Logger In Liferay 7
    You can define a Logger for a Class using the below code snippet.
    import com.liferay.portal.kernel.log.LogFactoryUtil;
    private static Log _log = LogFactoryUtil.getLog(LoggerPortlet.class);

    To Log The Messages With Different Log Levels.
    _log.info("Use INFO To Log Useful Information");
    _log.warn("Use WARN To Log Warning Message");
    _log.error("Use ERROR To Log Error Message ");
    _log.debug("Use DEBUG To Log Debug Message ");
    _log.fatal("Use FATAL To Log Fatal Message");
    _log.trace("Use TRACE To Log Trace Message of the exception");

    How To Check The Log Level of the class
    A Simple _log.info("Use INFO To Log Useful Information"); Will print the message even if you disable(turn OFF) the Log Level for the class in the server administration. As A Best Practice, we must check the Log Level before Logging a message in that level.
     _log.info("This Messge Will be printed only if the info log level is enbled"
     + " for the class 'LoggerExampleClass' from Liferay Server Administration");

    To Check The Log Other Levels of a Logger.

    Liferay 7 Logging Example
    Summing up all the parts into a Java class for an easy reference, this Liferay Portlet Example can give you clear idea of the Logging System in Liferay, Use the below code snippet to know more about the loggers in liferay.


    1. This comment has been removed by the author.

    2. How to create separate log file for each module ?