Class MMLogger

java.lang.Object
org.apache.logging.log4j.spi.AbstractLogger
org.apache.logging.log4j.spi.ExtendedLoggerWrapper
megamek.logging.MMLogger
All Implemented Interfaces:
Serializable, org.apache.logging.log4j.Logger, org.apache.logging.log4j.spi.ExtendedLogger, org.apache.logging.log4j.spi.LocationAwareLogger

public class MMLogger extends org.apache.logging.log4j.spi.ExtendedLoggerWrapper

Utility class to handle logging functions as well as reporting exceptions to Sentry. To deal with general recommendations of confirming log level before logging, additional checks are added to ensure we're only ever logging data based upon the currently active log level.

How to use:

To utilize this class properly, it must be initialized within each class that will use it. For example for use with the megamek.MegaMek.class.


 private static final MMLogger LOGGER = MMLogger.create(MegaMek.class);
 

And then for use, use LOGGER.info(message) and pass a string to it. Currently supported levels include trace, info, warn, debug, error, and fatal. Warn, Error and Fatal can take any Throwable for sending to Sentry and has an overload for a title to allow for displaying of a dialog box.

This class also implements both the parametric pattern and the string format for the functions that are overridden and added here. This means that you can use the following formats for the messages in some cases:


 LOGGER.info("number: {} string: {}", 42, "Hello");
 LOGGER.info("number: %d string: %s", 42, "Hello");
 

Due to how the LOGGER works, the first format using curly braces is preferred, but the second one is grandfathered exclusively for logs already existing, and it is not recommended for any new log.

