# Debug logging in Factorio mod

I'm writing a Factorio mod, and I'm trying to figure out why it doesn't work. Can I do some kind of error / debug logging somewhere? Both manual (printing variables) and simple error reporting (accessing a property that doesn't exist) would be great. I know about the player.print method, but I can only get it to print string literals, not objects:

script.on_event(defines.events.on_player_created, function(event)
pcall(function()
local player = game.get_player(event.player_index)
player.print("Hi player") -- works
player.print(player) -- does not work. Any error message would be nice
end)
end)


I've also tried the log file in the application folder, and the various debug modes (F5/F6 in game), but so far haven't found anything useful.

• Perhaps you're assuming it will automatically generate a string from the object? You might need to create and explicitly use some kind of ToString() method. – MichaelHouse May 1 '16 at 20:47

According to this forum post

You can launch the game from console, so standard print function would send its output there. You can redirect that output to files.

There is serpent pretty printer, which can print lua tables - syntax is print(serpent.block(arg)).

• Sounds good, but it doesn't quite work. Running factorio.exe from the command prompt starts the game, but returns immediately and I only see the output of the first log line. Using start factorio.exe /wait doesn't work either, because it only opens a console window and doesn't start the game at all. – Jorn May 1 '16 at 19:03

You can use the error() function which sends to factorio-current.log

For example:

error("Hi player")

or

error(serpent_block(table_name))

• serpent_block --> serpent.block – Tony Baguette Feb 12 '19 at 19:45
• It actually shows an "error" popup with the message at the game loading and prevents the mod to be loaded. – Tony Baguette Feb 12 '19 at 19:47

There is a write_file method in LuaGameScript.

game.write_file("mylog.log", serpent_block(table_name)) -- normal write
game.write_file("mylog.log", serpent_block(table_name), true) -- appending


The file will end up in a folder called script-output under %APPDATA%/Factorio, next to your scenarios folder.

• Does not seem to work from data.lua – LOST Aug 28 '17 at 0:25
• data.lua is not a normal runtime script. Its purpose is to describe a data structure and nothing else. Most(all?) runtime objects are not accessible during it's interpretation. In particular the global LuaGameScript object called game does not even exist then. – Martin Lütke Sep 3 '17 at 15:32