Skip to content

Commit

Permalink
feat(server): Updates to logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTolmay committed Jun 14, 2023
1 parent 0f90fdd commit e3fea46
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/server/lib/log/logEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function logEvent({ context, event, pageId, requestId }) {
context.logger.info({
event,
// TODO:
// app_id ? build_hash, config_hash
// app_name
// app_version
// lowdefy_version
Expand All @@ -30,6 +29,7 @@ function logEvent({ context, event, pageId, requestId }) {
request_id: requestId,
user: {
id: user.id,
roles: user.roles,
sub: user.sub,
session_id: user.session_id, // TODO: Implement session id
},
Expand Down
5 changes: 3 additions & 2 deletions packages/server/lib/serverSidePropsWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ function serverSidePropsWrapper(handler) {
return async function wrappedHandler(nextContext) {
let logger = console;
try {
const { req, res } = nextContext;
const traceId = crypto.randomUUID();

logger = createLogger({ traceId });
const session = await getServerSession(nextContext);
const session = await getServerSession({ req, res, logger });
// Important to give absolute path so Next can trace build files
const context = createApiContext({
buildDirectory: path.join(process.cwd(), 'build'),
config,
fileCache,
headers: nextContext.req.headers,
headers: req.headers,
logger,
session,
});
Expand Down
3 changes: 1 addition & 2 deletions packages/server/pages/[pageId].js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import Page from '../lib/Page.js';

async function getServerSidePropsHandler({ context, nextContext }) {
const { pageId } = nextContext.params;
logEvent({ context, event: 'page_view', pageId });

const [rootConfig, pageConfig] = await Promise.all([
getRootConfig(context),
Expand All @@ -37,7 +36,7 @@ async function getServerSidePropsHandler({ context, nextContext }) {
},
};
}

logEvent({ context, event: 'page_view', pageId });
return {
props: {
pageConfig,
Expand Down
4 changes: 2 additions & 2 deletions packages/server/pages/api/request/[pageId]/[requestId].js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ async function handler({ context, req, res }) {
if (req.method !== 'POST') {
throw new Error('Only POST requests are supported.');
}
const { blockId, pageId, requestId } = req.query;
const { pageId, requestId } = req.query;
logEvent({ context, event: 'request', pageId, requestId });

const { payload } = req.body;
const { blockId, payload } = req.body;
const response = await callRequest(context, { blockId, pageId, payload, requestId });
res.status(200).json(response);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/server/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import logEvent from '../lib/log/logEvent.js';
import serverSidePropsWrapper from '../lib/serverSidePropsWrapper.js';
import Page from '../lib/Page.js';

// TODO: What to log in redirects?
async function getServerSidePropsHandler({ context }) {
logEvent({ context, event: 'page_view', pageId: 'TODO:' });

const rootConfig = await getRootConfig(context);
const { home } = rootConfig;
if (home.configured === false) {
Expand All @@ -42,6 +41,7 @@ async function getServerSidePropsHandler({ context }) {
},
};
}
logEvent({ context, event: 'page_view', pageId: home.pageId });
return {
props: {
pageConfig,
Expand Down

0 comments on commit e3fea46

Please sign in to comment.