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