Logging - EverestAPI/Resources GitHub Wiki

Logging is a great resource for debugging, but care should be taken to control the amount of logs you are sending in release builds. Writing to the log file consumes game resources, and spamming messages to a log makes it harder to read and less useful for other users and developers.

Using the Logger class

Everest provides a static Logger class that you can use to write messages to log.txt. Logs are constructed of three parts:

  • Level: the urgency of the message.
    • Verbose < Debug < Info < Warn < Error
    • Defaults to Verbose if a level is not provided.
  • Tag: the category of the message. Tags are used to filter which logs get printed.
    • Should be unique, but not long enough to clutter the log.
    • The minimum level to log a tag is Info by default. Can be overridden with SetLogLevel.
  • Message: the actual log message.

⚠️ Based on the above, a log command with a default log level and tag level will not be printed. This is intentional to reduce log spam. If you want your logs to be printed, specify a log level and/or change the minimum level of the tag.

if (level.Tracker.GetEntity<Player>() == null) {
    Logger.Log(LogLevel.Warn, "MyMod/MyCustomEntity", "Oh no, can't find the player!");
    return false;
}

In this example, Warn is higher than our tag's minimum level, so the log will be printed:

(07/30/2022 21:09:02) [Everest] [Warn] [MyMod/MyCustomEntity] Oh no, can't find the player!

Here is a quick reference for common Logger features:

Logger.Log(LogLevel level, string tag, string str)          // Logs a message (if minimum level met).
Logger.Log(string tag, string str)                          // Logs a message at Verbose level.

Logger.LogDetailed(LogLevel level, string tag, string str)  // Logs a message with a stack trace.
Logger.LogDetailed(string tag, string str)                  // Logs a message with a stack trace at Verbose level.

Logger.SetLogLevel(string tagPrefix, LogLevel minimumLevel) // Sets the minimum log level for a tag prefix.

Note that SetLogLevel expects a tag prefix. A common setup is to have a single SetLogLevel call in your module's Load() function that applies to all of your tags, e.g. SetLogLevel("MyMod", LogLevel.Info). This lets you easily switch between Verbose for debugging and Info for release builds.

Other Logging Tools

  • Use the logdetours debug command to print all active hooks.
  • Use the setloglevel [tagPrefix] [level] debug command to override a tag's minimum log level.
    • This can also be set persistently by editing the LogLevels key in the Everest settings file.
  • Use the --loglevel command line argument to set the default print level for all tags at launch.
  • Console.WriteLine() will print directly to the log file.
    • ⚠️ Handy for quick debugging, but do not use in a release build of your mod.
⚠️ **GitHub.com Fallback** ⚠️