My log script ... how horrible is it? - Logger Question

So … yet again … reviewing my code, thinking about the ‘needs’ and ‘stupid stuff’ / ‘is it necessary’.

I make lots of scripts for our company and while I use them often, I’m also curious why else uses them. If not used, I can remove the scripts from our toolbar, or notify people of the sweetness of the script.

Since like forever I’m using an open-txt-file-add-stuff-close-file method. This always felt horrible to me, but it worked. Now I’m at the point of 1. remove the logging at all or 2. setup a better approach (lite weight database?)

Share your thoughts / suggestions. I’m open to them all :slight_smile:

def log():   
    if not revit.doc.IsFamilyDocument:
        import __main__ 
        project_nummer = projectnummer()
        date = datetime.now().strftime("%d %B %Y")
        script_naam = os.path.basename(__main__.__file__)[:-10]
        log_path = "T:\\Revit addons\\Our Tools\\our.log\\{}\\".format(str(datetime.now().year))
        bestand = log_path + "our_tools_log.txt"
        
        val = "{}\t{}\t{}".format(script_naam, project_nummer, date)
        exists = os.path.isfile(bestand)
        
        if exists:
            with open(bestand, "a+") as f:	
                f.writelines(val)	
            return "Log: {} {}".format(log_path, val)
        print "Log ({})bestand bestaat niet.".format(bestand)

# output 
# pdf	1234	02 January 2024

There is a logger module in pyrevit
https://docs.pyrevitlabs.io/reference/pyrevit/coreutils/logger/#pyrevit.coreutils.logger.loggers_have_errors

and plenty of tools that use the logger module in the repo

logger usage in pyRevit tools

And
Logger utility usage description

</start ashamed>

Oefff. no way I missed that one!!!

ughhh. 

</end ashamed>

Thanks for pointing me in the right direction :heart:

1 Like

ok,

I did use the logging, but only for outputting my ‘successes’ ‘errors’ etc, now I’m learning the other possiblities.

Can’t grasp the full extend of it, and I’m having doubts I use the correct approach.

Goal:
logging script usage on a server (name, projectnumber, date).
4 things I’ll need to get a proper logging: location, scriptname, projectnumber, date

#Create and return logger named for current script.
#this handles the 'name' part as it creates an instance of 
# logger.get_logger(EXEC_PARAMS.command_name)
mylog = script.get_logger()

Next stop would be altering the save location of the log file.
looking at the logger file these are the defaults.

# Creating default file log name and status
FILE_LOG_FILENAME = '{}runtime.log'.format(PYREVIT_FILE_PREFIX_STAMPED)
FILE_LOG_FILEPATH = op.join(PYREVIT_VERSION_APP_DIR, FILE_LOG_FILENAME)
FILE_LOGGING_DEFAULT_STATE = False

And a bit down it is used here. So FileHandler is my go to guy I think.
file_hndlr = logging.FileHandler(FILE_LOG_FILEPATH, mode='a', delay=True)

So with that I can alter this puppy.

mylog.FileHandler = “filepath using os.path”
#for example mylog.FileHandler = "X:\Revit " + revitversion() + “\99 logs\”

Putting this together … things are falling apart :slight_smile:

if __name__ == "__main__":	
	USER_DESKTOP = op.expandvars('%userprofile%\\desktop')
	MY_LOGS = op.join(USER_DESKTOP, 'hooks.log')

	#Create and return logger named for current script.
	mylog = script.get_logger()
	mylog.FileHandler = MY_LOGS
	print mylog.FileHandler
	mylog.error("asdfsadf")

	select_koz_groups()

What goes wrong:

  1. No log is made.
  2. output throws: " ERROR [select_koz] asdfsadf"
    (print mylog.FileHandler shows correct location)

So where am I messing things up?

Not able to help woth the code right now, but there is also a system in pyRevit close to what you want. :wink: