Jump to content

Write Output to a Text File for Logging


Recommended Posts

Hi

I have created a test autoit Script and am running it through SCiTe. THe script executes fine but I want to capture logs so that I can track it incase the automation fails or has any errors. I can write custom messages or text to a file but I want to  capture output that appearrs in the console. I have attached a screenshot of the logs I intend to capture.

I want to log messages that appear in the output window over here to a text file. 

 

test.png

Link to comment
Share on other sites

  • Moderators

@MathewThomas our forum has a pretty good search feature, something worth trying before you post. You can also look through the help file for suggestions. _FileWriteLog, for example, can write out to a log file for you. You could also use the _EventLog__* functions to use the Windows Event logs for your application.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

So rather than a picture (pictures are for Facebook) how about posting some code that shows what you're trying to do. Do you mean you're trying to automatically capture STDOut, or you want to capture everything in the console, time of completion, run time, etc?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

I seem to recall LazyCat doing something like this years ago; you might search the forum for DConsole. The only other way I could think of is to try a ControlGetText on the Console pane, but still think it is not a great idea. You should code in such a way you're catching errors on purpose, not just grabbing everything.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I use:
 

#include <File.au3>
Func _WriteErrorLog($ErrorMessage)
    Local $Logfile = @WorkingDir & "\" & @ScriptName & ".log"
    Local $errorFile = $Logfile
    Local $LogTime = @HOUR & ":" & @MIN & ":" & @SEC
    Local $hFileOpen = FileOpen($errorFile, 9)
    FileWriteLine($hFileOpen, $LogTime & " " & $ErrorMessage & @CRLF)
    FileClose($hFileOpen)
EndFunc   ;==>WriteErrorLog

Cheers, Starf0x

Link to comment
Share on other sites

  • Moderators

@Starf0x, a couple of things:

  • Your script doesn't do what the OP is asking (capture the entirety of the SciTE output pane)
  • Why do you declare a variable for the log file ($Logfile), only to declare it all over again in the very next line ($errorfile)?
  • Your entire function could be boiled down to a single line (two if you want to declare the filename in a variable) and without the need for the include:
_WriteErrorLog("Boy this is less code")

Func _WriteErrorLog($ErrorMessage)
    FileWriteLine(@ScriptDir & "\" & @ScriptName & ".log", @HOUR & ":" & @MIN & ":" & @SEC & ": " & $ErrorMessage)
EndFunc   ;==>WriteErrorLog

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...