Implementing Logging

Liferay DXP 7.1 uses the Log4j logging framework, but it may be replaced in the future. It’s a best practice to use Simple Logging Facade for Java (SLF4J) to log messages in your modules and traditional plugins. SLF4J is already integrated into Liferay DXP, so you can focus on logging messages.

Here’s how to use SLF4J to log messages in a class:

  1. Add a private static SLF4J Logger field.

    private static Logger _logger;
    
  2. Instantiate the logger.

    _logger = LoggerFactory.getLogger(this.getClass().getName());
    
  3. Throughout your class, log messages where noteworthy things happen.

    For example,

    _logger.debug("...");
    _logger.warn("...");
    _logger.error("...");
    ...
    

    Use Logger methods appropriate for each message:

    • debug: Event and application information helpful for debugging.
    • error: Normal errors. This is the least verbose message level.
    • info: High level events.
    • trace: Provides more information than debug. This is the most verbose message level.
    • warn: Information that might, but does not necessarily, indicate a problem.

Log verbosity should correlate with the log level set for the class or package. Make sure you provide additional information at log levels expected to be more verbose, such as info and debug.

You’re all set to add logging to your modules and traditional plugins.

Adjusting Module Logging

« Adjusting Module LoggingDeclaring Optional Import Package Requirements »
この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています