Skip to content

Logging

Miu edited this page Sep 28, 2023 · 3 revisions

Nexus does perform some form of logging, but this is rarely used, although due to using logging, we needed some form of flexible logging configuration. As such, we support using custom logging adapters for developers to use to handle the logging of Nexus.

By default, Nexus will use a default implementation of NexusConsoleLoggingAdapter when there are no available SLF4J bindings.

Creating your logging adapter

To create your own logging adapter, you have to implement the NexusLoggingAdapter which sports 4 methods (info, error, warn and debug). Originally, Nexus uses SLF4J as a default (although, we use Kotlin to interpolate data, so don't worry about placeholders!).

Once you've created your own logging adapter, you can then set the logging adapter to Nexus:

Nexus.configure {
  global.logger = <your logging adapter>
}

You can refer to the existing, built-in logging adapters for more information about different logging adapters:

Console Logging Adapter

Nexus comes with a built-in logging adapter that uses System.out and comes with level support. The console logging adapter is customizable enough for one's liking and can therefore be used, but we provide default configurations that follows Javacord's logging style with an example of the logging message being:

2022-03-09 15:57:20.493+0800 INFO Nexus You have received the message: Hello World

You can configure Nexus to use this logging adapter with a single line of code:

Nexus.setLogger(new NexusConsoleLoggingAdapter(new NexusConsoleLoggingConfiguration()));

The default configuration follows the specific configuration:

  • $_DATE $_LEVEL Nexus $_MESSAGE format
  • A default date-time format of yyyy-MM-dd HH:mm:ss.SSSZ.

Customizing Formats

You can customize the format but keep the default date-time format by creating a new instance with this:

new NexusConsoleLoggingConfiguration(
       "$_DATE $_LEVEL Nexus is Potato! $_MESSAGE"
)

This will create a configuration that has the following:

  • $_DATE $_LEVEL Nexus is Potato! $_MESSAGE format which translates into 2022-03-09 15:57:20.493+0800 INFO Nexus is Potato! You have received the message: Hello World.
  • A default date-time format of yyyy-MM-dd HH:mm:ss.SSSZ.

You can customize the date-time format but keep the default logging format by creating a new instance with this:

new NexusConsoleLoggingConfiguration(
       DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSZ")
)

You can also customize both of them at the same time if you wish by creating a new instance like this:

new NexusConsoleLoggingConfiguration(
       "$_DATE $_LEVEL Nexus is Potato! $_MESSAGE",
       DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSZ")
)

You can disable date-time formatting by either not including $_DATE in the format or by setting the date-time format as null.

Customizing Logging Levels

If you want to enable DEBUG logging level then you can use the method:

new NexusConsoleLoggingConfiguration().allow(NexusConsoleLoggingLevel.DEBUG);

If you want to prevent the logging of a specific level, for instance, INFO then you can use the method:

new NexusConsoleLoggingConfiguration().disallow(NexusConsoleLoggingLevel.INFO);

If you want to set specific logging levels without using disallow everytime then you can use this method:

new NexusConsoleLoggingConfiguration().set(NexusConsoleLoggingLevel.ERROR, NexusConsoleLoggingLevel.WARN);

To get started with Nexus, we recommend reading the following in chronological:

  1. Installation & Preparing Nexus
  2. Designing Commands
  3. Command Interceptors
  4. Additional Features (Subcommand Router, Option Validation)
  5. Context Menus
  6. Command Synchronization

You may want to read a specific part of handling command and middleware responses:

You can also read about additional features of Nexus:

You can read about synchronizing commands to Discord:

For more additional performance:

Additional configurations:

Clone this wiki locally