Jump to content

Custom Logging with Log4j


Katherine1

Recommended Posts

I'm aware that Forge already uses Log4J for logging and exposes a preconfigured logger for each mod though FMLPreInitializationEvent::getModLog() however, for my purposes, this is insufficient. I need to be able to actually set the configuration of the logger as I need to have two separate loggers, one as a debug logger that can be configured to only log error level or above on the actually server while being able to be set to trace level on a developer machine. The other would simply be for maintaining a chat log with some custom information.

This would be fairly trivial if I could configure the loggers myself, however, there doesn't seem to be an exposed log4j configuration in a Forge setup, and if I try to define my own and pass it to Log4J programmatically, FML overrides.

 

For instance, here is a log4j configuration:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="debug-file" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="C:\path\to\debug.log"/>
        <param name="append" value="true"/>
        <param name="encoding" value="UTF-8"/>
        <param name="MaxFileSize" value="500MB"/>

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %p | %m%n"/>
        </layout>
    </appender>

    <logger name="logger.name" additivity="false">
        <level value="all"/>
        <appender-ref ref="debug-file"/>
    </logger>
</log4j:configuration>

This is a perfectly fine Log4J configuration for a logger. The problem comes below:

ConfigurationSource source = new ConfigurationSource(new FileInputStream(new File("C:\path\to\config.xml")));
LoggerContext logSource = Configurator.initialize(null, source);

Logger logger = logSource.getLogger("logger.name");

To pass a configuration file into Log4J programmatically you'd do those first two lines, and then you should be able to just get the logger from the resulting LoggerContext. Unfortunately, what I end up getting is the preconfigured pile of loggers from Forge.

 

So, is there any way that I can define my loggers with a custom config, or do I just need to give up and configure them entirely programmatically?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.