See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    An extra button offered on error dialogs, installed by the user interface layer.
  • Field Summary

    Fields inherited from class org.apache.logging.log4j.spi.ExtendedLoggerWrapper

    logger

    Fields inherited from class org.apache.logging.log4j.spi.AbstractLogger

    CATCHING_MARKER, DEFAULT_FLOW_MESSAGE_FACTORY_CLASS, DEFAULT_MESSAGE_FACTORY_CLASS, ENTRY_MARKER, EXCEPTION_MARKER, EXIT_MARKER, FLOW_MARKER, name, THROWING_MARKER
  • Method Summary

    Modifier and Type
    Method
    Description
    static MMLogger
    Returns a custom Logger with the name of the calling class.
    static MMLogger
    create(Class<?> loggerName)
    Returns a custom Logger using the fully qualified name of the Class as the Logger name.
    static MMLogger
    create(String loggerName)
    Returns a custom Logger using a specific string as the Logger name.
    void
    debug(String message, Object... args)
    Debug Level Logging
    void
    debug(Throwable exception, String message, Object... args)
    Debug Level Logging
    void
    error(String message)
    Error Level Logging w/o Exception.
    void
    error(String message, Throwable exception)
    Error Level Logging w/ Exception This one was made to make it easier to replace the Log4J Calls
    void
    error(Throwable exception, String message)
    Error Level Logging w/ Exception
    void
    error(Throwable exception, String message, Object... args)
    Error Level Logging w/ Exception
    void
    errorDialog(String title, String message, Object... args)
    Formatted Error Level Logging w/o Exception w/ Dialog.
    void
    errorDialog(Throwable exception, String message, String title, Object... args)
    Formatted Error Level Logging w/ Exception w/ Dialog.
    void
    fatal(Throwable exception, String message)
    Fatal Level Logging w/ Exception.
    void
    fatalDialog(String message, String title)
    Fatal Level Logging w/o Exception w/ Dialog
    void
    fatalDialog(Throwable exception, String message, String title)
    Fatal Level Logging w/ Exception w/ Dialog
    void
    info(String message, Object... args)
    Info Level Logging.
    boolean
    isLevelLessSpecificThan(org.apache.logging.log4j.Level checkedLevel)
    Takes the passed in Level and checks if the current log level is less specific than provided.
    boolean
    isLevelMoreSpecificThan(org.apache.logging.log4j.Level checkedLevel)
    Takes the passed in Level and checks if the current log level is more specific than provided.
    static void
    Installs an extra button shown on every error dialog, typically one offering to report the problem.
    void
    warn(String message, Object... args)
    Warning Level Logging
    void
    warn(Throwable exception, String message, Object... args)
    Warning Level Logging

    Methods inherited from class org.apache.logging.log4j.spi.ExtendedLoggerWrapper

    getLevel, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, logMessage

    Methods inherited from class org.apache.logging.log4j.spi.AbstractLogger

    always, atDebug, atError, atFatal, atInfo, atLevel, atTrace, atWarn, catching, catching, catching, catchingMsg, checkMessageFactory, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, enter, enter, enter, enter, enter, entry, entry, entry, entryMsg, entryMsg, entryMsg, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, exit, exit, exit, exit, exitMsg, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, getFlowMessageFactory, getLogBuilder, getMessageFactory, getName, getRecursionDepth, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, isDebugEnabled, isDebugEnabled, isEnabled, isEnabled, isErrorEnabled, isErrorEnabled, isFatalEnabled, isFatalEnabled, isInfoEnabled, isInfoEnabled, isTraceEnabled, isTraceEnabled, isWarnEnabled, isWarnEnabled, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, printf, printf, requiresLocation, throwing, throwing, throwing, throwingMsg, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, traceEntry, traceEntry, traceEntry, traceEntry, traceEntry, traceExit, traceExit, traceExit, traceExit, traceExit, traceExit, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static MMLogger create()
      Returns a custom Logger with the name of the calling class.
      Returns:
      The custom Logger for the calling class.
    • create

      public static MMLogger create(Class<?> loggerName)
      Returns a custom Logger using the fully qualified name of the Class as the Logger name.
      Parameters:
      loggerName - The Class whose name should be used as the Logger name. If null it will default to the calling class.
      Returns:
      The custom Logger.
    • create

      public static MMLogger create(String loggerName)
      Returns a custom Logger using a specific string as the Logger name.
      Parameters:
      loggerName - The name to be used as the Logger name. Cannot be null nor blank.
      Returns:
      The custom Logger.
    • info

      public void info(String message, Object... args)
      Info Level Logging.
      Specified by:
      info in interface org.apache.logging.log4j.Logger
      Overrides:
      info in class org.apache.logging.log4j.spi.AbstractLogger
      Parameters:
      message - Message to be sent to the log file.
      args - Variable list of arguments for the message
    • warn

      public void warn(String message, Object... args)
      Warning Level Logging
      Specified by:
      warn in interface org.apache.logging.log4j.Logger
      Overrides:
      warn in class org.apache.logging.log4j.spi.AbstractLogger
      Parameters:
      message - Message to be logged.
      args - Variable list of arguments for the message
    • warn

      public void warn(Throwable exception, String message, Object... args)
      Warning Level Logging
      Parameters:
      exception - Exception to be logged.
      message - Message to be logged.
      args - Variable list of arguments for the message
    • debug

      public void debug(String message, Object... args)
      Debug Level Logging
      Specified by:
      debug in interface org.apache.logging.log4j.Logger
      Overrides:
      debug in class org.apache.logging.log4j.spi.AbstractLogger
      Parameters:
      message - Message to be logged.
      args - Variable list of arguments for the message
    • debug

      public void debug(Throwable exception, String message, Object... args)
      Debug Level Logging
      Parameters:
      exception - Exception to be logged.
      message - Message to be logged.
      args - Variable list of arguments for the message
    • error

      public void error(Throwable exception, String message)
      Error Level Logging w/ Exception
      Parameters:
      exception - Exception to be logged.
      message - Additional message.
    • error

      public void error(Throwable exception, String message, Object... args)
      Error Level Logging w/ Exception
      Parameters:
      exception - Exception to be logged.
      message - Additional message to be logged.
      args - Variable list of arguments for the message
    • error

      public void error(String message, Throwable exception)
      Error Level Logging w/ Exception This one was made to make it easier to replace the Log4J Calls
      Specified by:
      error in interface org.apache.logging.log4j.Logger
      Overrides:
      error in class org.apache.logging.log4j.spi.AbstractLogger
      Parameters:
      message - Message to be logged.
      exception - Exception to be logged.
    • error

      public void error(String message)
      Error Level Logging w/o Exception.
      Specified by:
      error in interface org.apache.logging.log4j.Logger
      Overrides:
      error in class org.apache.logging.log4j.spi.AbstractLogger
      Parameters:
      message - Message to be logged.
    • errorDialog

      public void errorDialog(Throwable exception, String message, String title, Object... args)
      Formatted Error Level Logging w/ Exception w/ Dialog.
      Parameters:
      exception - Exception to be logged.
      message - Message to write to the log file AND be displayed in the error pane.
      title - Title of the error message box.
      args - Variable list of arguments for the message.
    • errorDialog

      public void errorDialog(String title, String message, Object... args)
      Formatted Error Level Logging w/o Exception w/ Dialog.
      Parameters:
      title - Title of the error message box.
      message - Message to write to the log file AND be displayed in the error pane.
      args - Variable list of arguments for the message
    • setErrorDialogButton

      public static void setErrorDialogButton(MMLogger.ErrorDialogButton errorDialogButton)
      Installs an extra button shown on every error dialog, typically one offering to report the problem.

      An error dialog is the moment a player is most likely to want to file a bug, so this lets the user interface put the reporting tools directly in front of them instead of expecting them to find a menu.

      Parameters:
      errorDialogButton - the button to offer, or null to show a plain OK dialog as before
    • fatal

      public void fatal(Throwable exception, String message)
      Fatal Level Logging w/ Exception.
      Parameters:
      exception - Exception that was triggered. Probably uncaught.
      message - Message to report to the log file.
    • fatalDialog

      public void fatalDialog(Throwable exception, String message, String title)
      Fatal Level Logging w/ Exception w/ Dialog
      Parameters:
      exception - Exception that was triggered. Probably uncaught.
      message - Message to report to the log file.
      title - Title of the error message box.
    • fatalDialog

      public void fatalDialog(String message, String title)
      Fatal Level Logging w/o Exception w/ Dialog
      Parameters:
      message - Message to report to the log file.
      title - Title of the error message box.
    • isLevelMoreSpecificThan

      public boolean isLevelMoreSpecificThan(org.apache.logging.log4j.Level checkedLevel)
      Takes the passed in Level and checks if the current log level is more specific than provided. This is a helper method around the default LOGGER's method.
      Parameters:
      checkedLevel - Passed in Level to compare to.
      Returns:
      True if the current log level is more specific than the passed in
    • isLevelLessSpecificThan

      public boolean isLevelLessSpecificThan(org.apache.logging.log4j.Level checkedLevel)
      Takes the passed in Level and checks if the current log level is less specific than provided. This is a helper method around the default LOGGER's method.
      Parameters:
      checkedLevel - Passed in Level to compare to.
      Returns:
      True if the current log level is less specific than the passed in