Jump to content

Proper way to use a Logger


yooksi

Recommended Posts

What is the proper way of using a logger to log errors, debug information and other stuff I don't know about? I've tried to use it for debugging by calling logger.debug(msg) but I haven't been able to make it work, despite being in the actual debug mode. It was some time ago but I don't think I've been able to make it work for errors as well. How do you guys use the logger, and for what purposes?

I still haven't published a mod because I can never get that in-dev version just right to warrant a public release. And yes, after two years of mod development I am still learning to speak Java.

 

Follow me on GitHub: https://github.com/yooksi

Contact me on Twitter: https://twitter.com/yooksi

Read my Minecraft blog: https://yooksidoesminecraft.blogspot.de/

Link to comment
Share on other sites

Are you sure it didn't work?

 

Regardless of where you're running Minecraft, only messages logged at

INFO

or higher are printed to the console. Every message (including

DEBUG

and

TRACE

messages) is written to the log file.

 

I use this wrapper for logging. You can see where I use it here.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Very nice, thank you for giving me examples. I will try them out in a second, but I am wondering what are these markers. Searching on the internet about them gives me a bunch of technical information, but for starters I just want to know what is the simplest application of them?

I still haven't published a mod because I can never get that in-dev version just right to warrant a public release. And yes, after two years of mod development I am still learning to speak Java.

 

Follow me on GitHub: https://github.com/yooksi

Contact me on Twitter: https://twitter.com/yooksi

Read my Minecraft blog: https://yooksidoesminecraft.blogspot.de/

Link to comment
Share on other sites

Very nice, thank you for giving me examples. I will try them out in a second, but I am wondering what are these markers. Searching on the internet about them gives me a bunch of technical information, but for starters I just want to know what is the simplest application of them?

 

Markers are objects that can be used to filter log messages. For example, you could configure all messages except those with the marker

FOO

to be printed to one log file and all messages with the marker

FOO

to be printed to a separate log file.

 

You generally don't need to use markers and Forge's log config won't print them to the console or log file.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Are you sure it didn't work?

 

Regardless of where you're running Minecraft, only messages logged at

INFO

or higher are printed to the console. Every message (including

DEBUG

and

TRACE

messages) is written to the log file.

 

I use this wrapper for logging. You can see where I use it here.

Aha, now I understand. The error log is displayed in the console but the debug log is not, instead it goes into the log file. I didn't know that. Can you give me a quick example of how to simply setup a marker to post all my mod debug stuff in a separate log file? When I check the log file for debug logs there is too much info I don't want to see, and sorting through that to find what I need can be a bit tiresome.

I still haven't published a mod because I can never get that in-dev version just right to warrant a public release. And yes, after two years of mod development I am still learning to speak Java.

 

Follow me on GitHub: https://github.com/yooksi

Contact me on Twitter: https://twitter.com/yooksi

Read my Minecraft blog: https://yooksidoesminecraft.blogspot.de/

Link to comment
Share on other sites

Can you give me a quick example of how to simply setup a marker to post all my mod debug stuff in a separate log file? When I check the log file for debug logs there is too much info I don't want to see, and sorting through that to find what I need can be a bit tiresome.

 

It looks the version of log4j used by Minecraft only supports a single global configuration file, so you'll need to add this in code. I haven't done this myself, but the log4j website briefly explains it here.

 

Like in the example, you'll probably need to create an

Appender

to append to your file and a

LoggerConfig

with the same name as your

Logger

(this is your mod ID if you're using the one returned by

FMLPreInitializationEvent

). The

LoggerConfig

should be non-additive and have an

AppenderRef

to your

Appender

. You can also give it a

MarkerFilter

to only include messages with a specific marker.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

It seems to me that it would be a lot easier to just setup my own logger and have it write to a dedicated text file. I don't know where to add that code and I think this all requires me to setup a log4j.properties file, which I've created with default values but don't know how to test to see if it loads. Well, at least I got the error and debug messages to work. That's gonna do good for now, thanks.

I still haven't published a mod because I can never get that in-dev version just right to warrant a public release. And yes, after two years of mod development I am still learning to speak Java.

 

Follow me on GitHub: https://github.com/yooksi

Contact me on Twitter: https://twitter.com/yooksi

Read my Minecraft blog: https://yooksidoesminecraft.blogspot.de/

Link to comment
Share on other sites

I use this wrapper for logging. You can see where I use it here.

I decided to use your wrapper and come up with a secondary mod logging system. The system is currently setup to write errors, warnings and fatal logs to a separate text file in addition to being already printed to the console and written to the main log4j log file. This way I have a copy of all the logs I am really interested in. I can use that info (by checking the date and time) to find the stack trace in the FML-client log file. This is the best thing I could come up with, you can check it out here.

 

I've also discovered this in the comment of the FMLPreInitializationEvent.getModLog() method:

Configurations can be applied through the config/logging.properties file, specifying logging levels for your ModID. Use this!

Could this be where we should create out log4j property file or is this some kind of custom Forge configuration file?

I still haven't published a mod because I can never get that in-dev version just right to warrant a public release. And yes, after two years of mod development I am still learning to speak Java.

 

Follow me on GitHub: https://github.com/yooksi

Contact me on Twitter: https://twitter.com/yooksi

Read my Minecraft blog: https://yooksidoesminecraft.blogspot.de/

Link to comment
Share on other sites

I've also discovered this in the comment of the FMLPreInitializationEvent.getModLog() method:

Configurations can be applied through the config/logging.properties file, specifying logging levels for your ModID. Use this!

Could this be where we should create out log4j property file or is this some kind of custom Forge configuration file?

 

No, that's an outdated comment from before Minecraft switched to log4j.

 

Edit: I've reported it here.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.