mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
Colorize request method in log output
This commit is contained in:
parent
cb52ccffb9
commit
b66073955d
@ -6,9 +6,8 @@ const Data = jetzig.data.Data;
|
|||||||
const View = jetzig.views.View;
|
const View = jetzig.views.View;
|
||||||
|
|
||||||
pub fn index(request: *Request, data: *Data) anyerror!View {
|
pub fn index(request: *Request, data: *Data) anyerror!View {
|
||||||
_ = request;
|
|
||||||
var object = try data.object();
|
var object = try data.object();
|
||||||
try object.put("foo", data.string("hello"));
|
try object.put("foo", data.string("hello"));
|
||||||
return error.OhNo;
|
// return error.OhNo;
|
||||||
// return request.render(.ok);
|
return request.render(.ok);
|
||||||
}
|
}
|
||||||
|
@ -95,9 +95,9 @@ pub fn duration(allocator: std.mem.Allocator, delta: i64) ![]const u8 {
|
|||||||
var buf: [1024]u8 = undefined;
|
var buf: [1024]u8 = undefined;
|
||||||
const formatted_duration = try std.fmt.bufPrint(&buf, "{}", .{std.fmt.fmtDurationSigned(delta)});
|
const formatted_duration = try std.fmt.bufPrint(&buf, "{}", .{std.fmt.fmtDurationSigned(delta)});
|
||||||
|
|
||||||
if (delta < 100000) {
|
if (delta < 1000000) {
|
||||||
return try runtimeGreen(allocator, formatted_duration);
|
return try runtimeGreen(allocator, formatted_duration);
|
||||||
} else if (delta < 500000) {
|
} else if (delta < 5000000) {
|
||||||
return try runtimeYellow(allocator, formatted_duration);
|
return try runtimeYellow(allocator, formatted_duration);
|
||||||
} else {
|
} else {
|
||||||
return try runtimeRed(allocator, formatted_duration);
|
return try runtimeRed(allocator, formatted_duration);
|
||||||
|
@ -126,6 +126,18 @@ pub fn hash(self: *Self) ![]const u8 {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn fmtMethod(self: *Self) []const u8 {
|
||||||
|
return switch (self.method) {
|
||||||
|
.GET => jetzig.colors.cyan("GET"),
|
||||||
|
.PUT => jetzig.colors.yellow("PUT"),
|
||||||
|
.PATCH => jetzig.colors.yellow("PATCH"),
|
||||||
|
.HEAD => jetzig.colors.white("HEAD"),
|
||||||
|
.POST => jetzig.colors.green("POST"),
|
||||||
|
.DELETE => jetzig.colors.red("DELETE"),
|
||||||
|
inline else => |method| jetzig.colors.white(@tagName(method)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn resourceModifier(self: *Self) ?Modifier {
|
pub fn resourceModifier(self: *Self) ?Modifier {
|
||||||
const basename = std.fs.path.basename(self.segments.items[self.segments.items.len - 1]);
|
const basename = std.fs.path.basename(self.segments.items[self.segments.items.len - 1]);
|
||||||
const extension = std.fs.path.extension(basename);
|
const extension = std.fs.path.extension(basename);
|
||||||
|
@ -234,10 +234,10 @@ fn requestLogMessage(self: *Self, request: *jetzig.http.Request, result: jetzig.
|
|||||||
const formatted_duration = try jetzig.colors.duration(self.allocator, self.duration());
|
const formatted_duration = try jetzig.colors.duration(self.allocator, self.duration());
|
||||||
defer self.allocator.free(formatted_duration);
|
defer self.allocator.free(formatted_duration);
|
||||||
|
|
||||||
return try std.fmt.allocPrint(self.allocator, "[{s} {s}] {s} {s}", .{
|
return try std.fmt.allocPrint(self.allocator, "[{s}/{s}/{s}] {s}", .{
|
||||||
status.format(),
|
|
||||||
formatted_duration,
|
formatted_duration,
|
||||||
@tagName(request.method),
|
request.fmtMethod(),
|
||||||
|
status.format(),
|
||||||
request.path,
|
request.path,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user