AppLogService.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.portal.service.util;

  35. import java.util.ArrayList;
  36. import java.util.Collection;
  37. import org.apache.logging.log4j.Level;
  38. import org.apache.logging.log4j.LogManager;
  39. import org.apache.logging.log4j.Logger;
  40. import org.apache.logging.log4j.core.LoggerContext;
  41. import org.apache.logging.log4j.util.Supplier;

  42. /**
  43.  * This class provides writing services in the application logs files
  44.  */
  45. public final class AppLogService
  46. {
  47.     // Constants
  48.     private static final String LOGGER_EVENTS = "lutece.event";
  49.     private static final String LOGGER_DEBUG = "lutece.debug";
  50.     private static final String LOGGER_ERRORS = "lutece.error";
  51.     private static Logger _loggerEvents = LogManager.getLogger( LOGGER_EVENTS );
  52.     private static Logger _loggerErrors = LogManager.getLogger( LOGGER_ERRORS );
  53.     private static Logger _loggerDebug = LogManager.getLogger( LOGGER_DEBUG );

  54.     /**
  55.      * Creates a new AppLogService object.
  56.      */
  57.     private AppLogService( )
  58.     {
  59.     }
  60.     // //////////////////////////////////////////////////////////////////////////
  61.     // Log4j wrappers

  62.     /**
  63.      * Tells if the logger accepts debug messages. If not it prevents to build consuming messages that will be ignored.
  64.      *
  65.      * @return True if the logger accepts debug messages, otherwise false.
  66.      */
  67.     public static boolean isDebugEnabled( )
  68.     {
  69.         return _loggerDebug.isDebugEnabled( );
  70.     }

  71.     /**
  72.      * Log a message object with the DEBUG level. It is logged in application.log
  73.      *
  74.      * @param objToLog
  75.      *            the message object to log
  76.      */
  77.     public static void debug( Object objToLog )
  78.     {
  79.         _loggerDebug.debug( objToLog );
  80.     }

  81.     /**
  82.      * Logs a message with parameters at the {@link Level#DEBUG DEBUG} level.
  83.      *
  84.      * To improve performance, do not use String concatenation such as : AppLogService.error( "my message with param1 " + param1 + " and " + param2, myexception
  85.      * ); Recommended use : AppLogService.error( "my message with param1 {} and param2 {}", param1, param2, myexception );
  86.      *
  87.      * @param message
  88.      *            the message to log; the format depends on the message factory.
  89.      * @param params
  90.      *            parameters to the message.
  91.      * @see Logger##getMessageFactory()
  92.      */
  93.     public static void debug( String message, Object... params )
  94.     {
  95.         _loggerDebug.debug( message, params );
  96.     }

  97.     /**
  98.      * Logs a message with parameters which are only to be constructed if the logging level is the {@link Level#DEBUG DEBUG} level.
  99.      *
  100.      * @param message
  101.      *            the message to log; the format depends on the message factory.
  102.      * @param paramSuppliers
  103.      *            An array of functions, which when called, produce the desired log message parameters.
  104.      */
  105.     public static void debug( String message, Supplier<?>... paramSuppliers )
  106.     {
  107.         _loggerDebug.debug( message, paramSuppliers );
  108.     }

  109.     /**
  110.      * Tells if the logger accepts debug messages. If not it prevents to build consuming messages that will be ignored.
  111.      *
  112.      * @param strLogger
  113.      *            The Logger name
  114.      * @return True if the logger accepts debug messages, otherwise false.
  115.      */
  116.     public static boolean isDebugEnabled( String strLogger )
  117.     {
  118.         Logger logger = LogManager.getLogger( strLogger );

  119.         return logger.isDebugEnabled( );
  120.     }

  121.     /**
  122.      * Log a message object with the ERROR Level. It is logged in error.log
  123.      *
  124.      * @param objToLog
  125.      *            the message object to log
  126.      */
  127.     public static void error( Object objToLog )
  128.     {
  129.         _loggerErrors.error( objToLog );
  130.     }

  131.     /**
  132.      * Log a message object with the ERROR level including the stack trace of the Throwable t passed as parameter. It is logged in error.log
  133.      *
  134.      * @param message
  135.      *            the message object to log
  136.      * @param t
  137.      *            the exception to log, including its stack trace
  138.      */
  139.     public static void error( Object message, Throwable t )
  140.     {
  141.         _loggerErrors.error( message, t );
  142.     }

  143.     /**
  144.      * Logs a message with parameters at the {@link Level#ERROR ERROR} level.
  145.      *
  146.      * To improve performance, do not use String concatenation such as : AppLogService.error( "my message with param1 " + param1 + " and " + param2,
  147.      * myexception); Recommended use : AppLogService.error( "my message with param1 {} and param2 {}", param1, param2, myexception );
  148.      *
  149.      * @param message
  150.      *            the message to log; the format depends on the message factory.
  151.      * @param params
  152.      *            parameters to the message.
  153.      * @see Logger#getMessageFactory()
  154.      */
  155.     public static void error( String message, Object... params )
  156.     {

  157.         _loggerErrors.error( message, params );
  158.     }

  159.     /**
  160.      * Logs a message with parameters which are only to be constructed if the logging level is the {@link Level#ERROR ERROR} level.
  161.      *
  162.      * @param message
  163.      *            the message to log; the format depends on the message factory.
  164.      * @param paramSuppliers
  165.      *            An array of functions, which when called, produce the desired log message parameters.
  166.      */
  167.     public static void error( String message, Supplier<?>... paramSuppliers )
  168.     {

  169.         _loggerErrors.error( message, paramSuppliers );
  170.     }

  171.     /**
  172.      * Tells if the logger accepts error messages. If not it prevents to build consuming messages that will be ignored.
  173.      *
  174.      * @return True if the logger accepts error messages, otherwise false.
  175.      */
  176.     public static boolean isErrorEnabled( )
  177.     {
  178.         return _loggerErrors.isErrorEnabled( );
  179.     }

  180.     /**
  181.      * Log a message object with the INFO Level in application.log
  182.      *
  183.      * @param objToLog
  184.      *            the message object to log
  185.      */
  186.     public static void info( Object objToLog )
  187.     {
  188.         _loggerEvents.info( objToLog );

  189.     }

  190.     /**
  191.      * Logs a message with parameters at the {@link Level#INFO INFO} level.
  192.      *
  193.      * To improve performance, do not use String concatenation such as : AppLogService.error( "my message with param1 " + param1 + " and " + param2, myexception
  194.      * ); Recommended use : AppLogService.error( "my message with param1 {} and param2 {}", param1, param2, myexception );
  195.      *
  196.      * @param message
  197.      *            the message to log; the format depends on the message factory.
  198.      * @param params
  199.      *            parameters to the message.
  200.      * @see Logger##getMessageFactory()
  201.      */
  202.     public static void info( String message, Object... params )
  203.     {
  204.         _loggerEvents.info( message, params );
  205.     }

  206.     /**
  207.      * Logs a message with parameters which are only to be constructed if the logging level is the {@link Level#INFO INFO} level.
  208.      *
  209.      * @param message
  210.      *            the message to log; the format depends on the message factory.
  211.      * @param paramSuppliers
  212.      *            An array of functions, which when called, produce the desired log message parameters.
  213.      */
  214.     public static void info( String message, Supplier<?>... paramSuppliers )
  215.     {
  216.         _loggerEvents.info( message, paramSuppliers );
  217.     }

  218.     /**
  219.      * Tells if the logger accepts info messages. If not it prevents to build consuming messages that will be ignored.
  220.      *
  221.      * @return True if the logger accepts info messages, otherwise false.
  222.      */
  223.     public static boolean isInfoEnabled( )
  224.     {
  225.         return _loggerEvents.isInfoEnabled( );
  226.     }
  227.    
  228.     /**
  229.      * Gets the all the loggers.
  230.      *
  231.      * @return the all the loggers
  232.      */
  233.     public static Collection<LoggerInfo> getLoggersInfo( )
  234.     {
  235.         Collection<LoggerInfo> allLoggersInfo = new ArrayList<>( );
  236.         Collection<org.apache.logging.log4j.core.Logger> allLoggers = LoggerContext.getContext( ).getLoggers( );
  237.         LoggerContext logContext = LoggerContext.getContext( );
  238.         for ( org.apache.logging.log4j.core.Logger logger : allLoggers )
  239.         {
  240.             LoggerInfo log = new LoggerInfo( );
  241.             log.setName( logger.getName( ) );
  242.             log.setLevel( logger.getLevel( ).name( ) );
  243.             log.setPath( logContext.getConfigLocation( ).getPath( ) );
  244.             allLoggersInfo.add( log );
  245.         }
  246.        
  247.         return allLoggersInfo;
  248.     }
  249. }