File Logger

With the latest update I have added a custom file logger, which you can create in your code and save the logging results to your custom folder.

Let's create a logger with name TestLogger

This modded script should be created in Module #3 - 3_Game, so you can access that across the whole app. Otherwise you can choose a different place. I just think it's convenient to have it accessible from most of the codebase of DayZ Server.

// Create a modded class for DayZGame script
modded class DayZGame
{
    override void CRDTN_OnGameInit()
    {
        super.CRDTN_OnGameInit();
        CRDTN_FileLogger.CreateInstance("TestLogger");
        // instantiates the instance of FileLogger with key - TestLogger
        // 
    }
};

Congratulations, you have create your logger 😂 The file will be saved in $profile/CRDTN/Logs/TestLogger.log

If you want a different location for your logs, you must rewrite the constant

CFG_CRDTN_LogsFolder

Now you can access it from your code wherever you need by using the following code snippet.

CRDTN_FileLogger.GetInstance("TestLogger").Log("Some of your message");

You can also cache the reference for more convenient work like below.

class TestPlugin : CRDTN_PluginBase
{
    private ref CRDTN_FileLogger m_TestLogger;
    
    override void OnInit()
    {
        super.OnInit();
        m_TestLogger = CRDTN_FileLogger.GetInstance("TestLogger");
    }
    
    CRDTN_FileLogger GetTestLogger()
    {
        return m_TestLogger;
    }
};

TestPlugin GetTestPlugin()
{
    if (!GetGame().IsServer())
    {
        return NULL;
    }

    if (GetPluginManager() && GetPluginManager().GetPluginByType(TestPlugin))
    {
        return TestPlugin.Cast(GetPluginManager().GetPluginByType(TestPlugin));
    }

    return NULL;
}

CRDTN_TestLogger GetLogger()
{
    return GetTestPlugin().GetTestLogger();
}

If you have followed the code above, you now can just call in the code

GetLogger().Log("Some message");

Be aware, that the example showcases usage of PluginBase which is part of module #4 so you can use the GetLogger() method only within module #4 and #5.

Last updated