Insality/defold-log/log
Logger with Performance Metrics for Defold
- GitHub: Insality/defold-log — pinned to
8- This repo publishes no stable release. Pick a tag from Insality/defold-log tags and add its Source code (zip) URL — for the pinned tag,
https://github.com/Insality/defold-log/archive/refs/tags/8.zip— togame.projectunder[project]dependencies, then Fetch Libraries in the Defold editor. - Run
bunx @defold-typescript/cli resolveto materialize its types. - Import it under a namespace alias of your choice:
import * as log from "log"
- This repo publishes no stable release. Pick a tag from Insality/defold-log tags and add its Source code (zip) URL — for the pinned tag,
Functions
get_logger(logger_name?: string | undefined, force_logger_level_in_debug?: string | undefined): loggeradd_callback(callback: (logger: logger, level: string, message: string, context: unknown, log_message: string) => void)remove_callback(callback: (...args: any[]) => unknown)clear_callbacks()set_file(path?: string | undefined): string | undefinedget_file(): string | undefinedclear_log_files()final()
get_logger(logger_name?: string | undefined, force_logger_level_in_debug?: string | undefined): logger
Return the new logger instance
Parameters
logger_name?:string | undefined— Default is the file name of the current scriptforce_logger_level_in_debug?:string | undefined— Debug builds only, values: FATAL, ERROR, WARN, INFO, DEBUG, TRACE
Returns
logger
add_callback(callback: (logger: logger, level: string, message: string, context: unknown, log_message: string) => void)
Add a custom handler for log messages
Parameters
callback:(logger: logger, level: string, message: string, context: unknown, log_message: string) => void
remove_callback(callback: (...args: any[]) => unknown)
Remove a previously added handler
Parameters
callback:(...args: any[]) => unknown— The callback function to remove
clear_callbacks()
Remove all custom handlers. The file writing is not affected
set_file(path?: string | undefined): string | undefined
Set the log file for all loggers, in addition to the console and the personal logger files.
The path is always relative (/logs/game.log): project folder in the editor, save folder otherwise.
Save folder has no nested dirs (sys.get_save_file), so / is replaced with _ (logs_game.log).
Pass nil to disable
Parameters
path?:string | undefined
Returns
string | undefined
get_file(): string | undefined
Get the current log file path for all loggers, or nil if disabled
Returns
string | undefined
clear_log_files()
Delete all known logger .log files from the disk
final()
Flush, close and disable all log files.
Call it once on the application shutdown, e.g. from the final of your bootstrap script.
Further log messages will not reopen the files until set_file / set_file_nearby is called again
Types
log
log.trace(message: string | undefined, data: unknown)
Log message with TRACE level
Parameters
message:string | undefined— Message to log. Nil only refreshes the memory/time trackingdata:unknown— Additional context data
log.debug(message: string | undefined, data: unknown)
Log message with DEBUG level
Parameters
message:string | undefined— Message to log. Nil only refreshes the memory/time trackingdata:unknown— Additional context data
log.info(message: string | undefined, data: unknown)
Log message with INFO level
Parameters
message:string | undefined— Message to log. Nil only refreshes the memory/time trackingdata:unknown— Additional context data
log.warn(message: string | undefined, data: unknown)
Log message with WARN level
Parameters
message:string | undefined— Message to log. Nil only refreshes the memory/time trackingdata:unknown— Additional context data
log.error(message: string | undefined, data: unknown)
Log message with ERROR level
Parameters
message:string | undefined— Message to log. Nil only refreshes the memory/time trackingdata:unknown— Additional context data
log.set_file_nearby(): string | undefined
Write this logger messages to a <logger_name>.log file next to the calling script.
Needs game.project in the working directory, so it only works when the game is
started from the project root: the editor, or a desktop build launched from there
Returns
string | undefined