Cleaner timestamp for logging

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
julesverne
Posts: 42
Joined: 18 Apr 2017, 14:39
Contact:

Cleaner timestamp for logging

Post by julesverne » 25 Jan 2023, 22:04

Hi all,
Trying to make a cleaner logging system for a script. I need milliseconds and that's proving to be annoying.

Code: Select all

; this works but requires two lines of code EVERY time before it writes to the log file
;which is technically not the exact time of the log, since the time is taken down on the first line, and written to the file on the second line.
FormatTime, TimeString,, MM`/dd`/yyyy hh:mm:ss:%A_MSec%
FileAppend, %TimeString% - logging stuff here that's happening at this time`r`n, C:\scripts\test\log.txt

;this works but requires two lines of code EVERY time before it writes to the log file
;which is technically not the exact time of the log, since the time is taken down on the first line, and written to the file on the second line.
timestring := A_MM . "/" . A_DD . "/" . A_YYYY . " " . A_Hour . ":" . A_Min . ":" . A_Sec . ":" . A_Msec
FileAppend, %TimeString% - logging stuff here that's happening at this time`r`n, C:\scripts\test\log.txt

; this works but looks really ugly and long just to log.
FileAppend, % A_MM . "/" . A_DD . "/" . A_YYYY . " " . A_Hour . ":" . A_Min . ":" . A_Sec . ":" . A_Msec . " - logging stuff here that's happening at this time`r`n", C:\scripts\test\log.txt

; how can I get one line of code that looks good?

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Cleaner timestamp for logging

Post by mikeyww » 25 Jan 2023, 22:08

Hi,

Although "looks good" does not seem especially critical for this script, you can do it if you use the current version of AutoHotkey.

Code: Select all

#Requires AutoHotkey v2.0
logFile := A_ScriptDir "\beautiful.log"
FileAppend FormatTime(, "HH:mm") '`n', logFile
Run logFile

julesverne
Posts: 42
Joined: 18 Apr 2017, 14:39
Contact:

Re: Cleaner timestamp for logging

Post by julesverne » 25 Jan 2023, 22:32

I wasn't even aware Version 2 was finally official. Exciting news!

NewberEnerNeeder1
Posts: 47
Joined: 26 Mar 2019, 06:06

Re: Cleaner timestamp for logging

Post by NewberEnerNeeder1 » 27 Jan 2023, 14:47

You can use the FormatTime function with the A_Now variable to get the current time in the desired format and store it in a variable. Then use that variable in the FileAppend function to write to the log file, like this:

FormatTime, TimeString, A_Now, MM/dd/yyyy hh:mm:ss:%A_MSec%
FileAppend, %TimeString% - logging stuff here that's happening at this timern, C:\scripts\test\log.txt

does this help?

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Cleaner timestamp for logging

Post by mikeyww » 27 Jan 2023, 15:56

That's what the original script does.

julesverne
Posts: 42
Joined: 18 Apr 2017, 14:39
Contact:

Re: Cleaner timestamp for logging

Post by julesverne » 27 Jan 2023, 16:03

For anyone interested, my version 1 solution was to just make it a function, which I actually liked better. caveat, the folder path for log contained variables so I chose to just pass the filename as a second parameter. If you want to use this, and your log file path contains no variables, no need to.

Logging with a variable in the log file path

Code: Select all

LogFile := "C:\scripts\test\" . ExampleVariable  . "\AHKScript.Log"
Log("log message to send", LogFile)
Return

;Logging Function with dynamic log file
Log(logmessage, logfile)
{
	FormatTime, TimeString,, MM`/dd`/yyyy hh:mm:ss:%A_MSec%
	FileAppend, %TimeString% - %logmessage%`r`n, %logfile%
}
Logging with a static log file and path

Code: Select all

Log("log message to send")
Return

;Logging Function with static log file path
Log(logmessage)
{
	FormatTime, TimeString,, MM`/dd`/yyyy hh:mm:ss:%A_MSec%
	FileAppend, %TimeString% - %logmessage%`r`n, C:\scripts\test\AHKScript.Log
}
example output in the log file looks like

Code: Select all

01/26/2023 03:35:25:627 - log message to send
:offtopic: I didn't want to rewrite in ver2 after reviewing it. Vers 2 kinda seems like maybe I should just learn c# dotnet instead. Even after watching the videos trying to convince me it's still an easy language. I'm not saying I won't ever, but definitely questioning a path forward.

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Cleaner timestamp for logging

Post by mikeyww » 27 Jan 2023, 17:29

I'm not following the overwhelming challenge that you are describing. Most or all of these lines are the same or simpler.

Code: Select all

#Requires AutoHotkey v2.0
LogFile := "C:\scripts\test\" . ExampleVariable  . "\AHKScript.Log"
Log("log message to send", LogFile)

Log(logmessage, logfile)
{
	TimeString := FormatTime(, 'MM/dd/yyyy hh:mm:ss:' A_MSec)
	FileAppend TimeString ' - ' logmessage '`r`n', logfile
}
Or:

Code: Select all

#Requires AutoHotkey v2.0
LogFile := "C:\scripts\test\" ExampleVariable "\AHKScript.Log"
Log("log message to send", LogFile)

Log(logmessage, logfile) {
 FileAppend FormatTime(, 'MM/dd/yyyy hh:mm:ss:' A_MSec) ' - ' logmessage '`r`n', logfile
}
Or:

Code: Select all

#Requires AutoHotkey v2.0
logMsg  := (msg, Lfile) => FileAppend(FormatTime(, 'MM/dd/yyyy hh:mm:ss:' A_MSec) ' - ' msg '`r`n', Lfile)
LogFile := "C:\scripts\test\" ExampleVariable "\AHKScript.Log"
logMsg("log message to send", LogFile)

julesverne
Posts: 42
Joined: 18 Apr 2017, 14:39
Contact:

Re: Cleaner timestamp for logging

Post by julesverne » 27 Jan 2023, 21:13

Forgive me @mikeyww, I misspoke, I wasn't referring to this particular issue. Was referring to some of the more complicated scripts that I've written.

I think in particular, some scripts where I've needed to use objects is where my intimidation is happening. When I see words like namespace, static, public, class, these have historically been things about c# that I have not completely grasped and was happy to avoid them with ahk. This page in particular, https://www.autohotkey.com/docs/v2/lib/Object.htm just seemed pretty daunting from line one. And made me think if I have to learn the stress inducing trigger words like "class", then maybe it's time to expand my horizons and learn c#. That's all I was saying. Like some ahk script writers, I learned how to do some seemingly complicated things, with ease, because of ahk. I didn't go to school for programming. I needed something better than CMD but not intimidating, it was vbscript at first, until I discovered ahk, pretty much everything since has been ahk.

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Cleaner timestamp for logging

Post by mikeyww » 27 Jan 2023, 22:02

OK. It must be easy for me because I don't use that page! 8-)

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Cleaner timestamp for logging

Post by boiler » 27 Jan 2023, 22:13

julesverne wrote: When I see words like namespace, static, public, class, these have historically been things about c# that I have not completely grasped and was happy to avoid them with ahk.
I guess you’re not aware that AHK v1 also has each and every one of those things and yet you were somehow able to avoid them. You are seeing monsters under the bed regarding v2 that just aren’t there.

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Cleaner timestamp for logging

Post by mikeyww » 27 Jan 2023, 22:17

Yeah, I was confused as well.
Was referring to some of the more complicated scripts that I've written.
So I was imagining that you used objects in AHK v1, but now you couldn't figure out how they work in v2?

Post Reply

Return to “Ask for Help (v1)”