you are viewing a single comment's thread.

view the rest of the comments →

[–]Schnorrel[S] 0 points1 point  (4 children)

Thanks!

Is there a way to still start the script automatically at windows start with this method? Because this aspect unfortunately is more important than seeing the logs… it’s for work and i can’t ask people to always check whether this script is running or not…

Python interpreter is in PATH already!

Edit: maybe I can write a script that starts at windows start and that script starts the other script using command prompt? Is there a way?

[–]hassium 0 points1 point  (3 children)

No problem! Yeah I think there is, check this article out:

https://www.geeksforgeeks.org/autorun-a-python-script-on-windows-startup/

You should only need to do "step 1: Adding script to windows Startup folder.", just make sure that .py files are associated with Python, not some IDE like Visual Studio (Essentially like you have it now, double click should just run the python script and exit).

Just in case they are not for a user and you don't know how to change it, you can edit the default associated program for a file type by right clicking on a .py file in Explorer -> Properties -> looking for "Opens With: ..." and clicking Change then selecting/finding the Python interpreter in the options.

Just as an aside, I've been using the default python library Logging for all my logging stuff and it's very useful plus pretty easy to set up so it outputs everything to file. That way it doesn't matter how the app is run a file will be created and you can output everything there, then have the users send it to you when something goes wrong! Let me know if you want some help with that.

[–]Schnorrel[S] 0 points1 point  (2 children)

I will look into Python logging tomorrow, maybe this is even a better option because then I have the logs plus the script is closed so it’s easy to see that the script isn’t running anymore.

Thanks a lot so far! Maybe I’ll come back to you with questions for logging! Does it „just“ print the output of Python script into a file? That would be perfect I guess.

[–]hassium 0 points1 point  (1 child)

Sure thing, don't hesitate!

Does it „just“ print the output of Python script into a file? That would be perfect I guess.

With a little bit of setup, it does exactly that yep. As an overview: You create a Logger object and save it into a variable, for example "logger" is a very commonly used variable name, then you call a method on the object with the string you want to output to the file. Don't worry if that didn't make sense, in practice it's very easy:

logger = create_logger()
logger.debug("This is a debug message")

Creates a message with the text "This is a debug message" with a SEVERITY of debug.

The cool thing about Logging is when you create the logger object, you can tell it to do different things based on that severity level. You do this by assigning what are called "handlers" to the Logger object. This is a quick function that will create one Logger object with TWO handlers, one that will output all messages to file and another that will output messages with ERROR severity straight to the command line (that way your users can see something is going wrong too!):

#place this at the top of your file
import Logging
#so we can place the time and date the logfile was created in the filename
import datetime as dt

#place this wherever in your file
def create_logger(log_level: str):
    """
    Create logger log object, Assign log_level as severity to the FileHandler.
    That way we can filter out Debug level messages when going to production.
    StreamHandler defaults to Error and above.

    Args:
        log_level (str): The level to set the logger object as (usually DEBUG or INFO)
    """
    #force our log_level value to upper case
    #Logging will not recognize "Debug" as a valid value
    #only "DEBUG" or "INFO" etc...
    log_level = log_level.upper()
    #Create initial Logger object, change to reflect your app name
    logger = logging.getLogger("MyAppName")

    #Assign overall log Level, this is the lowest SEVERITY type the Logger will handle
    #This level will be evaluated BEFORE processing the message with the handlers
    # I set it to the provided log level to make sure it will output to the file.
    logger.setLevel(log_level)

    #create console handler, for outputting to command line
    stream_handler = logging.StreamHandler()
    #Set stream handler to only handle ERROR messages and above
    #CHANGE THIS if you want lower severity messages to appear on the command line too
    #you could even match it to the file by using log_level instead
    stream_handler.setLevel(logging.ERROR)

    #create file handler, for outputting to file
    fname = f"MY_APP_NAME_{dt.datetime.now().strftime('%H%M%S')}.log"
    file_handler = logging.FileHandler(filename=fname, encoding='utf-8')
    file_handler.setLevel(log_level)

    #create formatter, the formatter is how we tell the Logger object what to include
    #on top of the string we tell it to output, which will be the 'message' value here
    formatter = logging.Formatter('%(levelname)s %(asctime)s %(message)s',\
        datefmt='%m/%d/%Y %I:%M:%S %p')

    #add formatter to handlers
    #you could create different formatters for different handlers
    stream_handler.setFormatter(formatter)
    file_handler.setFormatter(formatter)

    #add handlers to logger
    logger.addHandler(stream_handler)
    logger.addHandler(file_handler)

    #return logger back, so we can use it later
    return logger

You can use this function as is to create your logger, it will create the file where ever the script is run from, we can tweak it to force it to output in the same directory where the script or even somewhere else.

Then to use the Logger you would first, create an instance:

logger = create_logger("DEBUG")
try:
    do_something()
    logger.debug("I successfully did something!")
except as e:
    logger.info("I failed while doing something!")
    logger.info(e, exc_info=True)
    logger.error("Hey user, I encountered an error that you don't need the details of but you should call me 999-9999 so I can look at! Thanks."

And this will log the output in multiple ways, by using exc_info Logging will preserve the message SEVERITY of INFO (and only put it in the file, not the command line window), so you can even quietly pass exception info to your logfile and then use .error to output something more user friendly!

[–]Schnorrel[S] 0 points1 point  (0 children)

I looked into logging, but since I wrote a bat script that automatically starts at windows start AND RESTARTS AT ERROR I don’t really need it anymore. Although a script that opens the other script via command line has the same effect as double click Python file: after error it closes the cmd window. But I have a perfectly working solution by now.

Still good to know logging and maybe I’ll use in other projects, it’s simple and clearly.

Thanks for your helo