Skip to content

Commit

Permalink
create helper method to parse array values and using direct on the se…
Browse files Browse the repository at this point in the history
…rvice
  • Loading branch information
vhcsilva committed Aug 27, 2024
1 parent 0ce1b6a commit 93889a8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
12 changes: 12 additions & 0 deletions helpers/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function stringifyArrayValues(obj: object) {
const result = { ...obj };

for (const key in result) {
if (Array.isArray(result[key]))
result[key] = { array_data: JSON.stringify(result[key]) };
else if (typeof result[key] === "object")
result[key] = stringifyArrayValues(result[key]);
}

return result
}
8 changes: 3 additions & 5 deletions middleware/log-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ export const LogAccess = (handler: NextApiHandler) => {
const pathname = url.split('/api')[1].replace(/\?.+/g, '');

const payload = {
data: JSON.stringify({
... (query ? { query } : {}),
... (body ? { body } : {})
})
... (query ? { query } : {}),
... (body ? { body } : {})
};

if (serverRuntimeConfig?.accessLogsEnabled)
elasticLoggerMaker(`bepro-access-logs`)
.log(`debug`, ["Access", [{
_type: "access",
payload: { data: JSON.stringify(payload) },
payload,
method,
pathname,
headers: { ...req?.headers, cookie: "removed" },
Expand Down
6 changes: 3 additions & 3 deletions services/elastic-logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {Client} from "@elastic/elasticsearch";
import {LoggerPlugin} from "@taikai/scribal/dist/lib/types";

import { stringifyArrayValues } from "helpers/object";

const {
NEXT_ELASTIC_SEARCH_URL: node,
NEXT_ELASTIC_SEARCH_USERNAME: username,
Expand All @@ -20,9 +22,7 @@ export const elasticLoggerMaker = (index_name: any = `bepro-app-logs`): LoggerPl
const params =
(typeof _params === "string" || typeof _params === "number")
? {value: _params}
: Array.isArray(_params)
? {value_array: JSON.stringify(_params) }
: _params;
: stringifyArrayValues(_params);

new Client({node, auth: {username, password}})
.index({
Expand Down

0 comments on commit 93889a8

Please sign in to comment.