UNPKG

@opengis/fastify-table

Version:

core-plugins

65 lines (64 loc) 2.01 kB
import { pino } from "pino"; import config from "../../../config.js"; import redactionList from "../../../redactionList.js"; import getRedis from "../redis/funcs/getRedis.js"; // utils import getHooks from "./getHooks.js"; import serializers from "./serializers.js"; import timestampWithTimeZone from "./timestampWithTimeZone.js"; const isServer = process.argv[2]; const level = process.env.LOG_LEVEL || "info"; console.log(`log level: ${level}`); // skip logging during npx vitest run const targets = process.env.VITEST ? [] : [ { target: "./createFileStream.js", // path.resolve('utils/createFileStream.js') }, { level: "error", target: "pino/file", options: { destination: 1 }, }, ]; // push trace logs to console (those would not be saved to file) if (["trace", "debug"].includes(level)) { targets.push({ level, target: "pino/file", options: { destination: 1 }, }); } const options = { level, // minimal log level to write timestamp: () => `,"time":"${timestampWithTimeZone()}"`, // timestamp as isostring hooks: getHooks(), serializers, // custom log params transport: { targets, }, redact: redactionList, }; const logger = pino(options); logger.file = function userFile(logfolder, msg, req) { logger.info({ logfolder, ...(typeof msg === "string" ? { msg } : msg) }, req); }; if (config.debug) { logger.file("test/redaction", { clientId: "should be redacted", clientSecret: "should be redacted", }); } logger.metrics = function metrics(key, val, dbName) { const rclient2 = getRedis({ db: 2 }); const dbname = dbName || config.pg?.database; if (!dbname && !isServer) return; if (!config.redis) return; rclient2 .hincrby(`${dbname}:system_metrics`, key, val || 1) .catch((err) => console.warn("⚠️ logger metrics error", err.toString())); }; export default logger;