Skip to content

Add logging documentation #580

Open
Open
@Strokkur424

Description

@Strokkur424

Three main things should be documented around logging:

  1. What are the differences between loggers (System.out (not a logger!!), java.util.Logger, slf4j logger, component logger)
  2. Correct logging usage in Paper plugins (make sure to say that logging more is better than less! Use the correct logging levels, keep debug logging in, and, again, do not use System.out!).
  3. Adding a custom log4j2.xml for your plugin for more robust logging of your plugin-internal logging. A.e. putting logs from your own plugin into /plugins/<pluginname>/logs to easier log sharing for plugin specific issues.

Should also link to the log4j2 documentation: https://logging.apache.org/log4j/2.x/

Example log4j2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
    <Appenders>
        <RollingRandomAccessFile name="YourPlugin" 
                                 fileName="plugins/YourPlugin/logs/latest.log"
                                 filePattern="plugins/YourPlugin/logs/%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout>
                <LoggerNamePatternSelector defaultPattern="[%d{HH:mm:ss}] [%t/%level]: [%logger] %stripAnsi{%msg}%n%xEx{full}" />
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <OnStartupTriggeringPolicy/>
            </Policies>
            <DefaultRolloverStrategy max="5"/>
        </RollingRandomAccessFile>
    </Appenders>
    <Loggers>
        <Root level="INFO"/>
        <Logger name="your.package.name" level="DEBUG" additivity="false">
            <AppenderRef ref="YourPluginFile"/>
        </Logger>
    </Loggers>
</Configuration>
Example: Registering the logger inside your plugin. (A nice use of the bootstrapper, too!)
// PluginBoostrap.java

public PluginBootstrap() {
    final URL configUrl = getClass().getResource("/log4j2.xml");
    if (configUrl != null) {
        Configurator.initialize("YourPlugin", configUrl.toExternalForm());
    }
    else {
        LOGGER.error("Failed to find log4j2.xml!");
    }
}

Furthermore, one should note on the usage of markers and how to add them to your log4j2.xml.
Reference documentation for marker filters: https://logging.apache.org/log4j/2.x/manual/filters.html.

<Logger name="your.package.name" level="DEBUG" additivity="false">
    <AppenderRef ref="YourPluginFile">
        <MarkerFilter marker="PluginMarker" onMatch="DENY" onMismatch="NEUTRAL"/>
    </AppenderRef>
</Logger>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions