refactor: enhance getContainerLogs function to support app name or ID

- Updated the `getContainerLogs` function to accept either an application name or container ID, improving flexibility in log retrieval.
- Simplified the command execution logic by consolidating the remote and local execution paths.
- Added a new parameter to directly use container IDs, streamlining the process for users.

These changes enhance the usability of the logging feature, allowing for more efficient access to container logs.
This commit is contained in:
Mauricio Siu
2026-04-09 11:41:01 -06:00
parent 7c10610a5a
commit b8db120432
2 changed files with 28 additions and 45 deletions
@@ -1168,6 +1168,7 @@ export const composeRouter = createTRPCRouter({
input.since, input.since,
input.search, input.search,
compose.serverId, compose.serverId,
true,
); );
}), }),
}); });
+27 -45
View File
@@ -355,60 +355,45 @@ export const getContainersByAppLabel = async (
}; };
export const getContainerLogs = async ( export const getContainerLogs = async (
appName: string, appNameOrId: string,
tail = 100, tail = 100,
since = "all", since = "all",
search?: string, search?: string,
serverId?: string | null, serverId?: string | null,
useContainerIdDirectly = false,
): Promise<string> => { ): Promise<string> => {
// First, find the real container ID by appName filter const exec = (cmd: string) =>
const findCommand = `docker ps -q --filter "name=^${appName}" | head -1`; serverId ? execAsyncRemote(serverId, cmd) : execAsync(cmd);
const findResult = serverId
? await execAsyncRemote(serverId, findCommand)
: await execAsync(findCommand);
const containerId = findResult.stdout.trim(); let target = appNameOrId;
if (!containerId) { let isService = false;
// Fallback: try as a swarm service
const svcCommand = `docker service ls -q --filter "name=${appName}" | head -1`;
const svcResult = serverId
? await execAsyncRemote(serverId, svcCommand)
: await execAsync(svcCommand);
const serviceId = svcResult.stdout.trim(); if (!useContainerIdDirectly) {
if (!serviceId) { // Find the real container ID by appName filter
throw new Error(`No container or service found for: ${appName}`); const findResult = await exec(
} `docker ps -q --filter "name=^${appNameOrId}" | head -1`,
);
const containerId = findResult.stdout.trim();
// Use docker service logs for swarm if (!containerId) {
const sinceFlag = since === "all" ? "" : `--since ${since}`; // Fallback: try as a swarm service
const baseCommand = `docker service logs --timestamps --raw --tail ${tail} ${sinceFlag} ${appName}`; const svcResult = await exec(
const escapedSearch = search?.replace(/'/g, "'\\''") ?? ""; `docker service ls -q --filter "name=${appNameOrId}" | head -1`,
const command = search );
? `${baseCommand} 2>&1 | grep -iF '${escapedSearch}'` const serviceId = svcResult.stdout.trim();
: `${baseCommand} 2>&1`; if (!serviceId) {
throw new Error(`No container or service found for: ${appNameOrId}`);
try {
const result = serverId
? await execAsyncRemote(serverId, command)
: await execAsync(command);
return result.stdout;
} catch (error: unknown) {
if (
error &&
typeof error === "object" &&
"stdout" in error &&
typeof (error as { stdout: string }).stdout === "string" &&
(error as { stdout: string }).stdout.length > 0
) {
return (error as { stdout: string }).stdout;
} }
throw error; isService = true;
} else {
target = containerId;
} }
} }
const sinceFlag = since === "all" ? "" : `--since ${since}`; const sinceFlag = since === "all" ? "" : `--since ${since}`;
const baseCommand = `docker container logs --timestamps --tail ${tail} ${sinceFlag} ${containerId}`; const baseCommand = isService
? `docker service logs --timestamps --raw --tail ${tail} ${sinceFlag} ${target}`
: `docker container logs --timestamps --tail ${tail} ${sinceFlag} ${target}`;
const escapedSearch = search?.replace(/'/g, "'\\''") ?? ""; const escapedSearch = search?.replace(/'/g, "'\\''") ?? "";
const command = search const command = search
@@ -416,10 +401,7 @@ export const getContainerLogs = async (
: `${baseCommand} 2>&1`; : `${baseCommand} 2>&1`;
try { try {
const result = serverId const result = await exec(command);
? await execAsyncRemote(serverId, command)
: await execAsync(command);
return result.stdout; return result.stdout;
} catch (error: unknown) { } catch (error: unknown) {
if ( if (