Standardized logging
grtswt opened this issue · 2 comments
Polaris Feature Request
Is your feature request related to a problem? Please describe
I want to be able to troubleshoot individual scripts after the fact.
Describe the solution you'd like
I'd like to have standardized logging to be able to log which response code was delivered at what time.
Describe alternatives you've considered
I'm thinking about having a standardized log format like the Apache CLF
I have considered writing my own Logging function and overloading the built-in function in the Polaris class. But the problem is that some $Response.Send()
does not have a $Polaris.Log()
.
Also, I'm unsure if the $Polaris.Log() function has access to the $Response and $Request objects, which is required if you want to log the Request-string and Response-code. I
Hi @grtswt,
This might be re-hashing what you've already said above but I just want to make sure I am understanding you correctly.
As you referenced we do currently support custom handlers for what to do with the strings that are passed to the Polaris::log function :
Polaris/Tests/unit/PolarisServer.Tests.ps1
Lines 31 to 41 in ed93821
I'll say this is pretty rudimentary though and although you can use the $Polaris.log functionality to send your own messages there is no way to filter out the built-in logging messages if you just wanted a standardized logging format for details about requests and responses.
A potential solution for logging requests easily would be to write your own logging function and place it in a middleware like this:
function Log-Request([PolarisRequest]$Request) {
# Request Logging Code Here
}
New-PolarisRouteMiddleware -Name "Request Logger" -Scriptblock {
Log-Request -Request $Request
}
You could do something similar with a Log-Response
function but our middleware functions all run before the other blocks so you would have to put a Log-Response -Response $Response
at the end of every route you created which would be kind of a pain.
What you're looking for would be something like this correct?
function Log-Response([PolarisResponse]$Response) {
# Request Logging Code Here
}
New-PolarisRouteMiddleware -Name "Response Logger" -OnResponse -Scriptblock {
Log-Response -Response $Response
}
The logging function I would like to use is like this:
$logfile=$BaseDir + "\logs\http-" + (get-date).ToString("yyyy-MM-dd") + ".log"
'{0} - {1} [{2}] "{3}" {4} {5}' -f $Request.clientIP,$Request.user,$Request.timestamp.ToString("yyyy-MM-dd HH:mm:ss"),$Request.url,$status,$size|out-file -Encoding utf8 -Append -FilePath $logfile
To get the Timestamp to work, I had to create a middleware for that.
New-PolarisRouteMiddleware -Name "AddTimeStamp" -Scriptblock {
$Request | Add-Member -Name TimeStamp -Value (get-date) -MemberType NoteProperty
}
It could be possible to do this by adding the above in each route and middleware. The problem is that the Contexthandler also responds in some cases and the above logging could not be inserted there. At least not without modifying the Polaris source.
Basically I would like to log every time $Response.ByteResponse
is set regardless of the reason.