Skip to content

Commit cdaed14

Browse files
committed
Update README
1 parent b52d7d8 commit cdaed14

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

README.md

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,47 @@ svr.set_file_request_handler([](const Request &req, Response &res) {
269269

270270
### Logging
271271

272+
cpp-httplib provides separate logging capabilities for access logs and error logs, similar to web servers like Nginx and Apache.
273+
274+
#### Access Logging
275+
276+
Access loggers capture successful HTTP requests and responses:
277+
272278
```cpp
273-
svr.set_logger([](const auto& req, const auto& res) {
274-
your_logger(req, res);
279+
svr.set_logger([](const httplib::Request& req, const httplib::Response& res) {
280+
std::cout << req.method << " " << req.path << " -> " << res.status << std::endl;
275281
});
276282
```
277283

278-
You can also set a pre-compression logger to capture request/response data before compression is applied. This is useful for debugging and monitoring purposes when you need to see the original, uncompressed response content:
284+
#### Pre-compression Logging
285+
286+
You can also set a pre-compression logger to capture request/response data before compression is applied:
279287

280288
```cpp
281-
svr.set_pre_compression_logger([](const auto& req, const auto& res) {
289+
svr.set_pre_compression_logger([](const httplib::Request& req, const httplib::Response& res) {
282290
// Log before compression - res.body contains uncompressed content
283291
// Content-Encoding header is not yet set
284292
your_pre_compression_logger(req, res);
285293
});
286294
```
287295

288-
The pre-compression logger is only called when compression would be applied. For responses without compression, only the regular logger is called.
296+
The pre-compression logger is only called when compression would be applied. For responses without compression, only the access logger is called.
297+
298+
#### Error Logging
299+
300+
Error loggers capture failed requests and connection issues. Unlike access loggers, error loggers only receive the Error and Request information, as errors typically occur before a meaningful Response can be generated.
301+
302+
```cpp
303+
svr.set_error_logger([](const httplib::Error& err, const httplib::Request* req) {
304+
std::cerr << httplib::to_string(err) << " while processing request";
305+
if (req) {
306+
std::cerr << ", client: " << req->get_header_value("X-Forwarded-For")
307+
<< ", request: '" << req->method << " " << req->path << " " << req->version << "'"
308+
<< ", host: " << req->get_header_value("Host");
309+
}
310+
std::cerr << std::endl;
311+
});
312+
```
289313

290314
### Error handler
291315

@@ -718,6 +742,51 @@ enum Error {
718742
};
719743
```
720744

745+
### Client Logging
746+
747+
#### Access Logging
748+
749+
```cpp
750+
cli.set_logger([](const httplib::Request& req, const httplib::Response& res) {
751+
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
752+
std::chrono::steady_clock::now() - start_time).count();
753+
std::cout << "✓ " << req.method << " " << req.path
754+
<< " -> " << res.status << " (" << res.body.size() << " bytes, "
755+
<< duration << "ms)" << std::endl;
756+
});
757+
```
758+
759+
#### Error Logging
760+
761+
```cpp
762+
cli.set_error_logger([](const httplib::Error& err, const httplib::Request* req) {
763+
std::cerr << "✗ ";
764+
if (req) {
765+
std::cerr << req->method << " " << req->path << " ";
766+
}
767+
std::cerr << "failed: " << httplib::to_string(err);
768+
769+
// Add specific guidance based on error type
770+
switch (err) {
771+
case httplib::Error::Connection:
772+
std::cerr << " (verify server is running and reachable)";
773+
break;
774+
case httplib::Error::SSLConnection:
775+
std::cerr << " (check SSL certificate and TLS configuration)";
776+
break;
777+
case httplib::Error::ConnectionTimeout:
778+
std::cerr << " (increase timeout or check network latency)";
779+
break;
780+
case httplib::Error::Read:
781+
std::cerr << " (server may have closed connection prematurely)";
782+
break;
783+
default:
784+
break;
785+
}
786+
std::cerr << std::endl;
787+
});
788+
```
789+
721790
### GET with HTTP headers
722791

723792
```c++

0 commit comments

Comments
 (0)