Skip to content

Commit

Permalink
Ref: naming: just logger in server and its helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Nov 14, 2024
1 parent 0d6ad46 commit 850a5f9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/server-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ export const moveRaw: RequestHandler = (req, {}, next) => {
/** @since v19 prints the actual path of the request, not a configured route, severity decreased to debug level */
export const createLoggingMiddleware =
({
rootLogger,
logger: parent,
config,
}: {
rootLogger: ActualLogger;
logger: ActualLogger;
config: CommonConfig;
}): RequestHandler =>
async (request, response, next) => {
const logger = config.childLoggerProvider
? await config.childLoggerProvider({ request, parent: rootLogger })
: rootLogger;
? await config.childLoggerProvider({ request, parent })
: parent;
logger.debug(`${request.method}: ${request.path}`);
if (request.res)
(request as EquippedRequest).res!.locals[metaSymbol] = { logger };
Expand Down
28 changes: 12 additions & 16 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,43 @@ import { getStartupLogo } from "./startup-logo";
const makeCommonEntities = (config: CommonConfig) => {
if (config.startupLogo !== false) console.log(getStartupLogo());
const errorHandler = config.errorHandler || defaultResultHandler;
const rootLogger = isLoggerInstance(config.logger)
const logger = isLoggerInstance(config.logger)
? config.logger
: new BuiltinLogger(config.logger);
rootLogger.debug("Running", {
logger.debug("Running", {
build: process.env.TSUP_BUILD || "from sources",
env: process.env.NODE_ENV || "development",
});
installDeprecationListener(rootLogger);
const loggingMiddleware = createLoggingMiddleware({ rootLogger, config });
const getLogger = makeGetLogger(rootLogger);
installDeprecationListener(logger);
const loggingMiddleware = createLoggingMiddleware({ logger, config });
const getLogger = makeGetLogger(logger);
const commons = { getLogger, errorHandler };
const notFoundHandler = createNotFoundHandler(commons);
const parserFailureHandler = createParserFailureHandler(commons);
return {
...commons,
rootLogger,
logger,
notFoundHandler,
parserFailureHandler,
loggingMiddleware,
};
};

export const attachRouting = (config: AppConfig, routing: Routing) => {
const { rootLogger, getLogger, notFoundHandler, loggingMiddleware } =
const { logger, getLogger, notFoundHandler, loggingMiddleware } =
makeCommonEntities(config);
initRouting({
app: config.app.use(loggingMiddleware),
routing,
getLogger,
config,
});
return { notFoundHandler, logger: rootLogger };
return { notFoundHandler, logger };
};

export const createServer = async (config: ServerConfig, routing: Routing) => {
const {
rootLogger,
logger,
getLogger,
notFoundHandler,
parserFailureHandler,
Expand Down Expand Up @@ -95,7 +95,7 @@ export const createServer = async (config: ServerConfig, routing: Routing) => {

const makeStarter =
(server: http.Server | https.Server, subject: HttpConfig["listen"]) => () =>
server.listen(subject, () => rootLogger.info("Listening", subject));
server.listen(subject, () => logger.info("Listening", subject));

const created: Array<http.Server | https.Server> = [];
const starters: Array<() => http.Server | https.Server> = [];
Expand All @@ -112,15 +112,11 @@ export const createServer = async (config: ServerConfig, routing: Routing) => {

if (config.gracefulShutdown) {
installTerminationListener({
logger,
servers: created,
logger: rootLogger,
options: config.gracefulShutdown === true ? {} : config.gracefulShutdown,
});
}

return {
app,
logger: rootLogger,
servers: starters.map((starter) => starter()),
};
return { app, logger, servers: starters.map((starter) => starter()) };
};
8 changes: 4 additions & 4 deletions tests/unit/server-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ describe("Server helpers", () => {
});

describe("createLoggingMiddleware", () => {
const rootLogger = makeLoggerMock();
const logger = makeLoggerMock();
const child = makeLoggerMock({ isChild: true });
test.each([undefined, () => child, async () => child])(
"should make RequestHandler writing logger to res.locals %#",
async (childLoggerProvider) => {
const config = { childLoggerProvider } as CommonConfig;
const handler = createLoggingMiddleware({ rootLogger, config });
const handler = createLoggingMiddleware({ logger, config });
expect(typeof handler).toBe("function");
const nextMock = vi.fn();
const response = makeResponseMock();
Expand All @@ -252,10 +252,10 @@ describe("Server helpers", () => {
await handler(request, response, nextMock);
expect(nextMock).toHaveBeenCalled();
expect(
(childLoggerProvider ? child : rootLogger)._getLogs().debug.pop(),
(childLoggerProvider ? child : logger)._getLogs().debug.pop(),
).toEqual(["GET: /test"]);
expect(request.res).toHaveProperty("locals", {
[metaSymbol]: { logger: childLoggerProvider ? child : rootLogger },
[metaSymbol]: { logger: childLoggerProvider ? child : logger },
});
},
);
Expand Down

0 comments on commit 850a5f9

Please sign in to comment.