here's my AHK V2 implementation for a logging class. I've implemented some basic concepts from log4j as there are:
- Simultaneously logging to several output channels (appenders). Using STDOUT as appender, it's possible to log to Scite4AutoHotkey-console.
- logging messages with different priority. The logging messages are only logged if they match the chosen priority (loglevels: TRACE->DEBUG->INFO->WARN->ERROR->FATAL)
- Format the prefix of a logging message via layout definition (for example: date/time, processid, functionname, elapsed time ... there are a few more)
It's implemented as a singleton - anywhere you need, just instanciate a new logger and the logger will be available with all its previously configured properties.
Here's an example usage:
Code: Select all
#include %A_ScriptDir%\log4ahk.ahk
logger := new log4ahk()
; Set the loglevel to be filtered upon
logger.loglevel.required := logger.loglevel.TRACE
; Set the appenders to be logged to
logger.appenders.push(new logger.appenderoutputdebug())
logger.appenders.push(new logger.appenderstdout())
; Show loglevel, current function, computername and log message in log protocol
logger.layout.required := "[%-5.5V] {%-15.15M}{%H} %m"
logger.info("Running log4ahk - Version " logger._version)
logger.trace("Test TRACE - Lvl TRACE")
logger.debug("Test DEBUG - Lvl TRACE")
logger.info("Test INFO - Lvl TRACE")
f1()
return
;########################################################
f1() {
logger := new log4ahk()
; Change the loglevel to be filtered upon
logger.loglevel.required := logger.loglevel.INFO
logger.debug("Test DEBUG - Lvl INFO") ; won't be logged as the current loglevel has lower priority than required loglevel
logger.info("Test INFO - Lvl INFO")
logger.layout.required := "%d - %r - %R [%P] [%-5.5V] {%s - %-15.15M}{%H} %m"
logger.info("INFO - Test INFO - Lvl INFO - after change of layout")
}
/*
; Output:
[INFO ] {[AUTO-EXECUTE] }{COMPI} Running log4ahk - Version 0.4.1
[TRACE] {[AUTO-EXECUTE] }{COMPI} Test TRACE - Lvl TRACE
[DEBUG] {[AUTO-EXECUTE] }{COMPI} Test DEBUG - Lvl TRACE
[INFO ] {[AUTO-EXECUTE] }{COMPI} Test INFO - Lvl TRACE
[INFO ] {f1 }{COMPI} Test INFO - Lvl INFO
2018/11/19 07:27:55 - 8.60363052823162633 - 1.63673621445787609 [4332] [INFO ] {log4ahk_demo01.ahk - f1 }{COMPI} INFO - Test INFO - Lvl INFO - after change of layout
*/
And here is the online documentation: https://autohotkey-v2.github.io/log4ahk/
It's a work in progress ... more features are likely to be implemented - also there might be interface changes.
Tested with AutoHotkey_2.0-a100-52515e2 on Win10 Enterprise