Sims Log Enabler

SCREENSHOTS
Downloaded 1,795 times 65 Thanks 32 Favourited 25,911 Views
 Say Thanks!
February 06, 2020 - Scumbumbo has passed on but his work and memories are still being kept alive by many of those whose lives he has touched. To download fixes for his mods, please check https://scumbumbomods.com/ and please direct any questions or concerns to the discord channel linked there.
Uploaded: 31st Jan 2018 at 12:33 PM
Updated: 3rd Feb 2018 at 10:38 PM - Added options for TIMESTAMP and INCLUDE_OWNER
NOTE: This is an advanced modders tool.
It is not intended for players or casual modders.

Description

I'm not sure why no one has ever enabled the logging features of the game, or if they have they haven't released a script for it. So I decided to write one up for myself and share it with all of you. This is probably most useful for script modders; however, there is a lot of information logged that can be useful for XML modders as well.

This is a script mod which modifies the logging functions in The Sims 4 to enable the game log file features. It's a bit different from most script mods in that it is uncompiled, which changes how it should be installed a bit. I chose to release this as an uncompiled mod as it is something that modders will want to be able to easily turn on and off, and possibly change some of the in-script options. It does not significantly slow down the mod, it will be compiled to bytecode when loaded by the game it just won't be optimized.

Installation

Installing the mod is as simple as extracting the mod into your mods folder. Note that it MUST be in a subfolder and that subfolder MUST include the scripts folder. If you just extract the zip file as is, it should setup fine, creating a folder named "Sims Log Enabler" in your mods folder, which contains the "scripts" folder with the uncompiled script.

So the script file should end up at:
....\Mods\Sims Log Enabler\scripts\sims_log_enabler.py

The actual log files will be placed into the Sims Log Enabler folder.

Usage

There is a HUGE amount of logging going on in the game, so by default, logging is disabled at startup. There are two cheat console commands to enable and disable logging, but if you need logging enabled from the start there is a configuration variable that can be set for that in the script file (more on that later).

The two commands are:
logs.enable
and
logs.disable

Wow, almost too simple! There is one option that can be specified to the enable command. By default, when a log file is opened it will be overwritten because otherwise the log files can grow to huge sizes. Typically you will want this behavior. However, if you want to append to existing log files, you can specify the append flag on the enable command:
logs.enable append

Log files aren't actually created or opened until they are used. There are many log groups used in the game, and each group will have it's own log file. Some of these logs are more useful than others, and some you'll probably never use.

Each of the log files can contain entries at the specific log levels INFO, DEBUG, WARN, and ERROR. The log level for each entry is noted at the start of the line in square brackets. Immediately following that you may see an optional "owner" tag in square brackets, this is just the user at EA who is responsible for the particular section of code that is producing that log info. Not very useful, but I included it for completness.

Configuration

There are three configuration variables at the start of the Python script which can be altered freely. Since these are processed when the game initially loads the mod they will only take effect when the game is restarted (so change them before starting the game).

