A class that provides simple logging facilities for AutoHotkey
This log-Class supports
Loglevel allows to define to a hierarchy of log messages and controls which messages are logged
Layout of the logged message
Appenders to the the channels to be logged to
Each message has to be logged on a certain loglevel. Consider the loglevel as the severity of the message you want to log: some logmessages are used for simple debug purposes, whereas other logmessages may indicate an Error. In some situations you want to see a very detailled logging - in other situations you just want to be notified about errors ... Both can be managed via loglevel.
Layouts allow to determine the format of the messages to be logged (see layout)
Appenders define the "channels" to be logged to. Currently following appenders can be used:
| - appenderstdout | log your messages via stdout. Using Scite4AutoHotkey or VSCode, this will be logging to console |
| - appenderoutputdebug | log your messages via outputDebug (you might need DbgView or a similar tool to view output) |
You might choose several appenders to be logged on simultaneously
log4ahk is implemented as singleton, so there is only one existing instance. Each change on loglevel, layout will be a global change and be valid from the time of change.
#include log4ahk.ahk
logger := new log4ahk()
; Enable logging to STDOUT
logger.appenders.push(new logger.appender.stdout())
; Set the loglevel to be filtered upon
logger.loglevel.required := logger.loglevel.TRACE
; Show loglevel, current function, computername and log message in log protocol
logger.layout.required := "[%-5.5V] {%-15.15M}{%H} %m"
logger.trace("TRACE - Test TRACE")
logger.debug("TRACE - Test DEBUG")
logger.info("TRACE - Test INFO")
f1()
return
;########################################################
f1() {
logger := new log4ahk()
;Change the loglevel to be filtered upon
logger.loglevel.required := logger.loglevel.INFO
logger.trace("INFO - Test TRACE") ; shouldn't be logged due to required loglevel
logger.debug("INFO - Test DEBUG") ; shouldn't be logged due to required loglevel
logger.info("INFO - Test INFO")
}
; Output:
;[TRACE] {[AUTO-EXECUTE] }{XYZ-COMP} TRACE - Test TRACE
;[DEBUG] {[AUTO-EXECUTE] }{XYZ-COMP} TRACE - Test DEBUG
;[INFO ] {[AUTO-EXECUTE] }{XYZ-COMP} TRACE - Test INFO
;[INFO ] {f1 }{XYZ-COMP} INFO - Test INFO
| trace( |
| ) |
Logs the given string at TRACE level
| str | String to be logged |
| debug( |
| ) |
Logs the given string at DEBUG level
| str | String to be logged |
| info( |
| ) |
Logs the given string at INFO level
| str | String to be logged |
| warn( |
| ) |
Logs the given string at WARN level
| str | String to be logged |
| error( |
| ) |
Logs the given string at ERROR level
| str | String to be logged |
| fatal( |
| ) |
Logs the given string at TRACE level
| str | String to be logged |
| _log( |
| ) |
Logs the given string at the given level
| str | String to be logged |
| loglvl | level on which the given message is to be logged |
The given loglevel is compared against the global required fixlevel (see <required>) Is the given loglevel equal or greater the required loglevel the logmessage is printed - otherwise the logmessage is suppressed.
| _fillLayoutPlaceholders( |
| ) |
Fills some variables needed by layout with the currently valid values.
| str | String to be logged |