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

add custom logger #301

Merged
merged 2 commits into from
Aug 7, 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
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@mui/material": "^5.15.15",
"@statisticsnorway/ssb-component-library": "^2.0.99",
"body-parser": "^1.20.2",
"dotenv": "^16.4.5",
"effect": "^3.3.1",
"eslint-config-prettier": "^9.1.0",
Expand Down
6 changes: 6 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getReasonPhrase } from 'http-status-codes'
import proxy from 'express-http-proxy'
import dotenv from 'dotenv'
import cache from 'memory-cache'
import bodyParser from 'body-parser'

if (!process.env.DAPLA_TEAM_API_URL) {
dotenv.config({ path: './.env.local' })
Expand Down Expand Up @@ -33,6 +34,11 @@ app.use(
})
)

app.post('/log', bodyParser.text({ type: '*/*' }), (req, res) => {
console.log(req.body)
res.send('Ok')
})

app.use(
'/api',
proxy(DAPLA_TEAM_API_URL, {
Expand Down
11 changes: 10 additions & 1 deletion src/pages/TeamOverview/TeamOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import FormattedTableColumn from '../../components/FormattedTableColumn/Formatte
import { User } from '../../services/userProfile'
import { isAuthorizedToCreateTeam } from '../../services/createTeam'
import { Effect } from 'effect'
import { customLogger } from '../../utils/logger.ts'

const MY_TEAMS_TAB = {
title: 'Mine team',
Expand Down Expand Up @@ -79,7 +80,15 @@ const TeamOverview = () => {
if (!user) return

Effect.promise(() => isDaplaAdmin(user.principal_name))
.pipe(Effect.runPromise)
.pipe(
Effect.tap((isDaplaAdmin: boolean) =>
Effect.logInfo(
`username: ${user.principal_name}; job-title: ${user.job_title}; is-dapla-admin: ${isDaplaAdmin}`
)
),
Effect.provide(customLogger),
Effect.runPromise
)
.then((isDaplaAdmin: boolean) => setIsAuthorized(isAuthorizedToCreateTeam(isDaplaAdmin, user.job_title)))
}, [])

Expand Down
12 changes: 12 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Console, Effect, Logger } from 'effect'
import * as Http from '@effect/platform/HttpClient'

const logger = Logger.make(({ logLevel, message }) => {
const logMsg = `[${logLevel.label}] ${message}`
Console.log(logMsg).pipe(
Effect.zipRight(Http.request.post('/log').pipe(Http.request.textBody(logMsg), Http.client.fetchOk, Effect.scoped)),
Effect.runPromise
)
})

export const customLogger = Logger.replace(Logger.defaultLogger, logger)