log.gd
log.gd copied to clipboard
A pretty-printer for Godot
Log.gd, a Godot pretty printer
Now available on the Godot Asset Library!.
Quick Overview
Log.gd provides static functions for printing colorized output. These
are intended as drop-in replacements for print(...)
.
-
Log.pr(...)
- pretty print args in one line -
Log.prn(...)
- the same, but with newlines
This makes your printed output is much more readable, and helps reduce wall-of-text noise and eye strain.
Colorized output
The colorized output really shines when showing nested data structures (Arrays
and Dictionaries
), but it's also very useful for other gdscript primitives,
like Vectors
, NodePaths
, and StringNames
. Support for more types is easily
added, feel free to create an issue!
Call-site prefixes
Log's print functions will prefix the output with the name of the script the log comes from, including the line number.
This call-site feature is really nice! Unfortunately it can only be used during development - it depends on
get_stack()
, which is not available in production builds or at@tool
script time.
Opt-in via duck-typing
You can opt-in to pretty-printing in your classes by implementing
to_printable()
, which Log will pickup via duck-typing.
class_name ExampleClass
func to_printable():
return {val=12}
func _ready():_
Log.pr(self) # colorized `{"val": 12}`
Example script and output
Checkout src/Example.gd for this code.
Log.pr()
colorizes and prints passed arguments, including recursively digging
into Arrays and Dictionaries.
Log.pr()
should Just-Work in most (all?) cases.
You can opt-in to pretty printing in your objects by implementing
to_printable()
,
which gets picked up by Log's static method via duck-typing.
This makes dictionaries and arrays much more readable at a glance, which speeds up debugging and reduces eye-strain.
Compare the above output with the usual from print(...)
:
Public API
-
Log.pr(...)
,Log.info(...)
,Log.log(...)
- pretty-print without newlines
-
Log.prn(...)
- pretty-print with newlines
-
Log.warn(...)
- pretty-print with newlines AND push a warning via
push_warning
- pretty-print with newlines AND push a warning via
-
Log.err(...)
,Log.error(...)
- pretty-print with newlines AND push a error via
push_error
- pretty-print with newlines AND push a error via
These functions all take up to 7 args. We could support more, but you can also just pass an Array or a Dictionary if you need more args right away.
Log.warn()
andLog.err()
are nice because push_warning and push_error on their own do not let you see warnings/errors in the same context as your usualprint()
statements.
godot printing resources
- RichTextLabel docs with lists of bbcode tags: https://docs.godotengine.org/en/stable/tutorials/ui/bbcode_in_richtextlabel.html
- print_rich
supported colors:
black
,red
,green
,yellow
,blue
,magenta
,pink
,purple
,cyan
,white
,orange
,gray
supported tags:b
,i
,u
,s
,indent
,code
,url
,center
,right
,color
,bgcolor
,fgcolor