- What should we actually log?
- Not too much, not too little
- If REST API, log the calls
- Default Docker logging is not the best
- Need to be logged into cloud provider to see
- Indivualised per instance
- Gone as soon as instance terminates
- Choose between the millions of logging providers
- Best bet is to use Syslog (using TIdSyslog), most providers support that
Note:
- Difficult to determine what beforehand
Add Middleware
MyLogging := TMyLogging.Create;
var LoggingMiddleware := TLoggingMiddleware.Create(MyLogging);
LoggingMiddleware.LogExceptions := True;
LoggingMiddleware.LogLevel := TLogLevel.Trace;
XDataModule.AddMiddleware(LoggingMiddleware);
XDataModule.Events.OnModuleException.Subscribe(
procedure(Args: TModuleExceptionArgs)
begin
LoggingMiddleware.Logger.Error(
Format('Exception %s: %s', [Args.Exception.ClassName, Args.Exception.Message])
);
end);Use x-forwarded-for
function GetOrigin: string;
var
RemoteIp: string;
begin
try
if not TXDataOperationContext.current.Request.Headers.GetIfExists('x-forwarded-for', RemoteIp) then
RemoteIp := TXDataOperationContext.Current.Request.RemoteIp;
Result := RemoteIp + ' ' + TXDataOperationContext.Current.Current.Request.Uri.Path;
except
// lost request context, but still need to return something
Result := 'unknown';
end;
end;Note:
- Don't use if you don't need it, but can be helpful who's doing what
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
