Skip to content

Commit be1356b

Browse files
authored
Request logger (#17)
* Sketch Logger untested sketch for logger hook. ideally we should provide options to pipe into a file and/or insert into a table, etc. * DEBUG or SYSLOG testing * syslog * No syslog for win32 builds * fix debug output * Update MainDistributionPipeline.yml * Update README.md
1 parent 6008ce1 commit be1356b

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

.github/workflows/MainDistributionPipeline.yml

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
name: Main Extension Distribution Pipeline
55
on:
66
push:
7+
paths-ignore:
8+
- '**.md'
9+
- '**..yml'
710
pull_request:
811
workflow_dispatch:
912

docs/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ LOAD httpserver;
4141
Start the HTTP server providing the `host`, `port` and `auth` parameters.<br>
4242
> * If you want no authentication, just pass an empty string as parameter.<br>
4343
> * If you want the API run in foreground set `DUCKDB_HTTPSERVER_FOREGROUND=1`
44+
> * If you want logs set `DUCKDB_HTTPSERVER_DEBUG` or `DUCKDB_HTTPSERVER_SYSLOG`
4445
4546
#### Basic Auth
4647
```sql
@@ -90,7 +91,7 @@ D SELECT * FROM duck_flock('SELECT version()', ['http://localhost:9999']);
9091
"version"() │
9192
varchar
9293
├─────────────┤
93-
v1.1.1
94+
v1.1.3
9495
└─────────────┘
9596
```
9697

@@ -122,7 +123,7 @@ curl -X POST -d "SELECT 'hello', version()" "http://localhost:9999/?default_form
122123
"data": [
123124
[
124125
"hello",
125-
"v1.1.1"
126+
"v1.1.3"
126127
]
127128
],
128129
"rows": 1,
@@ -153,7 +154,7 @@ D SELECT * FROM read_json_auto('http://localhost:9999/?q=SELECT version()');
153154
"version"() │
154155
│ varchar │
155156
├─────────────┤
156-
│ v1.1.1
157+
│ v1.1.3
157158
└─────────────┘
158159
```
159160

duckdb

Submodule duckdb updated 480 files

src/httpserver_extension.cpp

+43-5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
#include "duckdb/common/exception/http_exception.hpp"
1010
#include "duckdb/common/allocator.hpp"
1111
#include <chrono>
12+
#include <thread>
13+
#include <memory>
14+
#include <cstdlib>
15+
16+
#ifndef _WIN32
17+
#include <syslog.h>
18+
#endif
1219

1320
#define CPPHTTPLIB_OPENSSL_SUPPORT
1421
#include "httplib.hpp"
15-
16-
// Include yyjson for JSON handling
1722
#include "yyjson.hpp"
1823

19-
#include <thread>
20-
#include <memory>
21-
#include <cstdlib>
2224
#include "play.h"
2325

2426
using namespace duckdb_yyjson; // NOLINT
@@ -384,6 +386,42 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
384386

385387
string host_str = host.GetString();
386388

389+
390+
#ifndef _WIN32
391+
const char* debug_env = std::getenv("DUCKDB_HTTPSERVER_DEBUG");
392+
const char* use_syslog = std::getenv("DUCKDB_HTTPSERVER_SYSLOG");
393+
394+
if (debug_env != nullptr && std::string(debug_env) == "1") {
395+
global_state.server->set_logger([](const duckdb_httplib_openssl::Request& req, const duckdb_httplib_openssl::Response& res) {
396+
time_t now_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
397+
char timestr[32];
398+
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&now_time));
399+
// Use \r\n for consistent line endings
400+
fprintf(stdout, "[%s] %s %s - %d - from %s:%d\r\n",
401+
timestr,
402+
req.method.c_str(),
403+
req.path.c_str(),
404+
res.status,
405+
req.remote_addr.c_str(),
406+
req.remote_port);
407+
fflush(stdout);
408+
});
409+
} else if (use_syslog != nullptr && std::string(use_syslog) == "1") {
410+
openlog("duckdb-httpserver", LOG_PID | LOG_NDELAY, LOG_LOCAL0);
411+
global_state.server->set_logger([](const duckdb_httplib_openssl::Request& req, const duckdb_httplib_openssl::Response& res) {
412+
syslog(LOG_INFO, "%s %s - %d - from %s:%d",
413+
req.method.c_str(),
414+
req.path.c_str(),
415+
res.status,
416+
req.remote_addr.c_str(),
417+
req.remote_port);
418+
});
419+
std::atexit([]() {
420+
closelog();
421+
});
422+
}
423+
#endif
424+
387425
const char* run_in_same_thread_env = std::getenv("DUCKDB_HTTPSERVER_FOREGROUND");
388426
bool run_in_same_thread = (run_in_same_thread_env != nullptr && std::string(run_in_same_thread_env) == "1");
389427

0 commit comments

Comments
 (0)