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

From Stardew Valley Wiki
Jump to navigation Jump to search
(→‎Logging: Adding the "Example Log" section.)
(→‎Verbose logging: add example config)
 
(6 intermediate revisions by 2 users not shown)
Line 17: Line 17:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
will log something like this:
+
...will produce log entries with a timestamp, log level, mod name, and message:
  
 
<div style="font-family: monospace;">
 
<div style="font-family: monospace;">
<span style="color:#666;">[18:00:00 TRACE Mod Name] a trace message</span><br />
+
<span style="color:#666;">[18:00:00 TRACE NameOfMod] a trace message</span><br />
<span style="color:#666;">[18:00:00 DEBUG Mod Name] a debug message</span><br />
+
<span style="color:#666;">[18:00:00 DEBUG NameOfMod] a debug message</span><br />
<span style="color:black;">[18:00:00 INFO  Mod Name] an info message</span><br />
+
<span style="color:black;">[18:00:00 INFO  NameOfMod] an info message</span><br />
<span style="color:darkorange;">[18:00:00 WARN  Mod Name] a warning message</span><br />
+
<span style="color:darkorange;">[18:00:00 WARN  NameOfMod] a warning message</span><br />
<span style="color:red;">[18:00:00 ERROR Mod Name] an error message</span>
+
<span style="color:red;">[18:00:00 ERROR NameOfMod] an error message</span>
 
</div>
 
</div>
  
===Where to Find the Log File===
+
The text won't appear in color in the log file, but will appear in color in SMAPI console window as the game runs.
 
 
The log file exists in different locations based on your operating system:
 
 
 
Windows (Steam/GOG): <span style="font-family: monospace;">%appdata%\StardewValley\ErrorLogs\SMAPI-latest.txt</span>
 
 
 
Windows (Xbox app): <span style="font-family: monospace;">%localappdata%\Packages\ConcernedApe.StardewValleyPC_0c8vynj4cqe4e\LocalCache\Roaming\StardewValley\ErrorLogs\SMAPI-latest.txt</span>
 
 
 
macOS: <span style="font-family: monospace;">~/.config/StardewValley/ErrorLogs/SMAPI-latest.txt</span>
 
 
 
Linux: <span style="font-family: monospace;">~/.config/StardewValley/ErrorLogs/SMAPI-latest.txt</span>
 
 
 
Android: Find the <span style="font-family: monospace;">SMAPI-latest.txt</span> file in the <span style="font-family: monospace;">StardewValley/ErrorLogs</span> folder on your internal storage.
 
 
 
If Stardew Valley crashed, the log file will be named <span style="font-family: monospace;">SMAPI-crash.txt</span> instead of <span style="font-family: monospace;">SMAPI-latest.txt</span>.
 
  
 
===Log levels===
 
===Log levels===
Line 52: Line 38:
 
|-
 
|-
 
| <samp>Trace</samp>
 
| <samp>Trace</samp>
| 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.
+
| Tracing info intended for developers, usually low-level 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.
 
|-
 
|-
 
| <samp>Debug</samp>
 
| <samp>Debug</samp>
| Troubleshooting info that may be relevant to the player.
+
| Troubleshooting details that may be relevant to the player.
 
|-
 
|-
 
| <samp>Info</samp>
 
| <samp>Info</samp>
Line 61: Line 47:
 
|-
 
|-
 
| <samp>Warn</samp>
 
| <samp>Warn</samp>
| An issue the player should be aware of. This should be used rarely.
+
| Potential problems that the player should be aware of. This should be used rarely.
 
|-
 
|-
 
| <samp>Error</samp>
 
| <samp>Error</samp>
Line 67: Line 53:
 
|-
 
|-
 
| <samp>Alert</samp>
 
| <samp>Alert</samp>
| 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.
+
| Important information to highlight for the player when player action is needed (''e.g.,'' new version available). This is mainly meant for SMAPI itself, and should almost never be used by mods.
 
|}
 
|}
  
Line 77: Line 63:
  
 
===Verbose logging===
 
===Verbose logging===
You can have messages appear in the log (and console in the ''for developers'' version) only if SMAPI's verbose logging option is enabled. This is meant for diagnostic information that's sometimes needed to troubleshoot, but doesn't need to be logged in most cases. Players can enable verbose logging by editing the <samp>smapi-internal/StardewModdingAPI.config.json</samp> file.
+
You can have messages appear in the log (and console in the ''for developers'' version) only if SMAPI's verbose logging option is enabled. This is meant for diagnostic information that's sometimes needed to troubleshoot, but doesn't need to be logged in most cases.
  
 
There are two ways to use it:
 
There are two ways to use it:
Line 89: Line 75:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Example Log===
+
Players can enable verbose logging by adding your mod ID to the <samp>VerboseLogging</samp> field in the <samp>smapi-internal/StardewModdingAPI.config.json</samp> file. For example, this enables it for SMAPI and Content Patcher:
 
+
<syntaxhighlight lang="js">
Here's the start of an example <span style="font-family: monospace;">SMAPI-latest.txt</span> log file:
+
"VerboseLogging": [ "SMAPI", "Pathoschild.ContentPatcher" ],
 +
