Günlük Oluşturma ve Hata Giderme

Java’da kullanılabilir olan birkaç tane günlük oluşturma çerçevesi mevcuttur. Günlük oluşturma, System.out.println() ile stdout ya da stderr üzerine yazdırma için, birkaç sebep yüzünden tercih edilebilirdir:

  • Günlüğe kaydedilen mesajlar, kaydedilen mesajların nereden geldiğini anlamayı kolaylaştıran bir kaynak adı ile etiketlenir.

  • Günlüğe kaydedilen iletiler, basit filtrelemeye izin veren bir önem düzeyine sahiptir (örneğin, tüm kritik olmayan bildirimleri devre dışı bırakma).

  • Mevcut sunucu yapısı belirli kaynaklardan gelen mesajları etkinleştirmenize veya devre dışı bırakmanıza izin verir.

Sponge,``org.slf4j.Logger`` kullanır; java.util.logging.Logger kullanmaz.

Günlük Oluşturucu Alma

Eklentilerin başlatılması esnasında kullanılan Guice modülü, eklenti odaklı bir günlük oluşturucuya sahiptir. Bu durum, eklentiniz için, önceden doğru eklenti ID’siyle ayarlanmış günlük oluşturucuyu almak adına @Inject ile bir alan, metot ya da kurucuyu ek açıklama olarak belirtmenize olanak tanır.

Not

Eklenti ID’nizi yapılandırmak hakkında bilgi için, Ana eklenti sınıfı makalesine göz atın.

Örnek - Alan

import com.google.inject.Inject;
import org.slf4j.Logger;

@Inject
private Logger logger;

Örnek - Yöntem

private Logger logger;

@Inject
private void setLogger(Logger logger) {
    this.logger = logger;
}

Örnek - Oluşturucu

// For the purpose of this example, "Banana" is the class name

private Logger logger;

@Inject
public Banana(Logger logger) {
    this.logger = logger;
}

Sunucunuzu, eklenti yüklendiğinde Guice enjektörü ile somutlaştırıldığı gibi ana eklenti bölümünüzde ayarlamanız önerilir.

İsteğe bağlı olarak günlükçünüzün ayarlandığı aynı sınıfa alma yöntemi de oluşturmak idealdir. Aşağıda bir getter yöntemi örneği verilmiştir.

public Logger getLogger() {
    return logger;
}

Mesajları Yayımlama

Günlük oluşturucunuz ile mesaj yollamak çok kolaydır.

Not

Aşağıdaki örnek, önceki bölümde gösterildiği üzere, günlük oluşturucunuzun alma metodunun adının getLogger() olduğunu varsaymaktadır. Alma metodunuzu nasıl adlandırdığınıza bağlı olarak, bu ad sizin için farklı olabilir.

getLogger().info("String");
getLogger().debug("String");
getLogger().warn("String");
getLogger().error("String");

Bu satır yayınlamak istediğiniz mesajdır. Örneğin:

getLogger().warn("This is a warning!");

Manipulating the Logging

Not

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:

  1. Ask the author of that library to adjust their logging standards, as this fixes the problem at its source.

  2. Recommend your users to configure the logging using a log4j2.xml config file. Provide users with the recommended configuration additions.

  3. 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 to WARN. This will hide all messages that use a lower log level such as INFO and DEBUG.

    Uyarı

    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.