Skip to content
Open
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
60 changes: 34 additions & 26 deletions app/utils/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// is static and logger is later modified in `app/server/index.ts` to
// disable debug logging if the `HEADPLANE_DEBUG_LOG` specifies as such.

import pino from "pino";

const levels = ["info", "warn", "error", "debug"] as const;
type Category = "server" | "config" | "agent" | "api" | "auth" | "sse";

Expand All @@ -13,32 +15,38 @@ export interface Logger extends Record<
debugEnabled: boolean;
}

const logLevels = getLogLevels();
export default {
debugEnabled: logLevels.includes("debug"),
debug: (..._: Parameters<Logger["debug"]>) => {},
...Object.fromEntries(
logLevels.map((level) => [
level,
(category: Category, message: string, ...args: unknown[]) => {
const date = new Date().toISOString();
console.log(`${date} [${category}] ${level.toUpperCase()}: ${message}`, ...args);
},
]),
),
} as Logger;

function getLogLevels() {
function isDebugEnabled() {
const debugLog = process.env.HEADPLANE_DEBUG_LOG;
if (debugLog == null) {
return ["info", "warn", "error"];
}

if (debugLog == null) return false;
const normalized = debugLog.trim().toLowerCase();
const truthyValues = ["1", "true", "yes", "on"];
if (!truthyValues.includes(normalized)) {
return ["info", "warn", "error"];
}

return ["info", "warn", "error", "debug"];
return ["1", "true", "yes", "on"].includes(normalized);
}

const debugEnabled = isDebugEnabled();

const pinoLogger = pino({
level: debugEnabled ? "debug" : "info",
formatters: {
level: (label) => {
return { level: label };
},
},
});

export default {
debugEnabled,
debug: (...args: Parameters<Logger["debug"]>) => {
if (!debugEnabled) return;
const [category, message, ...rest] = args;
pinoLogger.debug({ category }, message, ...rest);
},
info: (category: Category, message: string, ...args: unknown[]) => {
pinoLogger.info({ category }, message, ...args);
},
warn: (category: Category, message: string, ...args: unknown[]) => {
pinoLogger.warn({ category }, message, ...args);
},
error: (category: Category, message: string, ...args: unknown[]) => {
pinoLogger.error({ category }, message, ...args);
},
} as Logger;
2 changes: 1 addition & 1 deletion nix/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ in
inherit (finalAttrs) pname version src;
fetcherVersion = 3;
pnpm = pnpm_10;
hash = "sha256-NGIeboj/2kXuWsmTVl1fv4LgU1VYRdO+qSnNLVuneC8=";
hash = "sha256-QHGsNb4A9hctK0EDqXe5fzTndc8r+X38Sb4aJA3/ZOc=";
};

buildPhase = ''
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"lucide-react": "^1.8.0",
"mime": "^4.1.0",
"openapi-types": "^12.1.3",
"pino": "^10.3.1",
"react": "19.2.5",
"react-codemirror-merge": "4.25.9",
"react-dom": "19.2.5",
Expand All @@ -69,6 +70,7 @@
"oxfmt": "^0.44.0",
"oxlint": "^1.59.0",
"oxlint-tsgolint": "^0.20.0",
"pino-pretty": "^13.1.3",
"tailwindcss": "^4.2.2",
"tailwindcss-animate": "^1.0.7",
"testcontainers": "^11.14.0",
Expand Down
Loading
Loading