Skip to content

Commit

Permalink
feat: getting all logs without authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanCassiere committed Jun 23, 2024
1 parent 7370449 commit dab3188
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/utils/query-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Takes a URL and returns an object with the query string parameters, multiple of the same key will be an array
*/
export function getQueryParams(url: string): Record<string, string | string[]> {
const search = new URL(url).searchParams;
const params: Record<string, string | string[]> = {};
search.forEach((value, key) => {
if (params[key]) {
if (Array.isArray(params[key])) {
params[key] = [...params[key], value];
} else {
params[key] = [params[key] as string, value];
}
} else {
params[key] = value;
}
});
return params;
}
33 changes: 33 additions & 0 deletions src/v2/logging/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
import { Hono } from "hono";

import type { ServerContext } from "@/types/hono";
import { getQueryParams } from "@/utils/query-string";
import { getLogsFiltersSchema } from "./schemas";
import { db } from "@/config/db";

const app = new Hono<ServerContext>();

/**
* Get all log entries
*/
app.get("/", async (c) => {
const searchQuery = getQueryParams(c.req.url);
const search = getLogsFiltersSchema.parse(searchQuery);

const logLevels = search.level.filter((val) => val !== "all");

const logs = await db.query.logs.findMany({
limit: search.page_size,
offset: search.page_size * (search.page - 1),
orderBy: (fields, { asc, desc }) => (search.sort === "ASC" ? asc(fields.createdAt) : desc(fields.createdAt)),
where: (fields, { and, eq, inArray }) =>
and(
// ...[eq(fields.serviceId, data.serviceId)],
...(search.environment ? [eq(fields.environment, search.environment)] : []),
...(search.lookup ? [eq(fields.lookupFilterValue, search.lookup)] : []),
...(logLevels.length > 0 ? [inArray(fields.level, logLevels)] : []),
),
});

return c.json(logs);
});

/**
* Create a log entry
*/
app.post("/", async (c) => {});

export default app;
45 changes: 45 additions & 0 deletions src/v2/logging/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { z } from "zod";

const f = {
action: z.string(),
environment: z.string(),
ip: z.string().nullable().optional(),
lookupFilterValue: z.string().nullable().optional(),
data: z.record(z.string(), z.any()).nullable(),
level: z
.preprocess(
(val) => {
if (val) return val;
return val;
},
z.enum(["info", "warn", "error", "fatal"]),
)
.default("info"),
levelWithAll: z
.preprocess(
(val) => {
if (val) return val;
return ["all"];
},
z.array(z.enum(["all", "info", "warn", "error", "fatal"])),
)
.default(["all"]),
};

export const createLogSchema = z.object({
action: f.action,
environment: f.environment.default("production"),
ip: f.ip,
lookupFilterValue: f.lookupFilterValue,
data: f.data,
level: f.level,
});

export const getLogsFiltersSchema = z.object({
lookup: f.lookupFilterValue.optional(),
environment: f.environment.optional(),
sort: z.enum(["ASC", "DESC"]).default("DESC"),
page: z.number().min(1).default(1),
page_size: z.number().min(1).default(50),
level: f.levelWithAll,
});

0 comments on commit dab3188

Please sign in to comment.