-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathLogger.zig
52 lines (40 loc) · 1.84 KB
/
Logger.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// This is a sample middleware
// Generally, using a custom Handler with a dispatch method provides the most
// flexibility and should be your first approach (see the dispatcher.zig example).
// Middleware provide an alternative way to manipulate the request/response and
// is well suited if you need different middleware (or middleware configurations)
// for different routes.
const std = @import("std");
const httpz = @import("httpz");
const Logger = @This();
query: bool,
// Must define an `init` method, which will accept your Config
// Alternatively, you can define a init(config: Config, mc: httpz.MiddlewareConfig)
// here mc will give you access to the server's allocator and arena
pub fn init(config: Config) !Logger {
return .{
.query = config.query,
};
}
// optionally you can define an "deinit" method
// pub fn deinit(self: *Logger) void {
// }
// Must define an `execute` method. `self` doesn't have to be `const`, but
// you're responsible for making your middleware thread-safe.
pub fn execute(self: *const Logger, req: *httpz.Request, res: *httpz.Response, executor: anytype) !void {
// Better to use an std.time.Timer to measure elapsed time
// but we need the "start" time for our log anyways, so while this might occasionally
// report wrong/strange "elapsed" time, it's simpler to do.
const start = std.time.microTimestamp();
defer {
const elapsed = std.time.microTimestamp() - start;
std.log.info("{d}\t{s}?{s}\t{d}\t{d}us", .{start, req.url.path, if (self.query) req.url.query else "", res.status, elapsed});
}
// If you don't call executor.next(), there will be no further processing of
// the request and we'll go straight to writing the response.
return executor.next();
}
// Must defined a pub config structure, even if it's empty
pub const Config = struct {
query: bool,
};