Log Entry Generator (seeking suggestions)

This is a log entry generator I’ve been using for a while. Well, more truthfully, the latest in a long evolution of this script.

The premise is to allow logging of key events to a special file I use for tracking my automated scripts. Also needs to be human-readable so I like having indent levels. I also use a variant of this script to simply to debugging logs (by changing the log file name, for example). It’s just a kind of “generic file write with tab idents.”

I’m looking to clean-up some of my automation scripts and use loadable libraries to standardize them, so figured I’d consult the wizards here for tips.

Ideally it should check for the existance of a pre-existing log “g_log_file_path” and use it, else create a new one. It should also cleanly open and close the log file so ScriptDebugger doesn’t have a hissyfit every time I run a script. writeEntry has been done separately for flexibility. Not necessary, but I’ve found it handy now and again.

My only request is I find too much “one lining” (Nigel…heheh) of scripts to make them tough to change or debug in a hurry, since AppleScripting isn’t my only hat here at work. So unless one-lining really has some “worth it” gains, I prefer “the long way” under most circumstances.

So, streamline away!

For reference, “g_debug” is used to allow me to leave in key debugging lines in some scripts but turn off the actually logging of those lines in the finished products.


on logMe(log_string, indent_level)
	if g_debug is "true" then --allows turning the debugger on and off so logMe's can be left in final version
		
		try
			writeEntry(log_string, indent_level, g_log_file_path)
		on error
			tell application "Finder"
				make new file at g_desktop_folder_path with properties {name:g_log_file_name & ".txt"}
			end tell
			writeEntry(log_string, indent_level, g_log_file_path)
		end try
	end if
end logMe

on writeEntry(log_string, indent_level, log_file_path)
	repeat indent_level times
		write tab to (g_log_file_path as alias) starting at eof
	end repeat
	write (log_string & return) to (g_log_file_path as alias) starting at eof
end writeEntry

Hi Kevin,

this is a similar routine using the read/write file capabilities of the standard Scripting Addition.
One big advantage is: if no log file exists at the location, a new one will be created automatically

property g_desktop_folder_path : ((path to desktop) as Unicode text)
property g_log_file_name : "logFile"
property g_debug : true
on logMe(log_string, indent_level)
	if g_debug is true then --allows turning the debugger on and off so logMe's can be left in final version
		set target to (g_desktop_folder_path & g_log_file_name & ".txt")
		try
			set ff to open for access file target with write permission
			repeat indent_level times
				write tab to ff starting at eof
			end repeat
			write (log_string & return) to ff starting at eof
			close access ff
			return true
		on error
			try
				close access file target
			end try
			return false
		end try
	end if
end logMe

logMe("Hello", 1)

Once again, worked like a charm. Nicely done.

I also like the extra “try” on error to close the target file, hadn’t considered that state. I also realized I pulled an older version of the script that had the old-style tab and return character references. This script has been kicking around a while, and this is why I need to make a library of these rather that cut-n-paste into every script that needs them. :wink:

One question: why the “return true” and “return false”?

This is just indicating whether writing to the log file was successful.
You can omit it, if you like

I had suspected in may be a way to check what the handler did. Could you give me an example of how you would use the script as well as check which way it went? Don’t need it for this log writer, but the technique might be handy elsewhere, fi you don’t mind. :slight_smile:

it’s very simple like this:

...
logMe("Hello", 1)
if the result is false then display dialog "Couldn't write log file"
...

Thanks StefanK, very helpful. :slight_smile: