Skip to content

Commit

Permalink
feat: support the level attribute for creating and querying log rec…
Browse files Browse the repository at this point in the history
…ords (#31)

* feat: downgrade [email protected]

* feat: database migration to support the `level` attribute

* feat: accept log level input

* feat: returning log level on creation

* feat: filtering the returned results based on the log level

* feat: the log levels filter should accept an array of values

* chore: bump package version to 2.2.0
  • Loading branch information
SeanCassiere authored Jan 7, 2024
1 parent 3ed5671 commit 9dc2071
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 44 deletions.
2 changes: 2 additions & 0 deletions drizzle/0002_aromatic_nightcrawler.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "logs" ADD COLUMN "level" text DEFAULT 'info' NOT NULL;--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "log_level_idx" ON "logs" ("level");
18 changes: 5 additions & 13 deletions drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@
"indexes": {
"log_created_at_idx": {
"name": "log_created_at_idx",
"columns": [
"created_at"
],
"columns": ["created_at"],
"isUnique": false
}
},
Expand All @@ -78,12 +76,8 @@
"name": "logs_service_id_services_id_fk",
"tableFrom": "logs",
"tableTo": "services",
"columnsFrom": [
"service_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["service_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "cascade"
}
Expand Down Expand Up @@ -135,9 +129,7 @@
"indexes": {
"service_created_at_idx": {
"name": "service_created_at_idx",
"columns": [
"created_at"
],
"columns": ["created_at"],
"isUnique": false
}
},
Expand All @@ -152,4 +144,4 @@
"tables": {},
"columns": {}
}
}
}
18 changes: 5 additions & 13 deletions drizzle/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@
"indexes": {
"log_created_at_idx": {
"name": "log_created_at_idx",
"columns": [
"created_at"
],
"columns": ["created_at"],
"isUnique": false
}
},
Expand All @@ -78,12 +76,8 @@
"name": "logs_service_id_services_id_fk",
"tableFrom": "logs",
"tableTo": "services",
"columnsFrom": [
"service_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["service_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "cascade"
}
Expand Down Expand Up @@ -135,9 +129,7 @@
"indexes": {
"service_created_at_idx": {
"name": "service_created_at_idx",
"columns": [
"created_at"
],
"columns": ["created_at"],
"isUnique": false
}
},
Expand All @@ -152,4 +144,4 @@
"tables": {},
"columns": {}
}
}
}
171 changes: 171 additions & 0 deletions drizzle/meta/0002_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{
"id": "ce2127be-1277-4db7-b4c3-789c393f369a",
"prevId": "19b9ab6a-21fa-4bb0-91c2-2460725df8c5",
"version": "5",
"dialect": "pg",
"tables": {
"logs": {
"name": "logs",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"action": {
"name": "action",
"type": "text",
"primaryKey": false,
"notNull": true
},
"environment": {
"name": "environment",
"type": "text",
"primaryKey": false,
"notNull": true
},
"ip": {
"name": "ip",
"type": "text",
"primaryKey": false,
"notNull": false
},
"data": {
"name": "data",
"type": "jsonb",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp(3)",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"service_id": {
"name": "service_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"level": {
"name": "level",
"type": "text",
"primaryKey": false,
"notNull": true,
"default": "'info'"
},
"lookup_filter_value": {
"name": "lookup_filter_value",
"type": "text",
"primaryKey": false,
"notNull": false
},
"is_persisted": {
"name": "is_persisted",
"type": "boolean",
"primaryKey": false,
"notNull": true
}
},
"indexes": {
"log_created_at_idx": {
"name": "log_created_at_idx",
"columns": [
"created_at"
],
"isUnique": false
},
"log_level_idx": {
"name": "log_level_idx",
"columns": [
"level"
],
"isUnique": false
}
},
"foreignKeys": {
"logs_service_id_services_id_fk": {
"name": "logs_service_id_services_id_fk",
"tableFrom": "logs",
"tableTo": "services",
"columnsFrom": [
"service_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "cascade"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"services": {
"name": "services",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"is_active": {
"name": "is_active",
"type": "boolean",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp(3)",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"is_persisted": {
"name": "is_persisted",
"type": "boolean",
"primaryKey": false,
"notNull": true
},
"is_admin": {
"name": "is_admin",
"type": "boolean",
"primaryKey": false,
"notNull": true
}
},
"indexes": {
"service_created_at_idx": {
"name": "service_created_at_idx",
"columns": [
"created_at"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
7 changes: 7 additions & 0 deletions drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
"when": 1688826426429,
"tag": "0001_clean_blindfold",
"breakpoints": true
},
{
"idx": 2,
"version": "5",
"when": 1704635877860,
"tag": "0002_aromatic_nightcrawler",
"breakpoints": true
}
]
}
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-logging-server",
"version": "2.1.2",
"version": "2.2.0",
"description": "This is a simple API for logging messages",
"private": true,
"main": "dist/index.js",
Expand All @@ -24,7 +24,7 @@
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.18.0",
"@typescript-eslint/parser": "^6.18.0",
"drizzle-kit": "^0.20.10",
"drizzle-kit": "^0.20.9",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.2",
Expand Down
8 changes: 5 additions & 3 deletions src/components/logging/logging.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function createLogForServiceHandler(
Headers: TXAppServiceIdHeaderSchema;
Body: CreateLogInput;
}>,
reply: FastifyReply
reply: FastifyReply,
) {
if (env.FREEZE_DB_WRITES) {
return reply.code(503).send({ success: false, message: "Database writes are currently frozen" });
Expand All @@ -41,7 +41,7 @@ export async function getLogsForServiceHandler(
Headers: TXAppServiceIdHeaderSchema;
Querystring: TGetLogsSearchParamsInput;
}>,
reply: FastifyReply
reply: FastifyReply,
) {
const serviceId = request.headers["x-app-service-id"];

Expand All @@ -53,6 +53,7 @@ export async function getLogsForServiceHandler(
environment: request.query.environment,
limit: request.query.page_size,
skip: (request.query.page - 1) * request.query.page_size,
level: request.query.level,
});
reply.code(200).send(logs);
} catch (error) {
Expand All @@ -64,7 +65,7 @@ export async function cleanLogsForAllHandler(
request: FastifyRequest<{
Headers: TXAppServiceIdHeaderSchema;
}>,
reply: FastifyReply
reply: FastifyReply,
) {
if (env.FREEZE_DB_WRITES) {
return reply.code(503).send({ success: false, message: "Database writes are currently frozen", errors: [] });
Expand Down Expand Up @@ -93,6 +94,7 @@ export async function cleanLogsForAllHandler(
environment: "production",
lookupFilterValue: "app-admin-action",
data: { client: client.name, ip },
level: "info",
});

reply.statusCode = 200;
Expand Down
Loading

0 comments on commit 9dc2071

Please sign in to comment.