log4ahk

A class that provides simple logging facilities for AutoHotkey

This log-Class supports

Loglevels

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.

Layout

Layouts allow to determine the format of the messages to be logged (see layout)

Appenders

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

Internals

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.

Example
#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
Public Methods
trace
trace(
str
)

Logs the given string at TRACE level

Parameters
str

String to be logged

debug
debug(
str
)

Logs the given string at DEBUG level

Parameters
str

String to be logged

info
info(
str
)

Logs the given string at INFO level

Parameters
str

String to be logged

warn
warn(
str
)

Logs the given string at WARN level

Parameters
str

String to be logged

error
error(
str
)

Logs the given string at ERROR level

Parameters
str

String to be logged

fatal
fatal(
str
)

Logs the given string at TRACE level

Parameters
str

String to be logged

Private Methods
_log
_log(
str,
loglvl := 2
)

Logs the given string at the given level

Parameters
str

String to be logged

loglvl

level on which the given message is to be logged

Internals

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
_fillLayoutPlaceholders(
str := ""
)

Fills some variables needed by layout with the currently valid values.

Parameters
str

String to be logged