Trying to log...

What am I doing wrong?

log_("Is this in a file? NO!")

on log_(info)
	set thename to my name
	set docs to path to documents folder
	set thefile to ((docs as string) & thename & ":log.log")
	tell application "Finder"
		if not (exists ((docs as string) & thename & ":")) then make new folder at folder (docs as string) with properties {name:thename}
		set find to exists thefile
		if find then
			if read thefile is not "" then
				try
					open for access thefile with write permission
					write (((current date) as string) & "; " & thename & ": " & info) to thefile as «class utf8»
					close access thefile
				on error e number n
					error e number n
					try
						close access thefile
					end try
				end try
			end if
		end if
	end tell
end log_

I’ve been using this for a long time in most of my scripts, courtesy of our own StefanK. It’s a generic log generator. Adds the ability to indent, making complex logs more human-readable (one my requirements).

Simply define the value of “g_debug” (true or false) and “g_log_file_name” (the name of the log file) at the top of the script and use my logMe(“sometext”,1) where 1 is some number of indents.

--Log Entry Generation
-- With help from StephanK of MacScripter
-- http://bbs.applescript.net/viewtopic.php?pid=76607#p76607
--
on logMe(log_string, indent_level)
	if g_debug is true then --allows turning the debugger on and off so my logMe's can be left in final version
		set log_target to (g_home_folder_path & "Library:Logs:" & g_log_file_name) as text
		try
			set log_file_ref to open for access file log_target with write permission
			repeat indent_level times
				write tab to log_file_ref starting at eof
			end repeat
			write ((log_string as text) & return) to log_file_ref starting at eof
			close access log_file_ref
			return true
		on error
			try
				close access file log_target
			end try
			return false
		end try
	end if
end logMe

a few things


log_("Is this in a file? NO!")

on log_(info)
	set thename to my name
	set docs to path to documents folder as text
	set thefile to (docs & thename & ":log.log")
	tell application "Finder"
		if not (exists folder (docs & thename & ":")) then make new folder at folder docs with properties {name:thename}
		set find to exists file thefile
	end tell
	if find then
		if read file thefile is not "" then
			try
				set dataStream to open for access file thefile with write permission
				write (((current date) as string) & "; " & thename & ": " & info) to dataStream as «class utf8»
				close access dataStream
			on error
				try
					close access file thefile
				end try
			end try
		end if
	end if
end log_

Did you try to fix it, because it isn’t working for me.

your code doesn’t write anything into the logfile if the file is empty.
This appends each message to the end of the file


log_("Is this in a file? NO!")

on log_(info)
	set thename to my name
	set logFolder to ((path to documents folder as text) & thename & ":")
	set logFile to logFolder & "log.log"
	do shell script "/bin/mkdir -p " & quoted form of POSIX path of logFolder
	try
		set dataStream to open for access file logFile with write permission
		write (((current date) as string) & "; " & thename & ": " & info & return) to dataStream as «class utf8» starting at eof
		close access dataStream
	on error
		try
			close access file logFile
		end try
	end try
end log_

Hi,

Just an addition to this discussion:

I made the experience that if you don’t take care of the log files, they can grow very large and this can lead to problems as far as script performance is concerned. I remember a PDF creation tool with a 21 MB log file…

Therefor I am using the following handler, which archives log files when they reach a file size of 1 MB. You can easily add additional features like zipping the archives (I am not doing this, because I hate to unzip them and we have enough space on the Xserves :smiley: ).


property mytitle : "Sample Script"
-- can be set during an initialize-handler
property logsfolderpath : missing value

on run
	set msg to "Test message"
	my logmsg(msg, "--", "info")
end

-- I am writing messages to a log file
on logmsg(msg, num, type)
	set curdatestr to ((current date) as string)
	set logentry to "[" & curdatestr & "]" & tab & msg & " (" & num & ")" & return
	if not my itempathexists(logsfolderpath) then
		set logsfolderpath to (((path to desktop) as text) & "Logs:")
		set qtdlogsfolderpath to quoted form of POSIX path of logsfolderpath
		do shell script "mkdir -p " & qtdlogsfolderpath
	end if
	set logfilepath to logsfolderpath & mytitle & "_" & type & ".log"
	if my itempathexists(logfilepath) then
		set logfileinfo to info for (logfilepath as alias)
		set logfilesize to size of logfileinfo
		-- verschieben von zu grossen Logdateien ins Archivverzeichnis
		if logfilesize is greater than 1048576 then
			try
				set timestamp to do shell script "date \"+%Y%m%d_%H%M%S\""
				do shell script "mkdir -p " & quoted form of POSIX path of (logsfolderpath & "old:")
				set oldlogfilepath to logsfolderpath & "old:" & timestamp & "_" & mytitle & "_" & type & ".log"
				set command to "mv " & quoted form of POSIX path of logfilepath & " " & quoted form of POSIX path of oldlogfilepath
				do shell script command
			end try
		end if
	end if
	try
		set openlogfile to open for access logfilepath with write permission
		write logentry to openlogfile starting at eof
		close access openlogfile
	on error
		try
			close access openlogfile
		end try
		return false
	end try
	return true
end logmsg

on itempathexists(itempath)
	try
		set itemalias to itempath as alias
		return true
	on error
		return false
	end try
end itempathexists

Agreed, log bloat would be a problem. The handler I use is only for my own troubleshooting and gets turned-off when I’m finished, and I flush it every time I check it (since I only care about the last run-through). Yours is neat for a persistantly-running log. :smiley:

I would like to have it as a resource to find whats going wrong.

The log command puts it in a bad place. It’s under HUGE strings, hidden in grey. I would only have it for a debug purpose.