Skip to content
Wizxrd edited this page May 20, 2022 · 4 revisions

logger API

Example Setup

local logwrite = logger:new("TestScript.lua")
logwrite:setLevel("debug")
logwrite:setFile(lfs.writedir().."Logs/TestScript.log")

function Test()
    logwrite:info("Test()", "This is a info message")
end
Test()

Enumerators

logger.enum

Level Number
off 0
alert 1
error 2
warning 3
info 4
debug 5
trace 6

Fields

Name Type Description
source string the script source of the logger
level number the level that is set and anything lower will only be written to a log file.
file string an external file to log to instead of the default dcs.log.
openmode string the mode in which to open a log file initially eg; "w" for a new file, or "a" to append an existing.
datetime string the format for how both date and time are written into custom log files.

Methods

logger:new(source, level, file, openmode, datetime)

Description: returns a new instance of a logger object

Parameter Type Required Description
source string yes the script source that logging will take place within
level number no the max level to be allowed to write to a log file
file string no a custom file path that will act as your log file
openmode string no the mode to open the above file initially, eg "a" to append new lines
datetime string no the format for how both date and time are written into custom log files.

Return: #logger
Examples: any of the following are valid

local logwrite = logger:new()
local logwrite = logger:new("TestScript.lua")
local logwrite = logger:new("TestScript.lua", logger.levels.info)
local logwrite = logger:new("TestScript.lua", logger.levels.debug, "C:/_gitMaster/DCSLogger/logger.log", "w")
local logwrite = logger:new("TestScript.lua", nil, "C:/_gitMaster/DCSLogger/logger.log", "w")
local logwrite = logger:new("TestScript.lua", 3, "C:/_gitMaster/DCSLogger/logger.log", "w", "%Y-%m-%d %H:%M:%S")

logger:setSource(source)

Description: set the source of the logger object
Note: this method is required.

Parameter Type Required Description
source string yes the script source that logging will take place within

Return: #logger
Examples:

logger:setSource("Main.lua")
logwrite:setSource("TestScript.lua")

logger:setLevel(level)

Description: set the level of the logger object

Parameter Type Required Description
level enum yes the max level to be allowed to write to a log file

Return: #logger
Examples:

logwrite:setLevel(logger.levels.error)

logwrite:error("errorMessage", "this message will be logged")
logwrite:info("infoMessage", "this message will not be logged")

logger:setFile(file, openmode)

Description: sets a custom file for log writing

Parameter Type Required Description
file string yes a custom file path that will act as your log file
openmode string yes the mode to open the above file initially, eg "a" to append new lines

Return: #logger
Examples:

logger:setFile(lfs.writedir().."Logs/MyLog.log", "a")
logwrite:setLevel("C:/_gitMaster/DCSLogger/logger.log", "w")

logger:setDateTime(datetime)

Description: set the format for how the date and time is printed to custom log files

Parameter Type Required Description
datetime string yes the format for how both date and time are written into custom log files.

Return: #logger
Examples:

logger:setDateTime("%Y-%m-%d %H:%M:%S")
logwrite:setDateTime("%Y-%m-%d %H:%M:%S")

logger:alert(func, message, ...)

Description: log a message with the prefix of ALERT

Parameter Type Required Description
func string yes the function in which the alert is coming from.
message string yes the message that contains information about the alert.
... vararg no optional variable amount of arguments to format into the message

Return: None
Examples:

function DoStuff(number)
    if type(number) == "string" then
        logger:alert("DoStuff()", "param %s is a string, function requires a number", number)
        return
    end
    -- do stuff
end

logger:error(func, message, ...)

Description: log a message with the prefix of ERROR

Parameter Type Required Description
func string yes the function in which the error is coming from.
message string yes the message that contains information about the error.
... vararg no optional variable amount of arguments to format into the message

Return: None
Examples:

function CheckWeapon(weapon)
    if not weapon then logger:error("CheckWeapon()", "could not check weapon, exiting") return end
    -- check some weapon
end

logger:warning(func, message, ...)

Description: log a message with the prefix of WARNING

Parameter Type Required Description
func string yes the function in which the warning is coming from.
message string yes the message that contains information about the warning.
... vararg no optional variable amount of arguments to format into the message

Return: None
Examples:

local warningTimer = 14100
local restartTimer = 14400
function Scheduler(currentTime)
    if currentTime >= warningTimer then
        logger:warning("Scheduler()", "Mission Restart Scheduler Has 5 Minutes Left")
    elseif currentTime >= restartTimer then
        logger:warning("Scheduler()", "Mission Restarting...")
    end
end

logger:info(func, message, ...)

Description: log a message with the prefix of INFO

Parameter Type Required Description
func string yes the function in which the info is coming from.
message string yes the message that contains information about the info.
... vararg no optional variable amount of arguments to format into the message

Return: None
Examples:

local function getPlayerZone(player)
    for _, zone in pairs(zones) do
        if playerInZone(player, zone.name) then
            logger:info("getPlayerZone()", "player %s in zone %s", player.name, zone.name)
            return zone
        end
    end
end
doStuff()

logger:debug(func, message, ...)

Description: log a message with the prefix of DEBUG

Parameter Type Required Description
func string yes the function in which the debug is coming from.
message string yes the message that contains information about the debug.
... vararg no optional variable amount of arguments to format into the message

Return: None
Examples:

local function main()
    logger:debug("main()", "initializing functions")
    -- initialize functions
end
main()

logger:trace(func, message, ...)

Description: log a message with the prefix of TRACE
Note: this method only works when a custom log file has been defined.

Parameter Type Required Description
func string yes the function in which the debug is coming from.
message string yes the message that contains information about the debug.
... vararg no optional variable amount of arguments to format into the message

Return: None
Examples:

function CallHandler(self, name, event)
    logger:trace("CallHandler()", "tracing call handler")
    -- handler function
end