How to log from your mod

I noticed a few of you are trying to write to our log. Here’s how.

First, in your lua create a logger and give it tag.

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

To write to the log, invoke methods on the logger

log:info(‘hello world’)

We have multiple levels of logging. Here they are

ERROR = 1
WARNING = 3
INFO = 5
DEBUG = 7
DETAIL = 8
SPAM = 9

All of these have a corresponding method on the logger

log:detail(‘really fine grained logging information’)
log:error(‘OMG CRAZY IMPORTANT ERROR!’)

You probably figured most of that out on your own. Here’s the secret sauce.

To actually show stuff in your log, add a logging block to your user_settings.json

"logging" : {
	"show_console" : true,
	"mods" : {
		"your_mod_name" : {
			"log_level" : 1,
			"tag_name" : 5
		}
	}
},

show_console: Pops up a window with the log when the game runs
log_level: The default logging level across the whole mod. This is optional.
tag_name: The logging level for log entries with that tag.

The idea is if you mod as several different logical features, you initialize a separate logger for each of them, with different tags. Then when you’re debugging feature a vs. feature b, you can adjust the logging level in your user settings so your log isn’t spammed with information that you don’t care about.

Examples!

This will log ONLY log:error(’…’) entries from loggers created with the tag ‘feature a’

	"mods" : {
		"your_mod_name" : {
			"feature_a" : 1
		}
	}

This will log all log entries from loggers created with the “feature_b” tag.

	"mods" : {
		"your_mod_name" : {
			"feature_b" : 9
		}
	}

And of course this works

	"mods" : {
		"your_mod_name" : {
			"feature_a" : 1
			"feature_b" : 9
		}
	}
11 Likes

I think you meant log:info('hello world').

Otherwise that seems to be pretty useful - would it be possible to have own listeners for stuff though (I’m thinking of something similar to TraceListener in .NET)?

1 Like

This is what happens when you context switch between lua and js too often. Fixed the typo.

Adding your own listener sounds like a good idea. We’ll add it to the list.

Is this still valid? I can’t get logging to work.

I’m writing a mod called “demon”:

"info" : {
  "name" : "demon",
  "version" : 1
},

I’m trying to write data from a lua file:

local log = radiant.log.create_logger("gen")
local DemonicBuffController=class()
function DemonicBuffController:__init(entity,buff)
    self._entity=entity
    self._buff=buff
    log:detail('Hello world')
end
return DemonicBuffController

My user_settings.json contains the following:

"logging" : {
  "show_console" : true,
  "mods" : {
    "demon" : {
      "gen" : 9
    }
  }
}

I do not get pretty messages in the Lua console.

print() works, though, so I’ll use that for the time being.

1 Like