Class Logging


  • public final class Logging
    extends java.lang.Object
    This class contains utility methods to log errors and warnings.

    There are multiple log levels supported.

    Since:
    10899
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.logging.Level LEVEL_DEBUG
      The josm internal log level to print debug output
      static java.util.logging.Level LEVEL_ERROR
      The josm internal log level indicating a severe error in the application that usually leads to a crash.
      static java.util.logging.Level LEVEL_INFO
      The josm internal log level to use for important events that will be useful when debugging problems
      static java.util.logging.Level LEVEL_TRACE
      The finest log level josm supports.
      static java.util.logging.Level LEVEL_WARN
      The josm internal log level to use when something that may lead to a crash or wrong behaviour has happened.
      private static java.util.logging.Logger LOGGER  
      private static Stopwatch startup  
      private static Logging.RememberWarningHandler WARNINGS  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Logging()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clearLastErrorAndWarnings()
      Clear the list of last warnings
      static void debug​(java.lang.String message)
      Prints a debug message if logging is on.
      static void debug​(java.lang.String pattern, java.lang.Object... args)
      Prints a formatted debug message if logging is on.
      static void debug​(java.lang.Throwable t)
      Prints a debug message for the given Throwable if logging is on.
      static void error​(java.lang.String message)
      Prints an error message if logging is on.
      static void error​(java.lang.String pattern, java.lang.Object... args)
      Prints a formatted error message if logging is on.
      static void error​(java.lang.Throwable t)
      Prints an error message for the given Throwable if logging is on.
      private static java.lang.String getErrorLog​(java.lang.String message, java.lang.Throwable t)  
      private static java.lang.String getErrorLogWithStack​(java.lang.String message, java.lang.Throwable t)  
      static java.lang.String getErrorMessage​(java.lang.Throwable t)
      Returns a human-readable message of error, also usable for developers.
      static java.util.List<java.lang.String> getLastErrorAndWarnings()
      Get the last error and warning messages in the order in which they were received.
      static java.util.logging.Logger getLogger()
      Provides direct access to the logger used.
      static void info​(java.lang.String message)
      Prints a info message if logging is on.
      static void info​(java.lang.String pattern, java.lang.Object... args)
      Prints a formatted info message if logging is on.
      static void info​(java.lang.Throwable t)
      Prints a info message for the given Throwable if logging is on.
      static boolean isDebugEnabled()
      Determines if debug log level is enabled.
      static boolean isLoggingEnabled​(java.util.logging.Level level)
      Tests if a given log level is enabled.
      static boolean isTraceEnabled()
      Determines if trace log level is enabled.
      static void log​(java.util.logging.Level level, java.lang.String message, java.lang.Throwable t)
      Logs a throwable that happened.
      static void log​(java.util.logging.Level level, java.lang.Throwable t)
      Logs a throwable that happened.
      private static void logPrivate​(java.util.logging.Level level, java.lang.String message)  
      private static void logPrivate​(java.util.logging.Level level, java.lang.String pattern, java.lang.Object... args)  
      private static void logPrivate​(java.util.logging.Level level, java.util.function.Supplier<java.lang.String> supplier)  
      static void logWithStackTrace​(java.util.logging.Level level, java.lang.String message, java.lang.Throwable t)
      Logs a throwable that happened.
      static void logWithStackTrace​(java.util.logging.Level level, java.lang.Throwable t)
      Logs a throwable that happened.
      static void logWithStackTrace​(java.util.logging.Level level, java.lang.Throwable t, java.lang.String pattern, java.lang.Object... args)
      Logs a throwable that happened.
      static void setLogLevel​(java.util.logging.Level level)
      Set the global log level.
      static void trace​(java.lang.String message)
      Prints a trace message if logging is on.
      static void trace​(java.lang.String pattern, java.lang.Object... args)
      Prints a formatted trace message if logging is on.
      static void trace​(java.lang.Throwable t)
      Prints a trace message for the given Throwable if logging is on.
      static void warn​(java.lang.String message)
      Prints a warning message if logging is on.
      static void warn​(java.lang.String pattern, java.lang.Object... args)
      Prints a formatted warning message if logging is on.
      static void warn​(java.lang.Throwable t)
      Prints a warning message for the given Throwable if logging is on.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LEVEL_ERROR

        public static final java.util.logging.Level LEVEL_ERROR
        The josm internal log level indicating a severe error in the application that usually leads to a crash.
      • LEVEL_WARN

        public static final java.util.logging.Level LEVEL_WARN
        The josm internal log level to use when something that may lead to a crash or wrong behaviour has happened.
      • LEVEL_INFO

        public static final java.util.logging.Level LEVEL_INFO
        The josm internal log level to use for important events that will be useful when debugging problems
      • LEVEL_DEBUG

        public static final java.util.logging.Level LEVEL_DEBUG
        The josm internal log level to print debug output
      • LEVEL_TRACE

        public static final java.util.logging.Level LEVEL_TRACE
        The finest log level josm supports. This lets josm print a lot of debug output.
      • LOGGER

        private static final java.util.logging.Logger LOGGER
    • Constructor Detail

    • Method Detail

      • setLogLevel

        public static void setLogLevel​(java.util.logging.Level level)
        Set the global log level.
        Parameters:
        level - The log level to use
      • error

        public static void error​(java.lang.String message)
        Prints an error message if logging is on.
        Parameters:
        message - The message to print.
      • error

        public static void error​(java.lang.String pattern,
                                 java.lang.Object... args)
        Prints a formatted error message if logging is on. Calls MessageFormat.format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) function to format text.
        Parameters:
        pattern - The formatted message to print.
        args - The objects to insert into format string.
      • error

        public static void error​(java.lang.Throwable t)
        Prints an error message for the given Throwable if logging is on.
        Parameters:
        t - The throwable object causing the error.
        Since:
        12620
      • warn

        public static void warn​(java.lang.String message)
        Prints a warning message if logging is on.
        Parameters:
        message - The message to print.
      • warn

        public static void warn​(java.lang.String pattern,
                                java.lang.Object... args)
        Prints a formatted warning message if logging is on. Calls MessageFormat.format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) function to format text.
        Parameters:
        pattern - The formatted message to print.
        args - The objects to insert into format string.
      • warn

        public static void warn​(java.lang.Throwable t)
        Prints a warning message for the given Throwable if logging is on.
        Parameters:
        t - The throwable object causing the error.
        Since:
        12620
      • info

        public static void info​(java.lang.String message)
        Prints a info message if logging is on.
        Parameters:
        message - The message to print.
      • info

        public static void info​(java.lang.String pattern,
                                java.lang.Object... args)
        Prints a formatted info message if logging is on. Calls MessageFormat.format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) function to format text.
        Parameters:
        pattern - The formatted message to print.
        args - The objects to insert into format string.
      • info

        public static void info​(java.lang.Throwable t)
        Prints a info message for the given Throwable if logging is on.
        Parameters:
        t - The throwable object causing the error.
        Since:
        12620
      • debug

        public static void debug​(java.lang.String message)
        Prints a debug message if logging is on.
        Parameters:
        message - The message to print.
      • debug

        public static void debug​(java.lang.String pattern,
                                 java.lang.Object... args)
        Prints a formatted debug message if logging is on. Calls MessageFormat.format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) function to format text.
        Parameters:
        pattern - The formatted message to print.
        args - The objects to insert into format string.
      • debug

        public static void debug​(java.lang.Throwable t)
        Prints a debug message for the given Throwable if logging is on.
        Parameters:
        t - The throwable object causing the error.
        Since:
        12620
      • trace

        public static void trace​(java.lang.String message)
        Prints a trace message if logging is on.
        Parameters:
        message - The message to print.
      • trace

        public static void trace​(java.lang.String pattern,
                                 java.lang.Object... args)
        Prints a formatted trace message if logging is on. Calls MessageFormat.format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) function to format text.
        Parameters:
        pattern - The formatted message to print.
        args - The objects to insert into format string.
      • trace

        public static void trace​(java.lang.Throwable t)
        Prints a trace message for the given Throwable if logging is on.
        Parameters:
        t - The throwable object causing the error.
        Since:
        12620
      • log

        public static void log​(java.util.logging.Level level,
                               java.lang.Throwable t)
        Logs a throwable that happened. The stack trace is not added to the log.
        Parameters:
        level - The level.
        t - The throwable that should be logged.
        See Also:
        logWithStackTrace(Level, Throwable)
      • log

        public static void log​(java.util.logging.Level level,
                               java.lang.String message,
                               java.lang.Throwable t)
        Logs a throwable that happened. The stack trace is not added to the log.
        Parameters:
        level - The level.
        message - An additional error message
        t - The throwable that caused the message
        See Also:
        logWithStackTrace(Level, String, Throwable)
      • logWithStackTrace

        public static void logWithStackTrace​(java.util.logging.Level level,
                                             java.lang.Throwable t)
        Logs a throwable that happened. Adds the stack trace to the log.
        Parameters:
        level - The level.
        t - The throwable that should be logged.
        See Also:
        log(Level, Throwable)
      • logWithStackTrace

        public static void logWithStackTrace​(java.util.logging.Level level,
                                             java.lang.String message,
                                             java.lang.Throwable t)
        Logs a throwable that happened. Adds the stack trace to the log.
        Parameters:
        level - The level.
        message - An additional error message
        t - The throwable that should be logged.
        See Also:
        logWithStackTrace(Level, Throwable)
      • logWithStackTrace

        public static void logWithStackTrace​(java.util.logging.Level level,
                                             java.lang.Throwable t,
                                             java.lang.String pattern,
                                             java.lang.Object... args)
        Logs a throwable that happened. Adds the stack trace to the log.
        Parameters:
        level - The level.
        t - The throwable that should be logged.
        pattern - The formatted message to print.
        args - The objects to insert into format string
        See Also:
        logWithStackTrace(Level, Throwable)
      • logPrivate

        private static void logPrivate​(java.util.logging.Level level,
                                       java.lang.String pattern,
                                       java.lang.Object... args)
      • logPrivate

        private static void logPrivate​(java.util.logging.Level level,
                                       java.lang.String message)
      • logPrivate

        private static void logPrivate​(java.util.logging.Level level,
                                       java.util.function.Supplier<java.lang.String> supplier)
      • isLoggingEnabled

        public static boolean isLoggingEnabled​(java.util.logging.Level level)
        Tests if a given log level is enabled. This can be used to avoid constructing debug data if required. For formatting text, you should use the debug(String, Object...) message
        Parameters:
        level - A level constant. You can e.g. use LEVEL_ERROR
        Returns:
        true if log level is enabled.
      • isDebugEnabled

        public static boolean isDebugEnabled()
        Determines if debug log level is enabled. Useful to avoid costly construction of debug messages when not enabled.
        Returns:
        true if log level is at least debug, false otherwise
        Since:
        12620
      • isTraceEnabled

        public static boolean isTraceEnabled()
        Determines if trace log level is enabled. Useful to avoid costly construction of trace messages when not enabled.
        Returns:
        true if log level is at least trace, false otherwise
        Since:
        12620
      • getErrorLog

        private static java.lang.String getErrorLog​(java.lang.String message,
                                                    java.lang.Throwable t)
      • getErrorLogWithStack

        private static java.lang.String getErrorLogWithStack​(java.lang.String message,
                                                             java.lang.Throwable t)
      • getErrorMessage

        public static java.lang.String getErrorMessage​(java.lang.Throwable t)
        Returns a human-readable message of error, also usable for developers.
        Parameters:
        t - The error
        Returns:
        The human-readable error message
      • getLastErrorAndWarnings

        public static java.util.List<java.lang.String> getLastErrorAndWarnings()
        Get the last error and warning messages in the order in which they were received.
        Returns:
        The last errors and warnings.
      • getLogger

        public static java.util.logging.Logger getLogger()
        Provides direct access to the logger used. Use of methods like warn(String) is preferred.
        Returns:
        The logger