From: Schollnick, B. <Ben...@xe...> - 2005-06-03 13:53:19
|
> For windows executables, py2exe assigns an object to=20 > sys.stderr that writes the output into a logfile named=20 > <appname>.exe.log, and registers an atexit handler that=20 > displays this messagebox when there has been any output. =20 > Another object is assigned to sys.stdout that simply ignores=20 > everything that is printed to it. The code doing this is in=20 > Lib\site-packages\py2exe\boot_common.py. >=20 > I've been thinking about this - maybe it would be better to=20 > use the logging module for that. The logging module is not bad.... It's not documented fantastically... Or at least it wasn't when I started the logging wrapper that I wrote... =20 > I haven't used the logging module myself before. Any ideas? The following routine will create a log module object... You can then use it as such.... log_file_interface.warning ( 'Directory [%s] does not Exist.' % server) def Return_Application_Name ( ): """Returns the name of the Executable. The code will process the sys.argv[0] string, and extract the last element, which should be the executable name. =20 Inputs - None Outputs - A single String. =20 Example: =20 """ Name =3D sys.argv[0].split(os.sep) Name =3D Name[len(Name)-1] Name =3D Name.split(".")[0] return Name def Log_to_File_Interface ( Application_Name =3D Return_Application_Name = ( ), logfile_path =3D "." + os.sep, logfile_name =3D Return_Application_Name = ( )+".log", warning_level =3D logging.INFO ): """ Setup a Logging Interface to a Log File. debug ( msg[, *args[, **kwargs]])=20 info ( msg[, *args[, **kwargs]])=20 warning ( msg[, *args[, **kwargs]])=20 error ( msg[, *args[, **kwargs]])=20 critical ( msg[, *args[, **kwargs]])=20 log ( lvl, msg[, *args[, **kwargs]])=20 exception ( msg[, *args]) Inputs - Application_Name - The name of the Application that we are logging. logfile_path - The pathname of where to store the log file, ensure to append a os.sep logfile_name - The name of the log file, does not automatically append .LOG warning_level - The default filter for the warning level. Defaults to Debugging. Outputs - Returns two interfaces. log_interface - Interface to the Log hdlr - The formating Interface =20 =20 Example: logger =3D Log_to_File_Interface () logger.error('We have a problem') logger.info('While this is just chatty') #logger.close() """ log_interface =3D logging.getLogger( Application_Name ) hdlr =3D logging.FileHandler( logfile_path + logfile_name ) formatter =3D logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) log_interface.addHandler(hdlr)=20 log_interface.setLevel( warning_level ) return log_interface, hdlr |