Log
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:
(
message:
any
,
customData:
table?
)
--
Log the message
Every:
(
n:
number
)
--
Log only every n
times
Throw:
(
)
--
Throw an error
Wrap:
(
)
--
Returns a function that can be called which will log out the given arguments
Assert:
(
condition:
boolean
,
args:
...
)
--
Assert the condition
}
TimeUnit
interface
TimeUnit {
Milliseconds:
number
Seeconds:
number
Minutes:
number
Hours:
number
Days:
number
Weeks:
number
Months:
number
Years:
number
}
Properties
TimeUnit
This item is read only and cannot be modified. Read OnlyLog.TimeUnit:
TimeUnit
Level
This item is read only and cannot be modified. Read OnlyLog.Level:
Level
Functions
new
Construct a new Log object.
warning
This should only be called once per script.
At
Types
interface
Level {
Trace:
number
Debug:
number
Info:
number
Warning:
number
Error:
number
Fatal:
number
}
AtTrace
Get a LogItem at the Trace log level.
AtDebug
Get a LogItem at the Debug log level.
AtInfo
Get a LogItem at the Info log level.
AtWarning
Get a LogItem at the Warning log level.
AtError
Get a LogItem at the Error log level.
AtFatal
Get a LogItem at the Fatal log level.
Assert
Log:
Assert
(
condition:
boolean
,
...:
any
) →
(
)
Asserts the condition and then logs the following arguments at the Error level if the condition fails.