ENABLE_LOGGING_AT_STARTUP
A lot of information is logged at the game startup, for example when the XML tuning is loaded any errors or warnings found in the XML is logged. To get these logs you will need to enable logging from startup (you won't have to do a log.enable command, although you can always turn logging off and back on again).

To enable logging at startup, simply alter the ENABLE_LOGGING_AT_STARTUP entry as follows:
Code:
ENABLE_LOGGING_AT_STARTUP = True


Remember to turn it back off again once you no longer need logs enabled at startup. The log files can get really big, and will slow down your game unless you have a fast hard drive.

INCLUDE_TIMESTAMPS (new in v2)
Including timestamps will add the current system time to each line of the logs. This can be helpful when determining the order of events when comparing multiple log group files, or if you are appending to existing logs.

To turn on timestamps, alter the INCLUDE_TIMESTAMPS entry as follows:
Code:
INCLUDE_TIMESTAMPS = True


INCLUDE_OWNER_INFO (new in v2)
By default, owner info is included on each log line. Disabling this option will remove the ownership tags from log lines, so you won't see the usernames of EA coders who are responsible for various sections of the code.

To disable ownership info, alter the INCLUDE_OWNER_INFO entry as follows:
Code:
INCLUDE_OWNER_INFO= False


USE_LINE_BUFFERING
By default, line buffering is off. Using line buffering will slow down logging a bit, but will enable other programs (for instance Notepad++) which can detect that a file has changed and reload it on the fly. Without line buffering on, changes to the log files will not be apparant to these programs until the log file has been closed and reopened.

To turn on line buffering, alter the USE_LINE_BUFFERING entry as follows:
Code:
USE_LINE_BUFFERING = True


ENABLE_AUTO_FLUSH
This option is of limited use and is primarily intended for script modders. The only reason you will want to enable this mode is if you are concerned with losing log entries due to the game crashing to desktop. Enabling auto flush will cause the log files to be flushed to disk every time a log entry is written. This option will slow down logging a bit.

To turn on auto flush, (you guessed it) alter the ENABLE_AUTO_FLUSH entry as follows:
Code:
ENABLE_AUTO_FLUSH = True


Disabling Specific Log Levels
If you have no interest in a particular log level, they can be disabled (or re-enabled) by modifying them at the end of the script. Simply comment out the log levels you wish to turn off. For instance, to disable the INFO and DEBUG log levels, you would modify these lines to appear as follows:
Code:
#sims4.log.Logger.info = info
#sims4.log.Logger.debug = debug
sims4.log.Logger.warn = warn
sims4.log.Logger.error = error


The two lines immediately after these log level settings should not be changed. These enable some missing code in the SimInfo class of the game so that the logs will contain the full name of sims instead of a blank.

(Script Modders) Creating Your Own Log Group

Script modders may wish to utilize the game's log file processing now that it is accessible via this mod. Creating your own log file group is very easy and will allow you to include log entries in your mod and leave them in when packaging the mod for players. Your mod will only produce actual logging when you have this mod installed, and if it's not installed (or enabled) the game's empty logging features will simply ignore the log entries without the possibility of creating exception errors.

To create your own log group, you simply include the sims4.log module and create an instance of the Logger class. I suggest using the EA standard and calling the log "logger". If you want your log entries to include an owner tag, include that in the default_owner argument to the Logger class. For instance:
Code:
import sims4.log
logger = sims4.log.Logger('My Mod Name', default_owner='Scumbumbo')


In your code, you can then call any of the log level methods (info, debug, warn or error) to produce log output, like so:
Code:
def MyFunction(obj=None, value):
    if obj is None:
        logger.warn('MyFunction called with no obj')

    # You can specify formatting and arguments to the logger functions, too
    logger.debug('Setting object {} value {}', obj, value)
    if not obj.do_something(value):
        logger.error('do_something failed')

    # Watch out for extraneous {}'s in a log entry.
    some_dict = {'red': 1, 'blue': 2}
    my_message = 'This will throw an exception: some_dict={}'.format(some_dict)
    logger.info(my_message, owner='me')

    # I consider the above a bug; however, it is consistent with the game's logging code.  To work
    #   around this, EA does the following:  If you must preformat a line that contains the {} characters
    #   you must not have a default_owner or owner keyword arg included, and you must not include
    #   other formatting and arguments.
    logger.info(If we include {} here, we can safely log this', some_dict, owner='okaytouseowner')
    logger.info('The next one will work too (assuming no default_owner is present) as no args are included')
    logger.info(my_message)

# Finally, logging lines which run when the module is loaded may or may not work
#   depending on what script gets loaded first.  I haven't found any method to guarantee
#   that sims_log_enabler gets loaded first.
logger.info('This log entry probably won't show up as sims_log_enabler has not loaded yet')


I think that covers most everything. Feel free to shout out if you have any questions, of course!