Add null logger

Silence logs completely when `null` logger is active.

Add pool size/timeout environment variable configuration for JetQuery.
This commit is contained in:
Bob Farrell 2024-11-12 22:20:13 +00:00
parent bed91b2131
commit 2dd2f7ae74
6 changed files with 43 additions and 9 deletions

View File

@ -15,8 +15,8 @@
.hash = "12201d75d73aad5e1c996de4d5ae87a00e58479c8d469bc2eeb5fdeeac8857bc09af", .hash = "12201d75d73aad5e1c996de4d5ae87a00e58479c8d469bc2eeb5fdeeac8857bc09af",
}, },
.jetquery = .{ .jetquery = .{
.url = "https://github.com/jetzig-framework/jetquery/archive/23c9741a407b6c4f93d9d21508f6568be747bdcc.tar.gz", .url = "https://github.com/jetzig-framework/jetquery/archive/f3d0a02943afb058a968df320032bce7d236ae36.tar.gz",
.hash = "122020374e5fd67d5836c0f3d7a8f814262aa076e3b90c1043f44184da1c2997e0bb", .hash = "12209b52e5a5877008f0aedba23ce1ff778ba8e21a2a4de33182de2d31524b351c88",
}, },
.jetcommon = .{ .jetcommon = .{
.url = "https://github.com/jetzig-framework/jetcommon/archive/a248776ba56d6cc2b160d593ac3305756adcd26e.tar.gz", .url = "https://github.com/jetzig-framework/jetcommon/archive/a248776ba56d6cc2b160d593ac3305756adcd26e.tar.gz",

View File

@ -36,6 +36,8 @@ pub fn main() !void {
std.enums.nameCast(jetzig.jetquery.Environment, jetzig.environment), std.enums.nameCast(jetzig.jetquery.Environment, jetzig.environment),
.{ .env = try jetzig.database.repoEnv(env), .context = .cli }, .{ .env = try jetzig.database.repoEnv(env), .context = .cli },
); );
defer repo.deinit();
const model = comptime jetzig.config.get(jetzig.auth.AuthOptions, "auth").user_model; const model = comptime jetzig.config.get(jetzig.auth.AuthOptions, "auth").user_model;
const stdin = std.io.getStdIn(); const stdin = std.io.getStdIn();
const reader = stdin.reader(); const reader = stdin.reader();

View File

@ -133,7 +133,7 @@ const Options = struct {
\\Minimum log level. Log events below the given level are ignored. Must be one of: { TRACE, DEBUG, INFO, WARN, ERROR, FATAL } (default: DEBUG in development, INFO in production) \\Minimum log level. Log events below the given level are ignored. Must be one of: { TRACE, DEBUG, INFO, WARN, ERROR, FATAL } (default: DEBUG in development, INFO in production)
, ,
.@"log-format" = .@"log-format" =
\\Output logs in the given format. Must be one of: { development, json } (default: development) \\Output logs in the given format. Must be one of: { development, production, json, null } (default: development)
, ,
.detach = .detach =
\\Run the server in the background. Must be used in conjunction with --log (default: false) \\Run the server in the background. Must be used in conjunction with --log (default: false)
@ -231,6 +231,9 @@ pub fn init(parent_allocator: std.mem.Allocator, env_options: EnvironmentOptions
log_queue, log_queue,
), ),
}, },
.null => jetzig.loggers.Logger{
.null_logger = jetzig.loggers.NullLogger{},
},
}; };
if (options.options.detach and std.mem.eql(u8, options.options.log, "-")) { if (options.options.detach and std.mem.eql(u8, options.options.log, "-")) {

View File

@ -49,11 +49,13 @@ pub fn repoEnv(env: jetzig.Environment) !Repo.AdapterOptions {
return switch (comptime adapter) { return switch (comptime adapter) {
.null => .{}, .null => .{},
.postgresql => .{ .postgresql => .{
.hostname = @as(?[]const u8, env.vars.get("JETQUERY_HOSTNAME")), .hostname = env.vars.get("JETQUERY_HOSTNAME"),
.port = @as(?u16, try env.vars.getT(u16, "JETQUERY_PORT")), .port = try env.vars.getT(u16, "JETQUERY_PORT"),
.username = @as(?[]const u8, env.vars.get("JETQUERY_USERNAME")), .username = env.vars.get("JETQUERY_USERNAME"),
.password = @as(?[]const u8, env.vars.get("JETQUERY_PASSWORD")), .password = env.vars.get("JETQUERY_PASSWORD"),
.database = @as(?[]const u8, env.vars.get("JETQUERY_DATABASE")), .database = env.vars.get("JETQUERY_DATABASE"),
.pool_size = try env.vars.getT(u16, "JETQUERY_POOL_SIZE"),
.timeout = try env.vars.getT(u32, "JETQUERY_TIMEOUT"),
}, },
}; };
} }

View File

@ -8,11 +8,12 @@ pub const DevelopmentLogger = @import("loggers/DevelopmentLogger.zig");
pub const JsonLogger = @import("loggers/JsonLogger.zig"); pub const JsonLogger = @import("loggers/JsonLogger.zig");
pub const TestLogger = @import("loggers/TestLogger.zig"); pub const TestLogger = @import("loggers/TestLogger.zig");
pub const ProductionLogger = @import("loggers/ProductionLogger.zig"); pub const ProductionLogger = @import("loggers/ProductionLogger.zig");
pub const NullLogger = @import("loggers/NullLogger.zig");
pub const LogQueue = @import("loggers/LogQueue.zig"); pub const LogQueue = @import("loggers/LogQueue.zig");
pub const LogLevel = enum(u4) { TRACE, DEBUG, INFO, WARN, ERROR, FATAL }; pub const LogLevel = enum(u4) { TRACE, DEBUG, INFO, WARN, ERROR, FATAL };
pub const LogFormat = enum { development, production, json }; pub const LogFormat = enum { development, production, json, null };
/// Infer a log target (stdout or stderr) from a given log level. /// Infer a log target (stdout or stderr) from a given log level.
pub inline fn logTarget(comptime level: LogLevel) LogQueue.Target { pub inline fn logTarget(comptime level: LogLevel) LogQueue.Target {
@ -26,6 +27,7 @@ pub const Logger = union(enum) {
json_logger: JsonLogger, json_logger: JsonLogger,
test_logger: TestLogger, test_logger: TestLogger,
production_logger: ProductionLogger, production_logger: ProductionLogger,
null_logger: NullLogger,
/// Log a TRACE level message to the configured logger. /// Log a TRACE level message to the configured logger.
pub fn TRACE(self: *const Logger, comptime message: []const u8, args: anytype) !void { pub fn TRACE(self: *const Logger, comptime message: []const u8, args: anytype) !void {

View File

@ -0,0 +1,25 @@
const std = @import("std");
const jetzig = @import("../../jetzig.zig");
pub inline fn log(self: @This(), comptime level: jetzig.loggers.LogLevel, comptime message: []const u8, args: anytype) !void {
_ = self;
_ = level;
_ = message;
_ = args;
}
pub inline fn logSql(self: @This(), event: jetzig.jetquery.events.Event) !void {
_ = self;
_ = event;
}
pub inline fn logRequest(self: @This(), request: *const jetzig.http.Request) !void {
_ = self;
_ = request;
}
pub inline fn logError(self: @This(), err: anyerror) !void {
_ = self;
std.debug.print("Error: {s}\n", .{@errorName(err)});
}