Jump to content
Sign in to follow this  


Recommended Posts


I was looking for a way to log events in a several AutoIt scripts. In addition I found out that while debugging with the tray icon and the msgbox is perfectly doable, sometimes you need more information than what they can display, and there are times where you want to copy that text, which is hard from a msgbox or a tray icon.

I created this very basic logfile system consisting of two functions, a _openLogFile() that must be called in the beginning of your script, and a _addToLog() that can be called at any time during your script. The system is extremely simple, but does the job. A new logfile is created if the old one is getting too large. You can set your own filesize limits.





dim $logFileHandle  
_addToLog("info","Application started")

;This function open the latest logfile for use. If the file is too large, a new logfile is created and opened.
func _openLogFile()
    ;Find the file with the latest date/time stamp, while matchine the filename
    $logFileHandle=FileFindFirstFile($logFileName & "*")
    $searchName=$logFileName & "-" & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR  & @MIN & @SEC & ".txt"
    while 1
        if @error then ExitLoop
        if $searchDate<$tmpfdate then 

    ;If the current file is too large (more than 200kb), then create a new file in stead of appending to the old file
    if FileGetSize($searchName)>200000 then $searchName =  $logFileName & "-" & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR &  @MIN &  @SEC & ".txt"
    if $logFileHandle=-1 then 
        MsgBox(0,"Error","Unable to open logfile: " & $logFilePath & $searchName)

;This function append one line to the logfile $logFileHandle that was previosuly opened/created with _openLogFile()
func _addToLog($logGroup, $logStr)
    ;First adjust the $logGroup parameter to a 8 digit string, fixed length, left aligned
    if StringLen($logGroup) > 8 then 
        $logGroup=$logGroup&StringRight("        ",8-StringLen($logGroup))
    FileWriteLine($logFileHandle,@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":"&  @MIN & ":"& @SEC &" |" & $logGroup & "|  " & $logStr )


Share this post

Link to post
Share on other sites

Welcome to the forum :mellow:

Nice example for a first post.

I guess it would be good for you to know that there is already something similar in AutoIt - you could have a look at various functions included in "EventLog.au3" and _FileWriteLog function included in "File.au3"

Good luck,

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post

Link to post
Share on other sites


that's a great tool!

Just a question: why do I see the new messages in the logfile AFTER I close the script? When the script is running, there are no messages from the current script session. How do I change this?

Thank you!


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this