Skip to main content

Log

This item only works when running on the server. Server

Log class for logging to the AnalyticsService (e.g. PlayFab). The API is based off of Google's Flogger fluent logging API.

local Log = require(somewhere.Log)
local logger = Log.new()

-- Log a simple message:
logger:AtInfo():Log("Hello world!")

-- Log only every 3 messages:
for i = 1,20 do
	logger:AtInfo():Every(3):Log("Hi there!")
end

-- Log only every 1 second:
for i = 1,100 do
	logger:AtInfo():AtMostEvery(3, Log.TimeUnit.Seconds):Log("Hello!")
	task.wait(0.1)
end

-- Wrap the above example into a function:
local log = logger:AtInfo():AtMostEvery(3, Log.TimeUnit.Seconds):Wrap()
for i = 1,100 do
	log("Hello!")
	task.wait(0.1)
end

-- Assertion:
logger:Assert(typeof(32) == "number", "Somehow 32 is no longer a number")

LogConfig

A LogConfig ModuleScript is expected to exist somewhere within ReplicatedStorage as well. This ModuleScript defines the behavior for the logger. If not found, the logger will default to the Debug log level for all operations.

For instance, this could be a script located at ReplicatedStorage.MyGameConfig.LogConfig. There just needs to be some LogConfig-named ModuleScript within ReplicatedStorage.

Below are a few examples of possible LogConfig ModuleScripts:

-- Set "Info" as default log level for all environments:
return "Info"
-- To set a configuration that is different while in Studio:
return {
	Studio = "Debug";
	Other = "Warn"; -- "Other" can be anything other than Studio (e.g. could be named "Default")
}
-- Fine-tune between server and client:
return {
	Studio = {
		Server = "Info";
		Client = "Debug";
	};
	Other = "Warn";
}
-- Fine-tune based on PlaceIds:
return {
	Studio = {
		Server = "Info";
		Client = "Debug";
	};
	Other = {
		PlaceIds = {123456, 234567}
		Server = "Severe";
		Client = "Warn";
	};
}
-- Fine-tune based on GameIds:
return {
	Studio = {
		Server = "Info";
		Client = "Debug";
	};
	Other = {
		GameIds = {123456, 234567}
		Server = "Severe";
		Client = "Warn";
	};
}
-- Example of full-scale config with multiple environments:
return {
	Studio = {
		Server = "Debug";
		Client = "Debug";
	};
	Dev = {
		PlaceIds = {1234567};
		Server = "Info";
		Client = "Info";
	};
	Prod = {
		PlaceIds = {2345678};
		Server = "Severe";
		Client = "Warn";
	};
	Default = "Info";
}

Types

LogItem

interface LogItem {
Log(
messageany,
customDatatable?
)--

Log the message

Every(nnumber)--

Log only every n times

AtMostEvery(
nnumber,
timeUnitTimeUnit
)--

Log only every n TimeUnit

Throw()--

Throw an error

Wrap()--

Returns a function that can be called which will log out the given arguments

Assert(
conditionboolean,
args...
)--

Assert the condition

}

TimeUnit

interface TimeUnit {
Millisecondsnumber
Seecondsnumber
Minutesnumber
Hoursnumber
Daysnumber
Weeksnumber
Monthsnumber
Yearsnumber
}

Properties

TimeUnit

This item is read only and cannot be modified. Read Only
Log.TimeUnit: TimeUnit

Level

This item is read only and cannot be modified. Read Only
Log.Level: Level

Functions

new

Log.new() → Log

Construct a new Log object.

warning

This should only be called once per script.

At

Log:At(levelLogLevel) → LogItem

Types

interface Level {
Tracenumber
Debugnumber
Infonumber
Warningnumber
Errornumber
Fatalnumber
}

AtTrace

Log:AtTrace() → LogItem

Get a LogItem at the Trace log level.

AtDebug

Log:AtDebug() → LogItem

Get a LogItem at the Debug log level.

AtInfo

Log:AtInfo() → LogItem

Get a LogItem at the Info log level.

AtWarning

Log:AtWarning() → LogItem

Get a LogItem at the Warning log level.

AtError

Log:AtError() → LogItem

Get a LogItem at the Error log level.

AtFatal

Log:AtFatal() → LogItem

Get a LogItem at the Fatal log level.

Assert

Log:Assert(
conditionboolean,
...any
) → ()

