Logowanie i Debugowanie
There are a few logging frameworks available for use in Java. Logging is preferable to printing to stdout or stderr with
System.out.println()
for a number of reasons:
Logged messages are labeled with a source name, making it easier to figure out where the logged messages are coming from.
Logged messages have a severity level which allows for simple filtering (e.g. disable all non-critical notices).
Domyślnie dostępne loggery pozwalają włączyć lub wyłączyć wiadomości z wybranych źródeł
Sponge uses org.slf4j.Logger
, not java.util.logging.Logger
.
Uzyskiwanie Loggera
The Guice module used during the initialization of plugins has a plugin-scoped logger. This allows you to annotate a
field, method, or constructor with @Inject
to get the logger for your plugin, which is pre-configured with the
correct plugin ID.
Informacja
See Główna Klasa Pluginu for information on configuring your plugin ID.
Example - Field
import com.google.inject.Inject;
import org.slf4j.Logger;
@Inject
private Logger logger;
** Przykład - metoda **
private Logger logger;
@Inject
private void setLogger(Logger logger) {
this.logger = logger;
}
Example - Constructor
// For the purpose of this example, "Banana" is the class name
private Logger logger;
@Inject
public Banana(Logger logger) {
this.logger = logger;
}
It is recommended to set your logger in your main plugin class, as it is instantiated with the Guice injector when the plugin is loaded.
Creating a getter method for your logger in the same class in which it was set is also ideal, although optional. An example getter method is illustrated below.
public Logger getLogger() {
return logger;
}
Wyświetlanie Wiadomości
Wysyłanie wiadomości z Twojego rejestratora jest bardzo proste.
Informacja
Poniższy przykład zakłada, że metoda pobierająca Twojego rejestratora o nazwie «» getLogger()»», jak pokazano w poprzedniej sekcji. Dla Ciebie może się różnić w zależności od tego, co wymieniłeś w metodzie getter.
getLogger().info("String");
getLogger().debug("String");
getLogger().warn("String");
getLogger().error("String");
Ciągiem jest wiadomość, którą chcesz emitować. Na przykład:
getLogger().warn("This is a warning!");
Manipulating the Logging
Informacja
These techniques should only be used in very rare cases such as badly chosen logging defaults in libraries. Add a config option to disable those if you use them.
Some libraries use bad logging practices such as logging on too high a level. In these cases, you have three choices:
Ask the author of that library to adjust their logging standards, as this fixes the problem at its source.
Recommend your users to configure the logging using a
log4j2.xml
config file. Provide users with the recommended configuration additions.Configure the logging in your plugin yourself:
((org.apache.logging.log4j.core.Logger) LogManager.getLogger("FtpLoggingFilter")).setLevel(Level.WARN);
This configures the log level of the
FtpLoggingFilter
logger toWARN
. This will hide all messages that use a lower log level such asINFO
andDEBUG
.Ostrzeżenie
This solution assumes that log4j2 is used as logging framework by the server, however that might not be the case for all/future implementations of the SpongeAPI.
If you have any questions regarding logging you can always ask us on IRC, Discord or the Forums.