|
1 |
| -# VBA-UTC |
| 1 | +# VBA-Log |
2 | 2 |
|
3 |
| -UTC and ISO 8601 date conversion and parsing for VBA (Windows and Mac Excel, Access, and other Office applications). |
| 3 | +Logging helpers for VBA. Logs to Immediate Window by default (`ctrl + g`), but can attach multiple loggers with callbacks. |
4 | 4 |
|
5 | 5 | Tested in Windows Excel 2013 32-bit and 64-bit and Excel for Mac 2011, but should apply to Windows Excel 2007+.
|
6 | 6 |
|
7 | 7 | # Example
|
8 | 8 |
|
9 | 9 | ```VB.net
|
10 |
| -' Timezone: EST (UTC-5:00), DST: True (+1:00) -> UTC-4:00 |
11 |
| -Dim LocalDate As Date |
12 |
| -Dim UtcDate As Date |
13 |
| -Dim Iso As String |
| 10 | +Logger.LogDebug "Howdy!" |
| 11 | +' -> does nothing because logging is disabled by default |
14 | 12 |
|
15 |
| -LocalDate = DateValue("Jan. 2, 2003") + TimeValue("4:05:06 PM") |
16 |
| -UtcDate = UtcConverter.ConvertToUtc(LocalDate) |
| 13 | +Logger.LogEnabled = True |
| 14 | +' -> Log all levels (Trace, Debug, Info, Warn, Error) |
17 | 15 |
|
18 |
| -Debug.Print VBA.Format$(UtcDate, "yyyy-mm-ddTHH:mm:ss.000Z") |
19 |
| -' -> "2003-01-02T20:05:06.000Z" |
| 16 | +Logger.LogThreshold = 3 |
| 17 | +' -> Log levels >= 3 (Info, Warn, and Error) |
20 | 18 |
|
21 |
| -Iso = UtcConverter.ConvertToIso(LocalDate) |
22 |
| -Debug.Print Iso |
23 |
| -' -> "2003-01-02T20:05:06.000Z" |
| 19 | +Logger.LogTrace "Start of logging" |
| 20 | +Logger.LogDebug "Logging has started" |
| 21 | +Logger.LogInfo "Logged with VBA-Log" |
| 22 | +Logger.LogWarn "Watch out!", "ModuleName.SubName" |
| 23 | +Logger.LogError "Something went wrong", "ClassName.FunctionName", Err.Number |
24 | 24 |
|
25 |
| -LocalDate = UtcConverter.ParseUtc(UtcDate) |
26 |
| -LocalDate = UtcConverter.ParseIso(Iso) |
| 25 | +' Attach alternative logging function(s) |
| 26 | +Public Sub LogFile(Level As Long, Message As String, From As String) |
| 27 | + ' ... |
| 28 | +End Sub |
27 | 29 |
|
28 |
| -Debug.Print VBA.Format$(LocalDate, "m/d/yyyy h:mm:ss AM/PM") |
29 |
| -' -> "1/2/2003 4:05:06 PM" |
| 30 | +' Log to single function |
| 31 | +Logger.LogCallback = "LogFile" |
| 32 | + |
| 33 | +Public Sub LogWorkbook(Level As Long, Message As String, From As String) |
| 34 | + ' ... |
| 35 | +End Sub |
| 36 | + |
| 37 | +' Log to multiple functions |
| 38 | +Logger.LogCallback = Array("LogFile", "LogWorkbook") |
30 | 39 | ```
|
| 40 | + |
| 41 | +For applications that don't support `Application.Run` (e.g. Access), there is a note in `Logger.Log` for where to put your custom logging function (if desired). |
0 commit comments