Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log to file as well as immediate window #215

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 113 additions & 17 deletions src/WebHelpers.bas
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,24 @@ End Enum
''
Public EnableLogging As Boolean

' @property EnableFileLogging
' @type Boolean
' @default False
''
Public EnableFileLogging As Boolean

' @property LogFile
' @type String
' @default ""
''
Public LogFile As String

' @property LogFileNumber
' @type Integer
' @default 0
''
Private LogFileNumber As Integer

''
' Store currently running async requests
'
Expand Down Expand Up @@ -409,11 +427,15 @@ Public AsyncRequests As Dictionary
' @method LogDebug
' @param {String} Message
' @param {String} [From="VBA-Web"]
' @param {Boolean} ForcePrint
''
Public Sub LogDebug(Message As String, Optional From As String = "VBA-Web")
If EnableLogging Then
Public Sub LogDebug(Message As String, Optional From As String = "VBA-Web", Optional ForcePrint as Boolean)
If EnableLogging Or ForcePrint Then
Debug.Print From & ": " & Message
End If
If EnableLogging Or EnableFileLogging Then
LogWrite "D", Message, From
End If
End Sub

''
Expand All @@ -436,6 +458,7 @@ End Sub
''
Public Sub LogWarning(Message As String, Optional From As String = "VBA-Web")
Debug.Print "WARNING - " & From & ": " & Message
LogWrite "W", Message, From
End Sub

''
Expand Down Expand Up @@ -473,6 +496,7 @@ Public Sub LogError(Message As String, Optional From As String = "VBA-Web", Opti
End If

Debug.Print "ERROR - " & From & ": " & web_ErrorValue & Message
LogWrite "E", web_ErrorValue & Message, From
End Sub

''
Expand All @@ -483,24 +507,31 @@ End Sub
' @param {WebRequest} Request
''
Public Sub LogRequest(Client As WebClient, Request As WebRequest)
If EnableLogging Then
Debug.Print "--> Request - " & Format(Now, "Long Time")
Debug.Print MethodToName(Request.Method) & " " & Client.GetFullUrl(Request)
If EnableLogging Or EnableFileLogging Then
Dim msg As String
msg = "--> Request - " & Format(Now, "Long Time") & vbNewLine
msg = msg & MethodToName(Request.Method) & " " & Client.GetFullUrl(Request) & vbNewLine

Dim web_KeyValue As Dictionary
For Each web_KeyValue In Request.Headers
Debug.Print web_KeyValue("Key") & ": " & web_KeyValue("Value")
msg = msg & web_KeyValue("Key") & ": " & web_KeyValue("Value") & vbNewLine
Next web_KeyValue

For Each web_KeyValue In Request.Cookies
Debug.Print "Cookie: " & web_KeyValue("Key") & "=" & web_KeyValue("Value")
msg = msg & "Cookie: " & web_KeyValue("Key") & "=" & web_KeyValue("Value") & vbNewLine
Next web_KeyValue

If Not IsEmpty(Request.Body) Then
Debug.Print vbNewLine & CStr(Request.Body)
msg = msg & vbNewLine & CStr(Request.Body) & vbNewLine
End If

Debug.Print
msg = msg & vbNewLine
End If
If EnableLogging Then
Debug.Print msg
End If
If EnableLogging Or EnableFileLogging Then
LogWrite "D", msg, "WebHelpers.LogRequest"
End If
End Sub

Expand All @@ -513,24 +544,89 @@ End Sub
' @param {WebResponse} Response
''
Public Sub LogResponse(Client As WebClient, Request As WebRequest, Response As WebResponse)
If EnableLogging Then
Dim web_KeyValue As Dictionary

Debug.Print "<-- Response - " & Format(Now, "Long Time")
Debug.Print Response.StatusCode & " " & Response.StatusDescription
If EnableLogging Or EnableFileLogging Then
Dim msg As String
msg = "<-- Response - " & Format(Now, "Long Time") & vbNewLine
msg = msg & Response.StatusCode & " " & Response.StatusDescription & vbNewLine

Dim web_KeyValue As Dictionary
For Each web_KeyValue In Response.Headers
Debug.Print web_KeyValue("Key") & ": " & web_KeyValue("Value")
msg = msg & web_KeyValue("Key") & ": " & web_KeyValue("Value") & vbNewLine
Next web_KeyValue

For Each web_KeyValue In Response.Cookies
Debug.Print "Cookie: " & web_KeyValue("Key") & "=" & web_KeyValue("Value")
msg = msg & "Cookie: " & web_KeyValue("Key") & "=" & web_KeyValue("Value") * vbNewLine
Next web_KeyValue

Debug.Print vbNewLine & Response.Content & vbNewLine
msg = msg & vbNewLine & Response.Content & vbNewLine
End If
If EnableLogging Then
Debug.Print msg
End If
If EnableLogging Or EnableFileLogging Then
LogWrite "D", msg, "WebHelpers.LogResponse"
End If
End Sub

''
' Close the log file if open.
'
''
Public Sub LogClose(Optional ByVal Dummy as Boolean)
If LogFile <> "" And LogFileNumber > 0 Then
Close #LogFileNumber
LogFileNumber = 0
End If
End Sub

''
' Log a message to a log file if specified.
'
' @param {String} LogType (E=Error, W=Warning, D=Debug)
' @param {String} Message
' @param {String} From
''
Private Sub LogWrite(ByVal LogType As String, ByVal Message As String, ByVal From As String)
If LogFile <> "" Then
If LogFileNumber < 0 Then
LogFileNumber = LogFileNumber + 1
Exit Sub
End If

If LogFileNumber = 0 Then
On Error GoTo FileOpenError
LogFileNumber = FreeFile
Open LogFile For Append Access Write Lock Write As #LogFileNumber
On Error GoTo 0
End If

Dim Lines() As String
Dim msg As Variant
Lines = split(Message, vbNewLine)
On Error GoTo FileWriteError
For Each msg In Lines
msg = Replace(msg, """", "'")
If Trim(msg) <> "" Then _
Write #LogFileNumber, LogType, Format(Now, "General Date"), From, Trim(msg)
Next msg
End If
Exit Sub

FileOpenError:
Debug.Print "ERROR: Unable to open logfile '" & LogFile & "' for Append Write: Error " & Err.Number & ": " & Err.Description
LogFileNumber = -100
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is not to issue excessive numbers of these errors to the immediate window - but upon reflection I am not sure this is such a good idea - or perhaps only if normal EnableLogging is true.

Err.Clear
Exit Sub

FileWriteError:
Debug.Print "ERROR: Unable to write to logfile '" & LogFile & "': Error " & Err.Number & ": " & Err.Description
Close #LogFileNumber
LogFileNumber = 0
Err.Clear
Exit Sub

End Sub

''
' Obfuscate any secure information before logging.
'
Expand Down