Log - General Purpose Logging Helper


#1

The difference between Log and Err is that Err is meant to help you log the automated Defold script errors / engine crashes. While Log is meant as a general purpose logging tool for whatever manual logging you would like to do.

Log

General purpose logging for Defold

Installation

You can use Log in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:

https://github.com/subsoap/log/archive/master.zip

Once added, you must require the main Lua module in your scripts via

local log = require("log.log")

Or require it globally in your main entrypoint so that all of your scripts can use Log without each needing to require it.

log = require("log.log")

Usage

First set your appname. Your appname determines a relative OS directory for storing your app’s logs.

log.set_appname("YourAppName")

You can then log information to logs based on levels. You can use a short or long form of the log function names.

log.c("This would be a critical log message.")
log.critical("This would also be a critical log message.")

Here is what the above lines would log to your log file. Note that it inclues the log level, timestamp, and script information (path and line number) of which script did the logging.

[CRIT  2018-06-12 00:21:10] /example/example.script:19: This would be a critical log message.
[CRIT  2018-06-12 00:21:10] /example/example.script:20: This would also be a critical log message.

There are muliple built in log levels. You are able to set the minimum log level so that any messages below that log level are not logged.

log.set_log_level(log.INFO) -- disable logging for all log levels below log.INFO

When you log a message you can include an optional tag. Tags can be used to whitelist (or blacklist) certain log information types. For example, you may only want to allow logging from one section of your game while you are doing debugging, you can disable all other log tags.


#2

Nice!
How does it handle release builds? (where logging is disabled)


#3

It doesn’t use any built in logging features (other than print), so I think it should just work as it saves to a file on the os. It uses debug to determine line number which I think is available in release but there’s also a check for that already in case it’s not available.

I think a way was added to detect if a build is release or not but I can’t quite remember how? I’ll add an option to disable logging entirely / ability to auto-detect non-debug builds


#4

It’s called sys.get_engine_info().is_debug

Yes, being able to disable the “debug” calls is probably good. Not sure how expensive they are, but still…


#5

Thank you!

I’ve just added a config option log.disable_logging_for_release which defaults to true, and will do zero logging if it detects the engine is not in debug if it is set to true.

I don’t know what the extra cost of the function calls would be, but the short circuits are in place so if logging is disabled, or logging is disabled in release mode then it will short circuit the log calls then.

If logging was a built in function then maybe a game.project option would be to strip out all log calls from scripts on bundling.


#6

Added log.delete_old_logs(days) which will automatically delete logs older than a set number of days. LFS is required to use this function but not Log as a whole. I’m using LFS because it’s much cleaner than alternatives for directory listing and is cross platform unlike other solutions.