</syntaxhighlight>
  
<pre>[23:26:19 INFO  SMAPI] SMAPI 3.13.4 with Stardew Valley 1.5.6 (build 22018) on Microsoft Windows 10 Pro
+
==FAQs==
[23:26:19 INFO  SMAPI] Mods go here: C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods
+
===Where is the log file created?===
[23:26:19 TRACE SMAPI] Log started at 2022-02-25T05:26:19 UTC
+
It's output to the game's <samp>ErrorLogs</samp> data folder. See [https://smapi.io/log/ smapi.io/log] for help finding the log file, and you can also upload your log there to see what it looks like. The log file is always created (even if there are no errors) and it's written continuously (so you don't need to exit the game to see the log so far).
[23:26:24 DEBUG SMAPI] Waiting for game to launch...
 
[23:26:25 TRACE game] NoFpsCap: This feature is not available
 
[23:26:26 DEBUG SMAPI] Loading mod metadata...
 
[23:26:26 DEBUG SMAPI] Loading mods...
 
[23:26:26 TRACE SMAPI]   CJB Cheats Menu (from Mods\CJBCheatsMenu\CJBCheatsMenu.dll)...
 
[23:26:27 TRACE SMAPI]      Rewrote CJBCheatsMenu.dll for OS...
 
[23:26:27 TRACE SMAPI]      Rewrote CJBCheatsMenu.dll to fix SpriteBatch methods...
 
[23:26:27 TRACE SMAPI]    CJB Item Spawner (from Mods\CJBItemSpawner\CJBItemSpawner.dll)...
 
[23:26:27 TRACE SMAPI]      Rewrote CJBItemSpawner.dll for OS...
 
[23:26:27 TRACE SMAPI]    TestSDVMod (from Mods\ClassLibrary1\ClassLibrary1.dll)...
 
[23:26:27 TRACE SMAPI]    Console Commands (from Mods\ConsoleCommands\ConsoleCommands.dll)...
 
[23:26:27 TRACE SMAPI]    Error Handler (from Mods\ErrorHandler\ErrorHandler.dll)...
 
[23:26:27 TRACE SMAPI]      Detected game patcher in assembly ErrorHandler.dll.
 
[23:26:27 TRACE SMAPI]    Save Backup (from Mods\SaveBackup\SaveBackup.dll)...
 
[23:26:27 INFO  SMAPI] Loaded 6 mods:
 
[23:26:27 INFO  SMAPI]    CJB Cheats Menu 1.29.1 by CJBok and Pathoschild | Simple in-game cheats menu!
 
[23:26:27 INFO  SMAPI]    CJB Item Spawner 2.1.4 by CJBok and Pathoschild | Simple in-game item spawner!
 
[23:26:27 INFO  SMAPI]    Console Commands 3.13.4 by SMAPI | Adds SMAPI console commands that let you manipulate the game.
 
[23:26:27 INFO  SMAPI]    Error Handler 3.13.4 by SMAPI | Handles some common vanilla errors to log more useful info or avoid breaking the game.
 
[23:26:27 INFO  SMAPI]    Save Backup 3.13.4 by SMAPI | Automatically backs up all your saves once per day into its folder.</pre>
 

Latest revision as of 19:04, 9 May 2023

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 produce log entries with a timestamp, log level, mod name, and message:

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

The text won't appear in color in the log file, but will appear in color in SMAPI console window as the game runs.

Log levels

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

level purpose
Trace Tracing info intended for developers, usually low-level 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 details that may be relevant to the player.
Info Info relevant to the player. This should be used judiciously.
Warn Potential problems that 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 is mainly meant for SMAPI itself, and should almost never be used by mods.

Log once

The LogOnce method lets you log a message just like the above, but only once per game launch. For example, you can use it to show a compatibility warning for an API call:

this.Monitor.LogOnce("Some Mod Name used the deprecated X API.", LogLevel.Warn);

Verbose logging

You can have messages appear in the log (and console in the for developers version) only if SMAPI's verbose logging option is enabled. This is meant for diagnostic information that's sometimes needed to troubleshoot, but doesn't need to be logged in most cases.

There are two ways to use it:

// log a TRACE message if verbose logging is enabled
this.Monitor.VerboseLog("This will only appear if verbose logging is enabled.");

// check the verbose option
if (this.Monitor.IsVerbose)
   this.Monitor("This will only appear if verbose logging is enabled.", LogLevel.Trace);

Players can enable verbose logging by adding your mod ID to the VerboseLogging field in the smapi-internal/StardewModdingAPI.config.json file. For example, this enables it for SMAPI and Content Patcher:

"VerboseLogging": [ "SMAPI", "Pathoschild.ContentPatcher" ],

FAQs

Where is the log file created?

It's output to the game's ErrorLogs data folder. See smapi.io/log for help finding the log file, and you can also upload your log there to see what it looks like. The log file is always created (even if there are no errors) and it's written continuously (so you don't need to exit the game to see the log so far).