How to log (flag or track) a variable?

I’m having a hard time with a few values.
So to make tests easier, I would like to know the value of a few variables. I just don’t know how to do it.

There is that stonehearth.log file that logs a few things, I thought it would be good to have a code to print my values there.

I tried to use this:
log:info(my_var)
and other variations, but it didn’t worked.

How to do it, and is this the right way to track a value?

Hi!
The way the log works is you create a log at the top of the file, and then you have to specify the level of the log, and then listen to that log’s level in your users settings.
Ex:

local log = radiant.log.create_logger(‘my_log_name’)

local MyClass = class()
function MyClass:func(arg_1)
log:detail(“calling func with arg_1: %s”, arg_1)
end

THEN, in your user_settings.json, add a logging section
"logging" : {
“mods” : {
“my_mod” : {
“my_log_name”: 9
}
}
}

The log level numbers are:
ERROR = 1,
WARNING = 3,
INFO = 5,
DEBUG = 7,
DETAIL = 8,
SPAM = 9,
The number you put into your user_settings.json file is the highest log level that will be printed out into stonehearth.log. Ex: putting the number 9 means you’ll get all errors, warnings, info, debug, detail, and spam. Meanwhile, putting 3 means you’ll only get errors and warnings.

Hope this helps
Let me know if you run into any issues.
-Yang

2 Likes

Yes, thanks, it helped a lot.
I was missing the user_settings.json setup.
Thanks!

The user_settings method does not work for me. If it doesn’t work for anyone else, see here for alternate way

http://discourse.stonehearth.net/t/moai-personal-thread/20070/16?u=moai

So if it doesn’t work for you, it might be related to the user setting registration for that log. I know based on which function is used to write out the log statement, the registration in user settings will change. Can you paste the line that creates the log and your user settings file?

Thanks
-Yang

Thank you, Yang! I actually sent you a PM yesterday with that information, so you might still have it there. I was trying to write from the main Stonehearth mod. I’ve since separated my mod into an smod, which can be found here, along with the relevant user_settings and lua code:

http://discourse.stonehearth.net/t/moai-personal-thread/20070/18?u=moai