Logging in UnrealScript
Here’s the translation of the Go logging example to UnrealScript, formatted in Markdown suitable for Hugo:
class LoggingExample extends Object;
var Logger StandardLogger;
var Logger CustomLogger;
function Init()
{
// Simply invoking functions like `Log` uses the standard logger
`log("standard logger");
// Loggers can be configured with flags to set their output format
// In UnrealScript, we can use the `LogLevel` enum to control log output
`log("with debug info", 'Debug');
// It also supports emitting the file name and line from which the log function is called
`log("with file/line", 'Verbose');
// It may be useful to create a custom logger
CustomLogger = new class'Logger';
CustomLogger.LogPrefix = "my:";
CustomLogger.Log("from custom logger");
// We can set the prefix on existing loggers
CustomLogger.LogPrefix = "ohmy:";
CustomLogger.Log("from custom logger");
// Loggers can have custom output targets in UnrealScript as well
// This would typically be done by redirecting log output to a file
class'FileLogger'.static.StartFileLogging();
`log("This will be logged to a file");
class'FileLogger'.static.StopFileLogging();
// UnrealScript doesn't have built-in structured logging like Go's slog
// However, we can simulate it by formatting our log messages
LogStructured("hi there");
LogStructured("hello again", "key=val|age=25");
}
function LogStructured(string Message, optional string Attributes)
{
local string FormattedMessage;
FormattedMessage = "{\"time\":\"" $ class'WorldInfo'.static.GetCurrentDateTime() $ "\",";
FormattedMessage $= "\"level\":\"INFO\",\"msg\":\"" $ Message $ "\"";
if (Attributes != "")
{
FormattedMessage $= "," $ Attributes;
}
FormattedMessage $= "}";
`log(FormattedMessage);
}
defaultproperties
{
}
This UnrealScript code demonstrates logging functionality similar to the Go example. Here’s an explanation of the key points:
UnrealScript uses the
log
function for basic logging. We can use the backtick syntax`log
as a shorthand.UnrealScript doesn’t have the same fine-grained control over log formatting as Go, but we can use the
LogLevel
enum to control the verbosity of logs.Custom loggers can be created by instantiating a new
Logger
class and setting its properties.File logging in UnrealScript is typically handled by the
FileLogger
class.UnrealScript doesn’t have built-in structured logging like Go’s
slog
. We simulate this by creating a customLogStructured
function that formats the log message as JSON.The
WorldInfo.GetCurrentDateTime()
function is used to get the current time for our structured logs.
To use this code in an Unreal Engine project:
- Create a new script file named
LoggingExample.uc
in your project’s Scripts folder. - Copy the above code into the file.
- Compile the script in the Unreal Editor.
- You can then create an instance of
LoggingExample
and call itsInit()
function to see the logging in action.
Note that the exact output format and available logging features may vary depending on the specific version of Unreal Engine you’re using. This example provides a general approximation of the Go logging functionality within the constraints of UnrealScript.