Skip to content

Commit

Permalink
feat: use winston logger instead of pino
Browse files Browse the repository at this point in the history
  • Loading branch information
j1nxie committed Mar 9, 2024
1 parent d083dc4 commit 53d17f2
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 13 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"jsdom": "^24.0.0",
"node-fetch": "^3.3.2",
"redis": "^4.6.13",
"semver": "^7.6.0"
"semver": "^7.6.0",
"winston": "^3.12.0"
}
}
151 changes: 146 additions & 5 deletions pnpm-lock.yaml

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

7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logger from "./logger.js";
import eventRouter from "./routes/eventRoute.js";
import statusRouter from "./routes/statusRoute.js";
import cors from "@fastify/cors";
Expand All @@ -8,7 +9,6 @@ dotenv.config();

const app = fastify({
ignoreTrailingSlash: true,
logger: true,
});

void app.register(eventRouter, { prefix: "/api" });
Expand All @@ -22,9 +22,10 @@ async function start() {
await app.register(cors);
await app.listen({ port: Number(PORT), host: HOST });

app.log.info(`binding to ${HOST} at port ${PORT}.`);
logger.info(`binding to ${HOST} at port ${PORT}.`);
logger.info(`closure-api available on http://${HOST}:${PORT}.`);
} catch (err) {
app.log.error(err);
logger.error(`fatal error: ${err}`);
process.exit(1);
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import winston from "winston";

export function init(appName: string) {
winston.loggers.add("default", {
level: "info",
levels: { fatal: 0, error: 1, warn: 2, info: 3, trace: 4, debug: 5 },
format: winston.format.combine(
winston.format.splat(),
winston.format.timestamp({ format: "YYYY-MM-DD hh:mm:ss A" }),
winston.format.simple(),
winston.format.printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
),
defaultMeta: { service: `${appName}+${process.env.NODE_ENV ?? "development"}` },
transports: [new winston.transports.Console()],
});

const logger = winston.loggers.get("default");

process.on("uncaughtException", (err) => {
// eslint-disable-next-line no-console
console.log("UncaughtException processing: %s", err);
});

// eslint-disable-next-line func-names
logger.child = function () {
return winston.loggers.get("default");
};

return logger;
}

const winstonLogger = init("closure-api");

export default winstonLogger;
Loading

0 comments on commit 53d17f2

Please sign in to comment.