Module xaos.core

Class LogUtils


  • public class LogUtils
    extends Object
    Utilities for logging messages.
    Author:
    claudio.rosati@esss.se
    • Method Detail

      • log

        public static void log​(Logger logger,
                               Level level,
                               String message)
        Log a message.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        message - The string message.
      • log

        public static void log​(Logger logger,
                               String className,
                               String methodName,
                               Level level,
                               String message)
        Log a message.
        Parameters:
        logger - The Logger to log the message.
        className - The name of the class to be logged.
        methodName - The name of the method to be logged.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        message - The string message. It can contains parameters.
        See Also:
        MessageFormat
      • log

        public static void log​(Logger logger,
                               Level level,
                               String message,
                               Object... parameters)
        Log a message.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        message - The string message. It can contains parameters.
        parameters - Array of parameters to the message.
        See Also:
        MessageFormat
      • log

        public static void log​(Logger logger,
                               String className,
                               String methodName,
                               Level level,
                               String message,
                               Object... parameters)
        Log a message.
        Parameters:
        logger - The Logger to log the message.
        className - The name of the class to be logged.
        methodName - The name of the method to be logged.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        message - The string message. It can contains parameters.
        parameters - Array of parameters to the message.
        See Also:
        MessageFormat
      • log

        public static void log​(Logger logger,
                               Level level,
                               Throwable thrown)
        Log a message, with associated Throwable information.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        thrown - Throwable associated with log message.
      • log

        public static void log​(Logger logger,
                               String className,
                               String methodName,
                               Level level,
                               Throwable thrown)
        Log a message, with associated Throwable information.
        Parameters:
        logger - The Logger to log the message.
        className - The name of the class to be logged.
        methodName - The name of the method to be logged.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        thrown - Throwable associated with log message.
      • log

        public static void log​(Logger logger,
                               Level level,
                               Throwable thrown,
                               String message)
        Log a message, with associated Throwable information.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        thrown - Throwable associated with log message.
        message - The string message.
      • log

        public static void log​(Logger logger,
                               String className,
                               String methodName,
                               Level level,
                               Throwable thrown,
                               String message)
        Log a message, with associated Throwable information.
        Parameters:
        logger - The Logger to log the message.
        className - The name of the class to be logged.
        methodName - The name of the method to be logged.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        thrown - Throwable associated with log message.
        message - The string message. It can contains parameters.
        See Also:
        MessageFormat
      • log

        public static void log​(Logger logger,
                               Level level,
                               Throwable thrown,
                               String message,
                               Object... parameters)
        Log a message, with associated Throwable information.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        thrown - Throwable associated with log message.
        message - The string message. It can contains parameters.
        parameters - Array of parameters to the message.
        See Also:
        MessageFormat
      • log

        public static void log​(Logger logger,
                               String className,
                               String methodName,
                               Level level,
                               Throwable thrown,
                               String message,
                               Object... parameters)
        Log a message, with associated Throwable information.
        Parameters:
        logger - The Logger to log the message.
        className - The name of the class to be logged.
        methodName - The name of the method to be logged.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        thrown - Throwable associated with log message.
        message - The string message. It can contains parameters.
        parameters - Array of parameters to the message.
        See Also:
        MessageFormat
      • logTime

        public static void logTime​(Logger logger,
                                   Level level,
                                   Runnable function,
                                   String startMessage,
                                   String endMessage,
                                   Object... args)
        Logs the time spent executing the given function.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        function - The function whose execution time must be measured and logged.
        startMessage - The start message to be logged. It can contains parameters, where parameter {0} will always be substituted by the time this method is called (i.e. before the given function is started). Parameter {1} indicates the first extra argument, parameter {2} the second one, and so on.
        endMessage - The message to be logged. It can contains parameters, where parameter {0} will always be substituted by the time spent running the given function in seconds (double value). If not used, the time spent will be appended to the message. Parameter {1} will be the start time, Parameter {2} will be the end time. Parameter {3} indicates the first extra argument, parameter {4} the second one, and so on.
        args - Arguments to be logged as parameters of messages.
        See Also:
        MessageFormat, Instant.toString()
      • logTime

        public static <T> T logTime​(Logger logger,
                                    Level level,
                                    Callable<T> function,
                                    String startMessage,
                                    String endMessage,
                                    Object... args)
        Logs the time spent executing the given function.
        Type Parameters:
        T - The type of the return value.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        function - The function whose execution time must be measured and logged.
        startMessage - The start message to be logged. It can contains parameters, where parameter {0} will always be substituted by the time this method is called (i.e. before the given function is started). Parameter {1} indicates the first extra argument, parameter {2} the second one, and so on.
        endMessage - The message to be logged. It can contains parameters, where parameter {0} will always be substituted by the time spent running the given function in seconds (double value). If not used, the time spent will be appended to the message. Parameter {1} will be the start time, Parameter {2} will be the end time. Parameter {3} indicates the first extra argument, parameter {4} the second one, and so on.
        args - Arguments to be logged as parameters of messages.
        Returns:
        The value returned by the give function.
        See Also:
        MessageFormat
      • logTimeSpent

        public static void logTimeSpent​(Logger logger,
                                        Level level,
                                        Runnable function,
                                        String message,
                                        Object... args)
        Logs the time spent executing the given function.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        function - The function whose execution time must be measured and logged.
        message - The message to be logged. It can contains parameters, where parameter {0} will always be substituted by the time spent running the given function in seconds (double value). If not used, the time spent will be appended to the message. Parameter {1} will be the start time, Parameter {2} will be the end time. Parameter {3} indicates the first extra argument, parameter {4} the second one, and so on.
        args - Arguments to be logged as parameters of message.
        See Also:
        MessageFormat
      • logTimeSpent

        public static void logTimeSpent​(Logger logger,
                                        Level level,
                                        long threshold,
                                        Runnable function,
                                        String message,
                                        Object... args)
        Logs the time spent executing the given function.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        threshold - The time spent executing the given function is logged only if it is larger than the threshold in milliseconds.
        function - The function whose execution time must be measured and logged.
        message - The message to be logged. It can contains parameters, where parameter {0} will always be substituted by the time spent running the given function in seconds (double value). If not used, the time spent will be appended to the message. Parameter {1} will be the start time, Parameter {2} will be the end time. Parameter {3} indicates the first extra argument, parameter {4} the second one, and so on.
        args - Arguments to be logged as parameters of message.
        See Also:
        MessageFormat
      • logTimeSpent

        public static <T> T logTimeSpent​(Logger logger,
                                         Level level,
                                         Callable<T> function,
                                         String message,
                                         Object... args)
        Logs the time spent executing the given function.
        Type Parameters:
        T - The type of the return value.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        function - The function whose execution time must be measured and logged.
        message - The message to be logged. It can contains parameters, where parameter {0} will always be substituted by the time spent running the given function in seconds (double value). If not used, the time spent will be appended to the message. Parameter {1} will be the start time, Parameter {2} will be the end time. Parameter {3} indicates the first extra argument, parameter {4} the second one, and so on.
        args - Arguments to be logged as parameters of message.
        Returns:
        The value returned by the give function.
        See Also:
        MessageFormat
      • logTimeSpent

        public static <T> T logTimeSpent​(Logger logger,
                                         Level level,
                                         long threshold,
                                         Callable<T> function,
                                         String message,
                                         Object... args)
        Logs the time spent executing the given function.
        Type Parameters:
        T - The type of the return value.
        Parameters:
        logger - The Logger to log the message.
        level - One of the message level identifiers, e.g. Level.SEVERE.
        threshold - The time spent executing the given function is logged only if it is larger than the threshold in milliseconds.
        function - The function whose execution time must be measured and logged.
        message - The message to be logged. It can contains parameters, where parameter {0} will always be substituted by the time spent running the given function in seconds (double value). If not used, the time spent will be appended to the message. Parameter {1} will be the start time, Parameter {2} will be the end time. Parameter {3} indicates the first extra argument, parameter {4} the second one, and so on.
        args - Arguments to be logged as parameters of message.
        Returns:
        The value returned by the give function.
        See Also:
        MessageFormat
      • registerHandle

        public static void registerHandle​(Level level,
                                          LogHandler handler)
        Associate a LogHandler with the given Level.

        Normally this method is called by this class' static initializer to register handlers provided by LogHandlerProviders discovered at run-time.

        Parameters:
        level - The Level the given handler has to be associated with.
        handler - The handler managing a message for the given level.
      • stackTrace

        public static String stackTrace​(StackTraceElement[] trace)
        Returns a string representation of the given stack trace.
        Parameters:
        trace - The stack trace array.
        Returns:
        The string representation of the give stack trace.
        See Also:
        Throwable.getStackTrace()