1

I can use NSSM to run my Python program as a service just fine, and I can run my Python program just fine on its own and get loguru output to a log file as well as console window - but when I run the program as a service with NSSM only the stdout sink seems to work, while the external log file isn't generated or written to.

Right now I'm using NSSM to log the console window output, but it comes with a lot of extra characters interjected making it tedious to read:

    [32m08:43:28[0m [1mINFO[0m [1mLogix write [36mInput_From_Word1[0m[1m      [32m0[0m[1m & [36mErsa_Input_From_Word2[0m[1m [32m8[0m[1m successful[0m
    [32m08:43:28[0m [1mINFO[0m [1mLogix tags [36mOutput_To_Word1[0m[1m and     [36mRecipe_STRING[0m[1m unchanged
    [32m08:43:28[0m[1m  INFO Skipping Click set([36mDS101[0m[1m,[36mDS300[0m[1m) to [32m0[0m[1m and [32m0844520[0m[1m[0m
    [32m08:43:39[0m [1mINFO[0m [1mRead Logix tags [36mOutput_To_Word1[0m[1m[0m

The loguru sinks I set up in the Python program look like this:


    logger.remove()
    # Setup the debug log file
    logger.add(sink="file_{time}.log", 
        format="{time:HH:mm:ss} {level} {message}",  
        level="TRACE", rotation="25 MB", backtrace=True, diagnose=True, retention="7 days")
    # Setup the debug to console
    logger.add(sink=sys.stdout, colorize=True, 
        format="<green>{time:HH:mm:ss}</green> <level>{level}</level> <level>{message}</level>", 
        level="INFO")
    # So we can have more colors in console window
    logger = logger.opt(colors=True)

I don't think the colors from loguru are what's causing the extra characters in NSSM (I have another program running that doesn't colorize console output). As far as NSSM settings, log rotation and capturing stdout work just fine, it's just the loguru sink that sets up its own log file with trace level logging that I'd really like to have.

I've tried changing the loguru sink for the trace log file from:

    
    logger.add(sink="file_{time}.log", 
        format="{time:HH:mm:ss} {level} {message}",  
        level="TRACE", rotation="25 MB", backtrace=True, diagnose=True,     retention="7 days")

to:

    
    logger.add(sink=open("file_{time}.log", 'rw'), 
        format="{time:HH:mm:ss} {level} {message}",  
        level="TRACE", rotation="25 MB", backtrace=True, diagnose=True,    retention="7 days")

I've also tried a couple different NSSM configurations: the HKLM ObjectName was set to "LocalSystem" and I tried changing this to a specific account with known privledges and this didn't seem to let the service to start at all (when setting up the NSSM registration for the Python program I used an elevated command prompt to enter the commands using the same account to which I tried changing the 'LocalSystem' value to). I also tried a couple different directories with different levels of access control; the "Public" directories I tried didn't allow the service to be started, so I kept it at a directory under the account which I used to register the NSSM service.

Again - if I run my Python program from a console window, Loguru logger logging works as expected. When running the same program as a registered NSSM service, only the stdout logging works when captured by NSSM's AppStdout logging.

How can I get loguru file logging to work while running my Python program as a Windows service, or how can I get NSSM to stop logging all the extra stdout characters that I normally don't see when I run the program from a console window?

blownupp
  • 21
  • 2

0 Answers0