Class LoggingService

java.lang.Object
ortus.boxlang.runtime.logging.LoggingService

public class LoggingService extends Object
This service allows BoxLang to leverage logging facilities and interact with the logging system: LogBack: https://logback.qos.ch/manual/index.html

It also manages all custom logging events, appenders and loggers.

It's not a true BoxLang service, due to the chicken and egg problem of logging being needed before the runtime starts.

The configureBasic(Boolean) method is called by the runtime to setup the basic logging system first, then once the runtime is online and has read the configuration file (boxlang.json), it can reconfigure the logging system via the reconfigure() method.

Please note that in BoxLang you can use the following arguments for logging via the logMessage(String, String, String, String) method:

  • message - The message to send for logging or a lambda that produces the message
  • type - The logging level type (error, info, warn, debug, trace)
  • applicationName - The name of the BoxLang application (if any)
  • logger - The named logger to emit to. Example: "scheduler, application, orm, etc"

If the named logger does not exist or it's an absolute path, then the logger will be registered as a new logger, with the name of the file as the category.

  • Field Details

  • Method Details

    • getInstance

      public static LoggingService getInstance(BoxRuntime runtime)
      Get the singleton instance of the LoggingService and initializing it with the runtime This is called by the Runtime only!
      Returns:
      The LoggingService instance
    • getInstance

      public static LoggingService getInstance()
      Get the singleton instance of the LoggingService
      Returns:
      The LoggingService instance
      Throws:
      IllegalStateException - If the LoggingService has not been initialized yet
    • getRootLogger

      public BoxLangLogger getRootLogger()
      Get the root logger
      Returns:
      The root logger
    • getLogsDirectory

      public String getLogsDirectory()
      Get the runtime's log directory as per the configuration
    • setRootLogger

      public LoggingService setRootLogger(BoxLangLogger logger)
      Set the root logger for the runtime
      Parameters:
      logger - The logger to set
      Returns:
      The logging service
    • getDefaultEncoder

      public ch.qos.logback.core.encoder.EncoderBase<ch.qos.logback.classic.spi.ILoggingEvent> getDefaultEncoder()
      Get the BoxLang pattern encoder
      Returns:
      The encoder
    • getEncoderForLoggerConfig

      public ch.qos.logback.core.encoder.EncoderBase<ch.qos.logback.classic.spi.ILoggingEvent> getEncoderForLoggerConfig(LoggerConfig loggerConfig)
      This method receives a logger config and returns the encoder to use
      Parameters:
      loggerConfig - The logger configuration to use
      Returns:
      The encoder to use for the logger configuration
    • setDefaultEncoder

      public LoggingService setDefaultEncoder(ch.qos.logback.core.encoder.EncoderBase<ch.qos.logback.classic.spi.ILoggingEvent> encoder)
      Store the BoxLang pattern encoder
      Parameters:
      encoder - The encoder to store
      Returns:
      LoggingService
    • getLoggerContext

      public ch.qos.logback.classic.LoggerContext getLoggerContext()
      Get the logger context
      Returns:
      The logger context
    • setLoggerContext

      public LoggingService setLoggerContext(ch.qos.logback.classic.LoggerContext loggerContext)
      Set the logger context
      Parameters:
      loggerContext - The logger context to set
      Returns:
      The logging service
    • configureBasic

      public LoggingService configureBasic(Boolean debugMode)
      This configures LogBack with a basic configuration, so we can use logging before we actually read the configuration file.

      Once the configuration file is read, we can reconfigure the logging system.

      Parameters:
      debugMode - The flag the runtime was started with
    • reconfigure

      public LoggingService reconfigure()
      This method is called by the runtime to reconfigure the logging system once the configuration file has been read.

      This could change logging levels, add new appenders, etc.

    • logMessage

      public LoggingService logMessage(String message)
      Log a message into the default log file and the default log type with no application name
      Parameters:
      message - The message to log
      Returns:
      The logging service
    • logMessage

      public LoggingService logMessage(String message, String type)
      Log a message into the default log file and a custom log type with no application name
      Parameters:
      message - The message to log
      type - The type of log message (fatal, error, info, warn, debug, trace)
      Returns:
      The logging service
    • logMessage

      public LoggingService logMessage(String message, String type, String applicationName)
      Log a message into the default log file and a custom log type and a custom application name
      Parameters:
      message - The message to log
      type - The type of log message (fatal, error, info, warn, debug, trace)
      applicationName - The name of the application requesting the log message
      Returns:
      The logging service
    • logMessage

      public LoggingService logMessage(String message, String type, String applicationName, String logger)
      Log a message with specific arguments
      Parameters:
      message - The message to log
      type - The type of log message (fatal, error, info, warn, debug, trace)
      applicationName - The name of the application requesting the log message
      logger - The logger destination. It can be a named logger or an absolute path
      Returns:
      The logging service
      Throws:
      IllegalArgumentException - If the log level is not valid
    • getRuntimeLogger

      public BoxLangLogger getRuntimeLogger()
      Alias to get the runtime logger
      Returns:
      The runtime logger
    • getExceptionLogger

      public BoxLangLogger getExceptionLogger()
      Alias to get the exception logger
    • getLogger

      public BoxLangLogger getLogger(String logger)
      Get a logger by registered name. If the logger doesn't exist, it will auto-register it and load it using the name as the file name in the logs directory.
      Parameters:
      logger - The name of the logger to retrieve.
      Returns:
      The logger requested
    • hasLogger

      public boolean hasLogger(Key loggerName)
      Verify if a logger with the specified name exists
      Parameters:
      loggerName - The name of the logger to verify
    • getLoggersCount

      public int getLoggersCount()
      Tell me how many loggers have been registered
      Returns:
      The number of loggers registered
    • getLoggersList

      public List<String> getLoggersList()
      Get a list of all the registered loggers so far in the runtime
      Returns:
      The list of loggers
    • getLoggersKeys

      public List<Key> getLoggersKeys()
      Get a list of all the registered loggers so far in the runtime
      Returns:
      The list of loggers
    • removeLogger

      public boolean removeLogger(Key loggerName)
      Remove a logger by name
      Parameters:
      loggerName - The name of the logger to remove
      Returns:
      True if the logger was removed, false otherwise
    • getOrBuildAppender

      public ch.qos.logback.core.Appender<ch.qos.logback.classic.spi.ILoggingEvent> getOrBuildAppender(String filePath, ch.qos.logback.classic.LoggerContext logContext, LoggerConfig loggerConfig)
      Get the requested file appender according to log location
      Parameters:
      filePath - The file path to get the appender for
      logContext - The logger context requested for the appender
      loggerConfig - The logger configuration
      Returns:
      The appender for the requested logger configuration
    • hasAppender

      public boolean hasAppender(String filePath)
      Verify if we have the passed in filePath appender
      Returns:
      True if the appender exists, false otherwise
    • removeAppender

      public boolean removeAppender(String filePath)
      Remove the appender from the cache using the file path
      Parameters:
      filePath - The file path to remove the appender for
      Returns:
      True if the appender was removed, false otherwise
    • getAppendersList

      public List<String> getAppendersList()
      Get a list of all registered file appenders
      Returns:
      The list of appenders
    • shutdownAppenders

      public LoggingService shutdownAppenders()
      Shutdown all the appenders
      Returns:
      The logging service
    • shutdown

      public LoggingService shutdown()
      Shutdown the logging service