Asserts the condition and then logs the following arguments at the Error level if the condition fails.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Construct a new Log object.\n\n:::warning\nThis should only be called once per script.\n:::",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Log"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 514,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "At",
            "desc": "",
            "params": [
                {
                    "name": "level",
                    "desc": "",
                    "lua_type": "LogLevel"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "LogItem"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 546,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "AtTrace",
            "desc": "Get a LogItem at the Trace log level.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "LogItem"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 554,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "AtDebug",
            "desc": "Get a LogItem at the Debug log level.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "LogItem"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 562,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "AtInfo",
            "desc": "Get a LogItem at the Info log level.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "LogItem"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 570,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "AtWarning",
            "desc": "Get a LogItem at the Warning log level.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "LogItem"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 578,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "AtError",
            "desc": "Get a LogItem at the Error log level.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "LogItem"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 586,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "AtFatal",
            "desc": "Get a LogItem at the Fatal log level.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "LogItem"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 594,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "Assert",
            "desc": "Asserts the condition and then logs the following\narguments at the Error level if the condition\nfails.",
            "params": [
                {
                    "name": "condition",
                    "desc": "",
                    "lua_type": "boolean"
                },
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 605,
                "path": "modules/log/init.lua"
            }
        }
    ],
    "properties": [
        {
            "name": "TimeUnit",
            "desc": "",
            "lua_type": "TimeUnit",
            "readonly": true,
            "source": {
                "line": 491,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "Level",
            "desc": "",
            "lua_type": "Level",
            "readonly": true,
            "source": {
                "line": 497,
                "path": "modules/log/init.lua"
            }
        }
    ],
    "types": [
        {
            "name": "LogItem",
            "desc": "",
            "fields": [
                {
                    "name": "Log",
                    "lua_type": "(message: any, customData: table?)",
                    "desc": "Log the message"
                },
                {
                    "name": "Every",
                    "lua_type": "(n: number)",
                    "desc": "Log only every `n` times"
                },
                {
                    "name": "AtMostEvery",
                    "lua_type": "(n: number, timeUnit: TimeUnit)",
                    "desc": "Log only every `n` `TimeUnit`"
                },
                {
                    "name": "Throw",
                    "lua_type": "()",
                    "desc": "Throw an error"
                },
                {
                    "name": "Wrap",
                    "lua_type": "()",
                    "desc": "Returns a function that can be called which will log out the given arguments"
                },
                {
                    "name": "Assert",
                    "lua_type": "(condition: boolean, args: ...)",
                    "desc": "Assert the condition"
                }
            ],
            "source": {
                "line": 461,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "TimeUnit",
            "desc": "",
            "fields": [
                {
                    "name": "Milliseconds",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Seeconds",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Minutes",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Hours",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Days",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Weeks",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Months",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Years",
                    "lua_type": "number",
                    "desc": ""
                }
            ],
            "source": {
                "line": 474,
                "path": "modules/log/init.lua"
            }
        },
        {
            "name": "Level",
            "desc": "",
            "fields": [
                {
                    "name": "Trace",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Debug",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Info",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Warning",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Error",
                    "lua_type": "number",
                    "desc": ""
                },
                {
                    "name": "Fatal",
                    "lua_type": "number",
                    "desc": ""
                }
            ],
            "source": {
                "line": 485,
                "path": "modules/log/init.lua"
            }
        }
    ],
    "name": "Log",
    "desc": "Log class for logging to the AnalyticsService (e.g. PlayFab). The API\nis based off of Google's [Flogger](https://google.github.io/flogger/)\nfluent logging API.\n\n```lua\nlocal Log = require(somewhere.Log)\nlocal logger = Log.new()\n\n-- Log a simple message:\nlogger:AtInfo():Log(\"Hello world!\")\n\n-- Log only every 3 messages:\nfor i = 1,20 do\n\tlogger:AtInfo():Every(3):Log(\"Hi there!\")\nend\n\n-- Log only every 1 second:\nfor i = 1,100 do\n\tlogger:AtInfo():AtMostEvery(3, Log.TimeUnit.Seconds):Log(\"Hello!\")\n\ttask.wait(0.1)\nend\n\n-- Wrap the above example into a function:\nlocal log = logger:AtInfo():AtMostEvery(3, Log.TimeUnit.Seconds):Wrap()\nfor i = 1,100 do\n\tlog(\"Hello!\")\n\ttask.wait(0.1)\nend\n\n-- Assertion:\nlogger:Assert(typeof(32) == \"number\", \"Somehow 32 is no longer a number\")\n```\n\n------------\n\n### LogConfig\n\nA LogConfig ModuleScript is expected to exist somewhere within ReplicatedStorage\nas well. This ModuleScript defines the behavior for the logger. If not found,\nthe logger will default to the Debug log level for all operations.\n\nFor instance, this could be a script located at `ReplicatedStorage.MyGameConfig.LogConfig`. There\njust needs to be some `LogConfig`-named ModuleScript within ReplicatedStorage.\n\nBelow are a few examples of possible LogConfig ModuleScripts:\n\n```lua\n-- Set \"Info\" as default log level for all environments:\nreturn \"Info\"\n```\n\n```lua\n-- To set a configuration that is different while in Studio:\nreturn {\n\tStudio = \"Debug\";\n\tOther = \"Warn\"; -- \"Other\" can be anything other than Studio (e.g. could be named \"Default\")\n}\n```\n\n```lua\n-- Fine-tune between server and client:\nreturn {\n\tStudio = {\n\t\tServer = \"Info\";\n\t\tClient = \"Debug\";\n\t};\n\tOther = \"Warn\";\n}\n```\n\n```lua\n-- Fine-tune based on PlaceIds:\nreturn {\n\tStudio = {\n\t\tServer = \"Info\";\n\t\tClient = \"Debug\";\n\t};\n\tOther = {\n\t\tPlaceIds = {123456, 234567}\n\t\tServer = \"Severe\";\n\t\tClient = \"Warn\";\n\t};\n}\n```\n\n```lua\n-- Fine-tune based on GameIds:\nreturn {\n\tStudio = {\n\t\tServer = \"Info\";\n\t\tClient = \"Debug\";\n\t};\n\tOther = {\n\t\tGameIds = {123456, 234567}\n\t\tServer = \"Severe\";\n\t\tClient = \"Warn\";\n\t};\n}\n```\n\n```lua\n-- Example of full-scale config with multiple environments:\nreturn {\n\tStudio = {\n\t\tServer = \"Debug\";\n\t\tClient = \"Debug\";\n\t};\n\tDev = {\n\t\tPlaceIds = {1234567};\n\t\tServer = \"Info\";\n\t\tClient = \"Info\";\n\t};\n\tProd = {\n\t\tPlaceIds = {2345678};\n\t\tServer = \"Severe\";\n\t\tClient = \"Warn\";\n\t};\n\tDefault = \"Info\";\n}\n```",
    "realm": [
        "Server"
    ],
    "source": {
        "line": 448,
        "path": "modules/log/init.lua"
    }
}