Difference between revisions of "Modding:Modder Guide/APIs/Logging"

From Stardew Valley Wiki
Jump to navigation Jump to search
(move content from Modding:Modder Guide/APIs (only author is Pathoschild))
 
(expand)
Line 1: Line 1:
 
{{../../header}}
 
{{../../header}}
  
===Logging===
+
The monitor API, available through <tt>this.Monitor</tt> in your mod class, mainly lets you write messages to the console window and log file.
Your mod can write messages to the console window and log file using the monitor. For example, this code:
+
 
 +
==Logging==
 +
===Overview===
 +
You can log messages by calling <tt>this.Monitor.Log</tt> with a string message and a log level. These messages will appear in the SMAPI console and log file.
 +
 
 +
For example, this code:
  
 
<source lang="c#">
 
<source lang="c#">
Line 22: Line 27:
 
</div>
 
</div>
  
Note that <tt>LogLevel.Trace</tt> messages won't appear in the console window by default, they'll only be written to the log file. Trace messages are for troubleshooting details that are useful when someone sends you their error log, but which the player normally doesn't need to see. (You can see trace messages in the console if you install the "SMAPI for developers" version.)
+
===Log levels===
 +
The log level is ''semantic'', meaning that the level itself conveys information (a <tt>Trace</tt> levels means something different from a <tt>Error</tt> level). You should use the log level that's closest to your message's purpose:
 +
 
 +
{| class="wikitable"
 +
|-
 +
! level
 +
! purpose
 +
|-
 +
| <tt>Trace</tt>
 +
| Tracing info intended for developers, usually troubleshooting details that are useful when someone sends you their error log. Trace messages won't appear in the console window by default (unless you have the "SMAPI for developers" version), though they're always written to the log file.
 +
|-
 +
| <tt>Debug</tt>
 +
| Troubleshooting info that may be relevant to the player.
 +
|-
 +
| <tt>Info</tt>
 +
| Info relevant to the player. This should be used judiciously.
 +
|-
 +
| <tt>Warn</tt>
 +
| An issue the player should be aware of. This should be used rarely.
 +
|-
 +
| <tt>Error</tt>
 +
| A message indicating something went wrong.
 +
|-
 +
| <tt>Alert</tt>
 +
| Important information to highlight for the player when player action is needed (e.g. new version available). This should be used rarely to avoid alert fatigue.
 +
|}
 +
 
 +
==Emergency shutdown==
 +
In extraordinary cases, you can cause an emergency game shutdown by calling <tt>this.Monitor.ExitGameImmediately</tt>. '''This will immediately crash the game, bypassing all SMAPI error checks.''' This should only be invoked when an irrecoverable error happens that risks save corruption or game-breaking bugs.

Revision as of 04:31, 28 May 2018

Creating SMAPI mods SMAPI mascot.png


Modding:Index

The monitor API, available through this.Monitor in your mod class, mainly lets you write messages to the console window and log file.

Logging

Overview

You can log messages by calling this.Monitor.Log with a string message and a log level. These messages will appear in the SMAPI console and log file.

For example, this code:

this.Monitor.Log("a trace message", LogLevel.Trace);
this.Monitor.Log("a debug message", LogLevel.Debug);
this.Monitor.Log("an info message", LogLevel.Info);
this.Monitor.Log("a warning message", LogLevel.Warn);
this.Monitor.Log("an error message", LogLevel.Error);

will log something like this:

[18:00:00 TRACE Mod Name] a trace message
[18:00:00 DEBUG Mod Name] a debug message
[18:00:00 INFO Mod Name] an info message
[18:00:00 WARN Mod Name] a warning message
[18:00:00 ERROR Mod Name] an error message

Log levels

The log level is semantic, meaning that the level itself conveys information (a Trace levels means something different from a Error level). You should use the log level that's closest to your message's purpose:

level purpose
Trace Tracing info intended for developers, usually troubleshooting details that are useful when someone sends you their error log. Trace messages won't appear in the console window by default (unless you have the "SMAPI for developers" version), though they're always written to the log file.
Debug Troubleshooting info that may be relevant to the player.
Info Info relevant to the player. This should be used judiciously.
Warn An issue the player should be aware of. This should be used rarely.
Error A message indicating something went wrong.
Alert Important information to highlight for the player when player action is needed (e.g. new version available). This should be used rarely to avoid alert fatigue.

Emergency shutdown

In extraordinary cases, you can cause an emergency game shutdown by calling this.Monitor.ExitGameImmediately. This will immediately crash the game, bypassing all SMAPI error checks. This should only be invoked when an irrecoverable error happens that risks save corruption or game-breaking bugs.