Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Expand Logger For Partner Site #626

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sites/partners/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as winston from "winston"

let log_level = "info"
if (process.env.LOG_LEVEL) {
log_level = process.env.LOG_LEVEL
}

const logger = winston.createLogger({
level: log_level,
format: winston.format.json(),
Expand Down
14 changes: 8 additions & 6 deletions sites/partners/src/pages/api/adapter/[...backendUrl].ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import qs from "qs"
import { wrapper } from "axios-cookiejar-support"
import { CookieJar } from "tough-cookie"
import { getConfigs } from "@bloom-housing/shared-helpers/src/types/backend-swagger"
import { maskAxiosResponse } from "@bloom-housing/shared-helpers"
import { logger } from "../../../logger"

/*
Expand Down Expand Up @@ -33,16 +32,15 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
jar,
})
)
// set up request to backend from request to next api

// eslint-disable-next-line prefer-const
let { backendUrl, ...rest } = req.query
logger.info(`${req.method} - ${backendUrl}`)
logger.debug(req)

if (Array.isArray(backendUrl)) {
backendUrl = backendUrl.join("/")
}

try {
// set up request to backend from request to next api
const configs = getConfigs(req.method || "", "application/json", backendUrl || "", {})
let cookieString = ""
Object.keys(req.cookies).forEach((cookieHeader) => {
Expand All @@ -52,6 +50,10 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
configs.params = rest
configs.data = req.body || {}

// log that call is going out
logger.info(`${req.method} - ${backendUrl}`)
logger.debug(req)

// send request to backend
// the zip endpoints also require a responseType of arraybuffer
const response = await axios.request({
Expand Down Expand Up @@ -85,7 +87,7 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
res.statusMessage = e.response.statusText
res.status(e.response.status).json(e.response.data)
} else {
logger.error("public backend url adapter error:", e)
logger.error("partner backend url adapter error:", e)
}
}
}
11 changes: 9 additions & 2 deletions sites/partners/src/pages/listings/[id]/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MetaTags } from "../../../components/shared/MetaTags"
import ListingGuard from "../../../components/shared/ListingGuard"
import { NavigationHeader } from "../../../components/shared/NavigationHeader"
import { FormListing } from "../../../lib/listings/formTypes"
import { logger } from "../../../logger"

const EditListing = (props: { listing: Listing }) => {
const metaDescription = ""
Expand Down Expand Up @@ -63,11 +64,17 @@ const EditListing = (props: { listing: Listing }) => {

export async function getServerSideProps(context: { params: Record<string, string> }) {
let response
const backendUrl = `/listings/${context.params.id}`

try {
response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`)
logger.info(`GET - ${backendUrl}`)
response = await axios.get(`${process.env.backendApiBase}${backendUrl}`)
} catch (e) {
console.log("e = ", e)
if (e.response) {
logger.error(`GET - ${backendUrl} - ${e.response?.status} - ${e.response?.statusText}`)
} else {
logger.error("partner backend url adapter error:", e)
}
return { notFound: true }
}

Expand Down
10 changes: 9 additions & 1 deletion sites/partners/src/pages/listings/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import DetailPreferences from "../../../components/listings/PaperListingDetails/
import DetailCommunityType from "../../../components/listings/PaperListingDetails/sections/DetailCommunityType"
import DetailPrograms from "../../../components/listings/PaperListingDetails/sections/DetailPrograms"
import DetailListingNotes from "../../../components/listings/PaperListingDetails/sections/DetailNotes"
import { logger } from "../../../logger"

interface ListingProps {
listing: Listing
Expand Down Expand Up @@ -129,10 +130,17 @@ export default function ListingDetail(props: ListingProps) {

export async function getServerSideProps(context: { params: Record<string, string> }) {
let response
const backendUrl = `/listings/${context.params.id}`

try {
response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`)
logger.info(`GET - ${backendUrl}`)
response = await axios.get(`${process.env.backendApiBase}${backendUrl}`)
} catch (e) {
if (e.response) {
logger.error(`GET - ${backendUrl} - ${e.response?.status} - ${e.response?.statusText}`)
} else {
logger.error("partner backend url adapter error:", e)
}
return { notFound: true }
}

Expand Down
12 changes: 12 additions & 0 deletions sites/public/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as winston from "winston"
let log_level = "info"
if (process.env.LOG_LEVEL) {
log_level = process.env.LOG_LEVEL
}
const logger = winston.createLogger({
level: log_level,
format: winston.format.json(),
defaultMeta: { service: "doorway-public" },
transports: [new winston.transports.Console()],
})
export { logger }
14 changes: 8 additions & 6 deletions sites/public/src/pages/api/adapter/[...backendUrl].ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import qs from "qs"
import { wrapper } from "axios-cookiejar-support"
import { CookieJar } from "tough-cookie"
import { getConfigs } from "@bloom-housing/shared-helpers/src/types/backend-swagger"
import { maskAxiosResponse } from "@bloom-housing/shared-helpers"
import { logger } from "../../../../logger"
import { logger } from "../../../logger"

/*
This file exists as per https://nextjs.org/docs/api-routes/dynamic-api-routes
Expand All @@ -30,16 +29,15 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
jar,
})
)
// set up request to backend from request to next api

// eslint-disable-next-line prefer-const
let { backendUrl, ...rest } = req.query
logger.info(`${req.method} - ${backendUrl}`)
logger.debug(req)

if (Array.isArray(backendUrl)) {
backendUrl = backendUrl.join("/")
}

try {
// set up request to backend from request to next api
const configs = getConfigs(req.method || "", "application/json", backendUrl || "", {})
let cookieString = ""
Object.keys(req.cookies).forEach((cookieHeader) => {
Expand All @@ -49,6 +47,10 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
configs.params = rest
configs.data = req.body || {}

// log that call is going out
logger.info(`${req.method} - ${backendUrl}`)
logger.debug(req)

// send request to backend
const response = await axios.request(configs)

Expand Down
Loading