Skip to content

Instantly share code, notes, and snippets.

@DavidMetcalfe
Created February 21, 2021 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidMetcalfe/c176a7245ce3dd7e64abd2868bb0b4e6 to your computer and use it in GitHub Desktop.
Save DavidMetcalfe/c176a7245ce3dd7e64abd2868bb0b4e6 to your computer and use it in GitHub Desktop.
AutoHotkey Logging function for debug and other uses since there's nothing native that seems to work well for this.
DebugLogger(logText, debugLevel:="DEBUG") {
; Debug logging functionality for AutoHotkey (AHK).
; Creates one log file per day, and appends date/time to each log line.
; Uses debug levels: DEBUG, INFO, WARNING, ERROR, CRITICAL.
FormatTime, CurrentDate,, yyyy-MM-dd
FormatTime, CurrentDateTime,, yyyy-MM-dd hh:mm:ss
IfNotExist, .\log\
FileCreateDir, .\log\
stdout := FileOpen(".\log\" CurrentDate ".log", "a")
if (logText == "") {
stdout.WriteLine(CurrentDateTime " - No log text provided - WARNING")
} else if (InStr("DEBUG,INFO,WARNING,ERROR,CRITICAL", debugLevel) == 0) {
stdout.WriteLine(CurrentDateTime " - Invalid debug level provided - CRITICAL")
} else {
stdout.WriteLine(CurrentDateTime " - " logText " - " debugLevel)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment