Logging
Logging information can be output from IdleKit via the static Logs
class. The output varies depending on the underlying IIKLogger
implementation that is being used by Logs
.
Logs
It is a globally accessible class within IdleKit. It acts as a simple static wrapper for an IIKLogger
implementation which performs the actual logging functionality, and whose settings can be modified on demand. Functionality is provided to log at several different verbosity (i.e. information, warnings, errors, and exceptions) and categories.
Because Logs
is a static class, it is not bound and retrieved via the DependencyContainer
. Instead, the IIKLogger
implementation that you wish to use for your build should be bound to the DependencyContainer
and then registered with Logs
at the initialization of your game.
IIKLogger logger = new DefaultLogger();
Logs.Initialize(logger);
container.Bind<IIKLogger>().ToInstance(logger).AsSingleton();
Logs can be initialized by specifying custom implementations of IIKLogger
, ILoggingConfig
and IEnvironmentConfig
, otherwise a default implementation of IIKLogger
will be used.
Tip
Check a default implementation of IIKLogger
used by the IdleKit DefaultLogger
Log messages can be output as follows. See LogSeverity
and ILogCategory
.
ILogCategory Editor = new LogCategory("Editor", "EDITOR", "#FFFFFF", "#000000"
// Error
Logs.Log(LogSeverity.Error,$"Message", Editor);
// Warning
Logs.Log(LogSeverity.Warning,$"Message", Editor);
// Exception
Logs.Log(LogSeverity.Exception,$"Message", Editor);
Logs.HandleException(Exception);
// Log
Logs.Log(LogSeverity.Log,$"Message", Editor);
Default Log Categories
IdleKit uses several standard log categories, all of which are defined at the top of the static Logs
class. When using the static Logs
class to log to the console, IdleKit will choose a default log category for your message if one is not provided. The default categories are IdleKit.Debug
for logs fired during runtime and IdleKit.Debug.Editor
for logs fired in editor.
Creating Log Categories
To create your own log category, simply create an instance of the LogCategory
class with you required parameters, and then use it in the Logs.Log
method.
Log Environment and Enabling/Disabling Log Categories
IdleKit offers a way to enable or disable certain log categories at runtime by using the ToggleCategory
and ToggleCategoryId
methods in the static Logs
class. If you wish to disable certain log categories from the start, then the best way to do that would be with IdleKit's environment config system.
- Generate environment config assets using the IdleKit menu item:
- Generate logging environment config assets using the IdleKit menu item:
- Make sure to assign ids to all the newly generated logging assets:
As part of this process, IdleKit would have already marked these assets as addressables and assigned the CONFIG
label to them. IdleKit automatically loads assets marked with this label during the startup process in the Startup
class in the SetLoadAddressablesDataLoadPhases
method.
- Attach the
LoggingConfig
asset as well as your desired environment asset to your startup script in the inspector:
Now when you launch your IdleKit project the logging config that corresponds to the attached environment (DEV
, STAGE
, PROD
) will be applied.
Enabling/Disabling Log Categories
By default all IdleKit log categories are enabled. If you wish to disable one of the log categories you will need to add them to the Disabled Category Ids
array in your active logging environment asset. To do that easily for the default IdleKit log categories use the inspector button on the logging environment asset itself:
If you've created any of your own categories adding their ids to this array will disable them as well.
Controlling Logs Through a Debug Tool at Runtime
In the IdleKit Showcase project we provide an example of how log categories can be controlled from a debug tool, as part of our DebugConsoleWatcher
prefab that's controlled by the CanvasDebugConsoleWatcher
script.