Skip to content

Latest commit

 

History

History
71 lines (53 loc) · 2.01 KB

File metadata and controls

71 lines (53 loc) · 2.01 KB

Sawing Logs

  • 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

Logging for API frameworks

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);

Logging for API frameworks

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:


Sawing Logs

Layout with logging