Middleware designed to register all http requests and requests on the Horse.
Support: [email protected]
$ boss install github.com/dliocode/horse-dataloggerAdd the following folders to your project, in Project> Options> Delphi Compiler> Search Path
../src
Datalogger - This is a tool used to register all http Horse requests.
For more information on how to use this tool in other situations, click here.
Clientip - Used to capture the IP.
To use this middleware you need to understand some things.
Providers : It is essentially for storing your logs.
Diponable Providers : Click HERE
At which position it is recommended to use this provider in the Horse: We recommend that it be added in the first position to record all information passed by it.
To use it is necessary to add to uses Horse.DataLogger and then add the designs chosen to register the logs;
Now that you have understood a little about how it works, let's go to the examples;
uses
Horse, Horse.Constants,
Horse.DataLogger,
DataLogger.Provider.Console, // Provider para Console
System.SysUtils;
begin
THorse
.Use(THorseDataLogger.Logger([TProviderConsole.Create])) // Adiconando Middleware e o Provider
.Get( ' /ping ' ,
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send( ' pong ' );
end );
THorse.KeepConnectionAlive := True;
THorse.Listen( 8080 ,
procedure(AHorse: THorse)
begin
Writeln( ' ' + Format(START_RUNNING, [THorse.Host, THorse.Port]));
Writeln;
end );
end . Combined, Common, Dev, Short, Tiny
Each format has a different and pre -established structure.
Standard Apache Combined Log output.
${request_remote_clientip} [${time}] "${request_method} ${request_raw_path_info}${request_query} '${request_protocol_version}" ${response_status_code} ${response_content_length} "${request_referer}" "${request_user_agent}"
Common logging output.
${request_remote_clientip} [${time}] "${request_method} ${request_raw_path_info}${request_query} '${request_protocol_version}" ${response_status_code} ${response_content_length}
Simple log output
${request_method} ${request_raw_path_info}${request_query} ${response_status_code} ${execution_time} ms - ${response_content_length}
Shorter than the standard, also including response time.
${request_remote_clientip} ${request_method} ${request_raw_path_info}${request_query} ${request_protocol_version} ${response_status_code} ${response_content_length} ${execution_time} ms
Minimum log output
${request_method} ${request_raw_path_info}${request_query} ${response_status_code} ${response_content_length} - ${execution_time} ms
uses
Horse, Horse.Constants,
Horse.DataLogger,
DataLogger.Provider.Console, // Provider para Console
System.SysUtils;
begin
THorse
.Use(
THorseDataLogger.Logger(
THorseDataLoggerFormat.tfCombined, // Formato dos logs
[TProviderConsole.Create] // Adicionado o Middleware
)
)
.Get( ' /ping ' ,
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send( ' pong ' );
end );
// output: 0:0:0:0:0:0:0:1 [2022-12-22 17:18:31:791] "GET /ping HTTP/1.1" 200 4 "-" "PostmanRuntime/7.30.0"
THorse.KeepConnectionAlive := True;
THorse.Listen( 8080 ,
procedure(AHorse: THorse)
begin
Writeln( ' ' + Format(START_RUNNING, [THorse.Host, THorse.Port]));
Writeln;
end );
end .You can define your own formats of use
uses
Horse, Horse.Constants,
Horse.DataLogger,
DataLogger.Provider.Console, // Provider para Console
System.SysUtils;
begin
THorse
.Use(
THorseDataLogger.Logger(
// Formato dos logs
' ${request_method} ${request_raw_path_info}${request_query} ${response_status_code} ${response_content_length} - ${execution_time} ms ' ,
// Adicionado o Middleware
[TProviderConsole.Create]
)
)
.Get( ' /ping ' ,
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send( ' pong ' );
end );
// output: 0:0:0:0:0:0:0:1 [2022-12-22 17:18:31:791] "GET /ping HTTP/1.1" 200 4 "-" "PostmanRuntime/7.30.0"
THorse.KeepConnectionAlive := True;
THorse.Listen( 8080 ,
procedure(AHorse: THorse)
begin
Writeln( ' ' + Format(START_RUNNING, [THorse.Host, THorse.Port]));
Writeln;
end );
end . ${time}
${execution_time} // Time in ms
${request_accept}
${request_authorization}
${request_cache_control}
${request_connection}
${request_content}
${request_content_encoding}
${request_content_length}
${request_content_type}
${request_content_version}
${request_cookie}
${request_cookie_fields}
${request_derived_from}
${request_from}
${request_host}
${request_internal_path_info}
${request_internal_script_name}
${request_method}
${request_path_info}
${request_path_translated}
${request_protocol_version}
${request_query}
${request_query_fields}
${request_raw_path_info}
${request_referer}
${request_remote_addr}
${request_remote_clientip}
${request_remote_host}
${request_remote_ip}
${request_script_name}
${request_server_port}
${request_title}
${request_url}
${request_user_agent}
${response_allow}
${response_content}
${response_content_encoding}
${response_content_length}
${response_content_type}
${response_content_version}
${response_custom_headers}
${response_date}
${response_derived_from}
${response_expires}
${response_last_modified}
${response_location}
${response_log_message}
${response_realm}
${response_reason}
${response_server}
${response_status_code}
${response_title}
${response_version}
${response_wwwauthenticate}
You can add multiple provides to register each request in different locations.
For this example, we will show the console requests and save in text format, all using two units to register
DataLogger.Provider.Console, DataLogger.Provider.TextFile
uses
Horse, Horse.Constants,
Horse.DataLogger,
DataLogger.Provider.Console, // Provider para Console
DataLogger.Provider.TextFile, // Provider para TextFile
System.IOUtils, System.SysUtils;
begin
THorse
.Use(
THorseDataLogger.Logger(
THorseDataLoggerFormat.tfCombined, // Formato dos logs
TProviderConsole.Create,
TProviderTextFile.Create
.LogDir(TPath.GetDirectoryName(ParamStr( 0 )) + ' logrequest ' )
.PrefixFileName( ' request_ ' )
.Extension( ' .txt ' )
]
)
)
.Get( ' /ping ' ,
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send( ' pong ' );
end );
// output: 0:0:0:0:0:0:0:1 [2022-12-22 17:18:31:791] "GET /ping HTTP/1.1" 200 4 "-" "PostmanRuntime/7.30.0"
THorse.KeepConnectionAlive := True;
THorse.Listen( 8080 ,
procedure(AHorse: THorse)
begin
Writeln( ' ' + Format(START_RUNNING, [THorse.Host, THorse.Port]));
Writeln;
end );
end .