Class LoggerMethods


  • public final class LoggerMethods
    extends Object

    Contains one-liners for logging operations.

    The one-liners in this class make it easy to log INFO, WARNING, ALERT, TRACE, and DEBUG-level messages to Martini's underlying logging engine. The default logger used by these one-liners is named Martini.

    • Constructor Summary

      Constructors 
      Constructor Description
      LoggerMethods()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void addFishTag​(String key, String value)
      Fish tagging is a technique in log4j wherein log statements are stamped with unique tags / information to allow for easier log tracing.
      static void clearFishTags()
      Clears all previously added fish tags.
      static String debug​(String message)
      Logs a DEBUG message to the underlying log engine of Martini, using a logger named Martini.
      static String debug​(String loggerName, String message)
      Logs a DEBUG message to the underlying log engine of Martini, using a logger named after loggerName.
      static String debugIfEnabled​(String message)
      Logs a DEBUG message to the underlying log engine of Martini, using a logger named Martini.
      static String debugIfEnabled​(String loggerName, String message)
      Logs a DEBUG message to the underlying log engine of Martini, using a logger named after loggerName.
      static String error​(String message)
      Logs an ERROR message to the underlying log engine of Martini, using a logger named Martini.
      static String error​(String loggerName, String message)
      Logs an ERROR message to the underlying log engine of Martini, using a logger named after loggerName.
      static String error​(String loggerName, String message, Throwable throwable)
      Logs an ERROR message to the underlying log engine of Martini, using a logger named after loggerName.
      static String error​(String message, Throwable throwable)
      Logs an ERROR message to the underlying log engine of Martini, using a logger named Martini.
      static String errorIfEnabled​(String message)
      Logs an ERROR message to the underlying log engine of Martini, using a logger named Martini.
      static String errorIfEnabled​(String loggerName, String message)
      Logs an ERROR message to the underlying log engine of Martini, using a logger named after loggerName.
      static String errorIfEnabled​(String loggerName, String message, Throwable throwable)
      Logs an ERROR message to the underlying log engine of Martini, using a logger named after loggerName.
      static String errorIfEnabled​(String message, Throwable throwable)
      Logs an ERROR message to the underlying log engine of Martini, using a logger named Martini.
      static String info​(String message)
      Logs an INFO message to the underlying log engine of Martini, using a logger named Martini.
      static String info​(String loggerName, String message)
      Logs an INFO message to the underlying log engine of Martini, using a logger named after loggerName.
      static String infoIfEnabled​(String message)
      Logs a INFO message to the underlying log engine of Martini, using a logger named Martini.
      static String infoIfEnabled​(String loggerName, String message)
      Logs an INFO message to the underlying log engine of Martini, using a logger named after loggerName.
      ApplicationLogger logger()
      Get a logged named groovy.
      static ApplicationLogger logger​(Class clazz)
      Get a logger named after a class.
      static ApplicationLogger logger​(String scriptName)
      Get a logger named after a script.
      static String logTo​(String message, String scriptName)
      Log a message using a logger named after scriptName.
      static String print​(String message)
      Print a message using {@link System#out#print(String)}.
      static String println​(String message)
      Print a message using {@link System#out#println(String)}.
      static void removeFishTag​(String key)
      Removes the fish tag denoted by the key specified
      static String trace​(String message)
      Logs a TRACE message to the underlying log engine of Martini, using a logger named Martini.
      static String trace​(String loggerName, String message)
      Logs a TRACE message to the underlying log engine of Martini, using a logger named after loggerName.
      static String traceIfEnabled​(String message)
      Logs a TRACE message to the underlying log engine of Martini, using a logger named Martini.
      static String traceIfEnabled​(String loggerName, String message)
      Logs a TRACE message to the underlying log engine of Martini, using a logger named after loggerName.
      static String warn​(String message)
      Logs a WARN message to the underlying log engine of Martini, using a logger named Martini.
      static String warn​(String loggerName, String message)
      Logs a WARN message to the underlying log engine of Martini, using a logger named after loggerName.
      static String warnIfEnabled​(String message)
      Logs a WARN message to the underlying log engine of Martini, using a logger named Martini.
      static String warnIfEnabled​(String loggerName, String message)
      Logs a WARN message to the underlying log engine of Martini, using a logger named after loggerName.
    • Constructor Detail

      • LoggerMethods

        LoggerMethods()
    • Method Detail

      • print

        public static String print​(String message)
        Print a message using {@link System#out#print(String)}.
        Parameters:
        message - the message
        Returns:
        the printed message
        Since:
        1.0
      • println

        public static String println​(String message)
        Print a message using {@link System#out#println(String)}.
        Parameters:
        message - the message
        Returns:
        the printed message
        Since:
        1.0
      • logger

        public static ApplicationLogger logger​(String scriptName)

        Get a logger named after a script. The logger can then be used by a script to log to Martini's underlying logging engine.

        Example usage:

         def logger = 'tld.organization.project.Script'.logger()
         logger.info( "This message will be logged at the INFO level" );
         

        Parameters:
        scriptName - the name of the logger
        Returns:
        the logger
        Since:
        1.0
      • logger

        public static ApplicationLogger logger​(Class clazz)

        Get a logger named after a class. The logger can then be used by a script to log to Martini's underlying logging engine.

        Example usage:

         def logger = getClass().logger()
         logger.info( "This message will be logged at the INFO level" );
         

        Parameters:
        clazz - the class to attach the logger to
        Returns:
        the logger
        Since:
        1.0
      • logger

        public ApplicationLogger logger()

        Get a logged named groovy. The logger can then be used by a script to log to Martini's underlying logging engine.

        Example usage:

         def logger = logger()
         logger.info( "This message will be logged at the INFO level" );
         

        Returns:
        the logger
        Since:
        1.0
      • logTo

        public static String logTo​(String message,
                                   String scriptName)

        Log a message using a logger named after scriptName.

        Example usage:

         'This message will be logged at the INFO level'.logTo( 'tld.organization.project.Script' )
         

        Parameters:
        message - the message to log
        scriptName - the name of the logger
        Returns:
        the message to log
        Since:
        1.0
      • trace

        public static String trace​(String message)

        Logs a TRACE message to the underlying log engine of Martini, using a logger named Martini.

        Example usage:

         'This message will be logged at the TRACE level'.trace();
         

        Parameters:
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • trace

        public static String trace​(@GloopParameter(allowNull=false)
                                   String loggerName,
                                   String message)

        Logs a TRACE message to the underlying log engine of Martini, using a logger named after loggerName.

        Example usage:

         'tld.organization.project.Script'.trace( 'This message will be logged at the TRACE level' )
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • traceIfEnabled

        public static String traceIfEnabled​(String message)

        Logs a TRACE message to the underlying log engine of Martini, using a logger named Martini. To succeed, TRACE messages must be enabled for said logger.

        Example usage:

         'This message will be logged at the TRACE level'.traceIfEnabled();
         

        Returns:
        the message to log
        Since:
        1.0
      • traceIfEnabled

        public static String traceIfEnabled​(@GloopParameter(allowNull=false)
                                            String loggerName,
                                            String message)

        Logs a TRACE message to the underlying log engine of Martini, using a logger named after loggerName. To succeed, TRACE messages must be enabled for said logger.

        Example usage:

         'tld.organization.project.Script'.traceIfEnabled( 'This message will be logged at the TRACE level' )
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • debug

        public static String debug​(String message)

        Logs a DEBUG message to the underlying log engine of Martini, using a logger named Martini.

        Example usage:

         'This message will be logged at the DEBUG level'.debug();
         

        Returns:
        the message to log
        Since:
        1.0
      • debug

        public static String debug​(@GloopParameter(allowNull=false)
                                   String loggerName,
                                   String message)

        Logs a DEBUG message to the underlying log engine of Martini, using a logger named after loggerName.

        Example usage:

         'tld.organization.project.Script'.debug( 'This message will be logged at the DEBUG level' )
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • debugIfEnabled

        public static String debugIfEnabled​(String message)

        Logs a DEBUG message to the underlying log engine of Martini, using a logger named Martini. To succeed, DEBUG messages must be enabled for said logger.

        Example usage:

         'This message will be logged at the DEBUG level'.debugIfEnabled();
         

        Returns:
        the message to log
        Since:
        1.0
      • debugIfEnabled

        public static String debugIfEnabled​(@GloopParameter(allowNull=false)
                                            String loggerName,
                                            String message)

        Logs a DEBUG message to the underlying log engine of Martini, using a logger named after loggerName. To succeed, DEBUG messages must be enabled for said logger.

        Example usage:

         'tld.organization.project.Script'.debugIfEnabled( 'This message will be logged at the DEBUG level' )
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • info

        public static String info​(String message)

        Logs an INFO message to the underlying log engine of Martini, using a logger named Martini.

        Example usage:

         'This message will be logged at the INFO level'.info();
         

        Parameters:
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • info

        public static String info​(@GloopParameter(allowNull=false)
                                  String loggerName,
                                  String message)

        Logs an INFO message to the underlying log engine of Martini, using a logger named after loggerName.

        Example usage:

         'tld.organization.project.Script'.info( 'This message will be logged at the INFO level' )
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • infoIfEnabled

        public static String infoIfEnabled​(String message)

        Logs a INFO message to the underlying log engine of Martini, using a logger named Martini. To succeed, INFO messages must be enabled for said logger.

        Example usage:

         'This message will be logged at the INFO level'.infoIfEnabled();
         

        Parameters:
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • infoIfEnabled

        public static String infoIfEnabled​(@GloopParameter(allowNull=false)
                                           String loggerName,
                                           String message)

        Logs an INFO message to the underlying log engine of Martini, using a logger named after loggerName. To succeed, INFO messages must be enabled for said logger.

        Example usage:

         'tld.organization.project.Script'.infoIfEnabled( 'This message will be logged at the INFO level' )
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • warn

        public static String warn​(String message)

        Logs a WARN message to the underlying log engine of Martini, using a logger named Martini.

        Example usage:

         'This message will be logged at the WARN level'.warn();
         

        Parameters:
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • warn

        public static String warn​(@GloopParameter(allowNull=false)
                                  String loggerName,
                                  String message)

        Logs a WARN message to the underlying log engine of Martini, using a logger named after loggerName.

        Example usage:

         'tld.organization.project.Script'.warn( 'This message will be logged at the WARN level' )
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • warnIfEnabled

        public static String warnIfEnabled​(String message)

        Logs a WARN message to the underlying log engine of Martini, using a logger named Martini. To succeed, WARN messages must be enabled for said logger.

        Example usage:

         'This message will be logged at the WARN level'.warnIfEnabled();
         

        Parameters:
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • warnIfEnabled

        public static String warnIfEnabled​(@GloopParameter(allowNull=false)
                                           String loggerName,
                                           String message)

        Logs a WARN message to the underlying log engine of Martini, using a logger named after loggerName. To succeed, WARN messages must be enabled for said logger.

        Example usage:

         'tld.organization.project.Script'.warnIfEnabled( 'This message will be logged at the WARN level' )
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • error

        public static String error​(String message)

        Logs an ERROR message to the underlying log engine of Martini, using a logger named Martini.

        Example usage:

         'This message will be logged at the ERROR level'.error();
         

        Parameters:
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • error

        public static String error​(@GloopParameter(allowNull=false)
                                   String loggerName,
                                   String message)

        Logs an ERROR message to the underlying log engine of Martini, using a logger named after loggerName.

        Example usage:

         'tld.organization.project.Script'.error( 'This message will be logged at the ERROR level' )
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • error

        public static String error​(String message,
                                   Throwable throwable)

        Logs an ERROR message to the underlying log engine of Martini, using a logger named Martini.

        Example usage:

         try {
             ...
         } catch ( Exception exception ) {
             'This message will be logged at the ERROR level'.error( 'Could not perform operation', exception );
         }
         

        Parameters:
        message - the message to log
        throwable - the throwable to log
        Returns:
        the message to log
        Since:
        1.0
      • error

        public static String error​(@GloopParameter(allowNull=false)
                                   String loggerName,
                                   String message,
                                   Throwable throwable)

        Logs an ERROR message to the underlying log engine of Martini, using a logger named after loggerName.

        Example usage:

         try {
             ...
         } catch ( Exception exception ) {
             'tld.organization.project.Script'.error( 'This message will be logged at the ERROR level', exception )
         }
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        throwable - the throwable to log
        Returns:
        the message to log
        Since:
        1.0
      • errorIfEnabled

        @GloopComment("Send an error message to the underlying log engine with category `Martini`, if error messages are enabled for the category")
        public static String errorIfEnabled​(@GloopParameter("Message to print to log error")
                                            String message)

        Logs an ERROR message to the underlying log engine of Martini, using a logger named Martini. To succeed, ERROR messages must be enabled for said logger.

        Example usage:

         'This message will be logged at the ERROR level'.errorIfEnabled();
         

        Parameters:
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • errorIfEnabled

        public static String errorIfEnabled​(@GloopParameter(allowNull=false)
                                            String loggerName,
                                            String message)

        Logs an ERROR message to the underlying log engine of Martini, using a logger named after loggerName. To succeed, ERROR messages must be enabled for said logger.

        Example usage:

         'tld.organization.project.Script'.errorIfEnabled( 'This message will be logged at the ERROR level' )
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        Returns:
        the message to log
        Since:
        1.0
      • errorIfEnabled

        public static String errorIfEnabled​(String message,
                                            Throwable throwable)

        Logs an ERROR message to the underlying log engine of Martini, using a logger named Martini. To succeed, ERROR messages must be enabled for said logger.

        Example usage:

          try {
              ...
          } catch ( Exception exception ) {
              'This message will be logged at the ERROR level'.errorIfEnabled( exception );
          }
         

        Parameters:
        message - the message to log
        throwable - the throwable to log
        Returns:
        the message to log
        Since:
        1.0
      • errorIfEnabled

        public static String errorIfEnabled​(@GloopParameter(allowNull=false)
                                            String loggerName,
                                            String message,
                                            Throwable throwable)

        Logs an ERROR message to the underlying log engine of Martini, using a logger named after loggerName. To succeed, ERROR messages must be enabled for said logger.

        Example usage:

         try {
             ...
         } catch ( Exception exception ) {
             'tld.organization.project.Script'.errorIfEnabled( 'This message will be logged at the ERROR level',
                                                               exception )
         }
         

        Parameters:
        loggerName - the name of the logger
        message - the message to log
        throwable - the throwable to log
        Returns:
        the message to log
        Since:
        1.0
      • addFishTag

        public static void addFishTag​(@GloopParameter(allowNull=false)
                                      String key,
                                      @GloopParameter(allowNull=false)
                                      String value)

        Fish tagging is a technique in log4j wherein log statements are stamped with unique tags / information to allow for easier log tracing. Once tags are added, the key must be included in the log pattern of the appender set in /conf/overrides/log4j2.properties file in the following format:

         ${service:the-name-of-your-key}
         

        Example below will stamp logs in the martini log file:

         appender.rolling.layout.pattern=%d{dd/MM/yy} ${service:the-name-of-your-key} %-5p [%c{1}] %m%n
         

        Parameters:
        key - key of the fish tag
        value - value of the fish tag
      • removeFishTag

        public static void removeFishTag​(@GloopParameter(allowNull=false)
                                         String key)
        Removes the fish tag denoted by the key specified
        Parameters:
        key - key of the fish tag
      • clearFishTags

        public static void clearFishTags()
        Clears all previously added fish tags.