Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusting logging levels - log4j2.xml #648

Closed
brunogenovese opened this issue May 16, 2022 · 8 comments
Closed

Adjusting logging levels - log4j2.xml #648

brunogenovese opened this issue May 16, 2022 · 8 comments

Comments

@brunogenovese
Copy link

Currently it is difficult to setup per-package logging levels during mod development. Since fabric/loom rely on log4j it should be a simple matter of editing the log4j2.xml file or rely or adding a log4j2.xml file that would get merged automatically by functionality already in loom.

As discussed with modmuss50, there are two possible solutions: (1) To improve the wiki documentation so that it would become trivial to do. And/or (2) to add a configuration item in fabric.mod.json to indicate where to place the supplemental log4j2.xml file.

With something like that it would be very easy to create a log4j2.xml like this:

<Configuration name="myMod">
  <Loggers>
    <Logger name="org.mymod.mypackage1" level="debug" additivity="false"/>
    <Logger name="net.hismod.hispackage2" level="debug" additivity="false"/>
  </Loggers>
</Configuration>
@Sharparam
Copy link

Sharparam commented Jun 13, 2022

For others who arrive here from searches:

I managed to get something working by

  • Adding a custom Log4j config in the root of the project log4j-dev.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration name="Dev">
  <Loggers>
    <Logger name="com.example" level="trace" additivity="false">
      <AppenderRef ref="DebugFile" level="${sys:fabric.log.debug.level:-debug}" />
      <AppenderRef ref="SysOut" />
      <AppenderRef ref="LatestFile" level="${sys:fabric.log.level:-info}" />
      <AppenderRef ref="ServerGuiConsole" />
    </Logger>
  </Loggers>
</Configuration>

(Replace com.example with the package your mod uses, and remove the level attributes from DebugFile and LatestFile in case you want detailed logs there as well, I was only interested in the console being more verbose)

loom {
    log4jConfigs.from(file("log4j-dev.xml"))
}

@PlexSheep
Copy link

This doesn't work for me and i can't find a way to set my log level anywhere on the web. Maybe I'm just stupid but i think logging is a huge hole in the fabric docs.

@reoseah
Copy link

reoseah commented Jan 7, 2024

If someone finds this issue like I did, here's a configuration that worked for me

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG" packages="com.example">
    <Loggers>
        <Logger name="com.example.ExampleMod" level="debug">
            <AppenderRef ref="SysOut"/>
            <AppenderRef ref="ServerGuiConsole"/>
        </Logger>
    </Loggers>
</Configuration>

Inside of the ExampleMod:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExampleMod implements ModInitializer {
    public static final Logger LOGGER = LoggerFactory.getLogger(ExampleMod.class);
    
    @Override
    public void onInitialize() {
        LOGGER.debug("Hello DEBUG");
        // ...	

@boholder
Copy link

boholder commented Jan 13, 2024

If someone finds this issue like I did, here's a configuration that worked for me

Hi, could you please point out that how to make the program use this custom log4j config file? Like, where do you put this file?

I tried to replace loom's log4j file with loom { log4jConfigs.from(file("src/main/resources/log4j-dev.xml")) } config but it seems that I couldn't figure where my custom log4j file should be.
https://fabricmc.net/wiki/documentation:fabric_loom

And adding -Dlog4j.configurationFile={absolute path}/log4j-dev.xml to start configuration isn't working too.
https://stackoverflow.com/a/778955/11397457

Sincerely thanks.

@reoseah
Copy link

reoseah commented Jan 13, 2024

The file could be wherever you want it to, though in normal applications it's usually put in src/main/resources where log4j picks it automatically, so you may choose that, or create a folder for it like I did:

loom {
    log4jConfigs.from file("src/dev/resources/log4j.xml")
}

I can see in .gradle/loom-cache/launch.cfg it's added to log4j configuration:

commonProperties
	...
	log4j.configurationFile=/redacted/.gradle/loom-cache/log4j.xml,/redacted/src/dev/resources/log4j.xml
	...

@boholder
Copy link

boholder commented Jan 13, 2024

The file could be wherever you want it to, though in normal applications it's usually put in src/main/resources where log4j picks it automatically, so you may choose that, or create a folder for it like I did:

loom {

    log4jConfigs.from file("src/dev/resources/log4j.xml")

}

I can see in .gradle/loom-cache/launch.cfg it's added to log4j configuration:


commonProperties

	...

	log4j.configurationFile=/redacted/.gradle/loom-cache/log4j.xml,/redacted/src/dev/resources/log4j.xml

	...

Thanks again!

For others who arrive here, please check this commit as reference. That mod is using architectury fork of loom for supporting more mod platforms, so as you can see we have both fabric and forge submodules, but the config remains unchanged. Pleace note about file path.

@modmuss50
Copy link
Member

Closing this issue as I dont think there is anything else for loom to do.

@modmuss50 modmuss50 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2024
@ScribbleTAS
Copy link

For all Eclipse users:
If your custom config doesn't get applied to MC, check the .gradle/loom-cache/launch.cfg/ as mentioned by reoseah.

If you can't see the path that you set in the build.grade, run the eclipse gradle task, which should add the path to that config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants