feat(logs): add log level selection to tools ui (#3565)
This commit is contained in:
@@ -1,22 +1,33 @@
|
||||
import { observable } from "@trpc/server/observable";
|
||||
import z from "zod";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
import { logLevels } from "@homarr/log/constants";
|
||||
import type { LoggerMessage } from "@homarr/redis";
|
||||
import { loggingChannel } from "@homarr/redis";
|
||||
import { zodEnumFromArray } from "@homarr/validation/enums";
|
||||
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../trpc";
|
||||
|
||||
export const logRouter = createTRPCRouter({
|
||||
subscribe: permissionRequiredProcedure.requiresPermission("other-view-logs").subscription(() => {
|
||||
return observable<LoggerMessage>((emit) => {
|
||||
const unsubscribe = loggingChannel.subscribe((data) => {
|
||||
emit.next(data);
|
||||
});
|
||||
logger.info("A tRPC client has connected to the logging procedure");
|
||||
subscribe: permissionRequiredProcedure
|
||||
.requiresPermission("other-view-logs")
|
||||
.input(
|
||||
z.object({
|
||||
levels: z.array(zodEnumFromArray(logLevels)).default(["info"]),
|
||||
}),
|
||||
)
|
||||
.subscription(({ input }) => {
|
||||
return observable<LoggerMessage>((emit) => {
|
||||
const unsubscribe = loggingChannel.subscribe((data) => {
|
||||
if (!input.levels.includes(data.level)) return;
|
||||
emit.next(data);
|
||||
});
|
||||
logger.info("A tRPC client has connected to the logging procedure");
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
});
|
||||
}),
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
});
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./constants": "./src/constants.ts",
|
||||
"./env": "./src/env.ts"
|
||||
},
|
||||
"typesVersions": {
|
||||
|
||||
17
packages/log/src/constants.ts
Normal file
17
packages/log/src/constants.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export const logLevels = ["error", "warn", "info", "debug"] as const;
|
||||
export type LogLevel = (typeof logLevels)[number];
|
||||
|
||||
export const logLevelConfiguration = {
|
||||
error: {
|
||||
prefix: "🔴",
|
||||
},
|
||||
warn: {
|
||||
prefix: "🟡",
|
||||
},
|
||||
info: {
|
||||
prefix: "🟢",
|
||||
},
|
||||
debug: {
|
||||
prefix: "🔵",
|
||||
},
|
||||
} satisfies Record<LogLevel, { prefix: string }>;
|
||||
@@ -2,9 +2,11 @@ import { z } from "zod";
|
||||
|
||||
import { createEnv } from "@homarr/env";
|
||||
|
||||
import { logLevels } from "./constants";
|
||||
|
||||
export const env = createEnv({
|
||||
server: {
|
||||
LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]).default("info"),
|
||||
LOG_LEVEL: z.enum(logLevels).default("info"),
|
||||
},
|
||||
experimental__runtimeEnv: process.env,
|
||||
});
|
||||
|
||||
@@ -24,7 +24,11 @@ const logTransports: Transport[] = [new transports.Console()];
|
||||
|
||||
// Only add the Redis transport if we are not in CI
|
||||
if (!(Boolean(process.env.CI) || Boolean(process.env.DISABLE_REDIS_LOGS))) {
|
||||
logTransports.push(new RedisTransport());
|
||||
logTransports.push(
|
||||
new RedisTransport({
|
||||
level: "debug",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const logger = winston.createLogger({
|
||||
|
||||
@@ -2,6 +2,9 @@ import { Redis } from "ioredis";
|
||||
import superjson from "superjson";
|
||||
import Transport from "winston-transport";
|
||||
|
||||
const messageSymbol = Symbol.for("message");
|
||||
const levelSymbol = Symbol.for("level");
|
||||
|
||||
//
|
||||
// Inherit from `winston-transport` so you can take advantage
|
||||
// of the base functionality and `.exceptions.handle()`.
|
||||
@@ -12,7 +15,7 @@ export class RedisTransport extends Transport {
|
||||
/**
|
||||
* Log the info to the Redis channel
|
||||
*/
|
||||
log(info: { message: string; timestamp: string; level: string }, callback: () => void) {
|
||||
log(info: { [messageSymbol]: string; [levelSymbol]: string }, callback: () => void) {
|
||||
setImmediate(() => {
|
||||
this.emit("logged", info);
|
||||
});
|
||||
@@ -24,9 +27,8 @@ export class RedisTransport extends Transport {
|
||||
.publish(
|
||||
"pubSub:logging",
|
||||
superjson.stringify({
|
||||
message: info.message,
|
||||
timestamp: info.timestamp,
|
||||
level: info.level,
|
||||
message: info[messageSymbol],
|
||||
level: info[levelSymbol],
|
||||
}),
|
||||
)
|
||||
.then(() => {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { LogLevel } from "@homarr/log/constants";
|
||||
|
||||
import { createListChannel, createQueueChannel, createSubPubChannel } from "./lib/channel";
|
||||
|
||||
export {
|
||||
@@ -27,8 +29,7 @@ export const queueChannel = createQueueChannel<{
|
||||
|
||||
export interface LoggerMessage {
|
||||
message: string;
|
||||
level: string;
|
||||
timestamp: string;
|
||||
level: LogLevel;
|
||||
}
|
||||
|
||||
export const loggingChannel = createSubPubChannel<LoggerMessage>("logging");
|
||||
|
||||
@@ -4204,5 +4204,15 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"log": {
|
||||
"level": {
|
||||
"option": {
|
||||
"debug": "Debug",
|
||||
"info": "Info",
|
||||
"warn": "Warn",
|
||||
"error": "Error"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user