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,
); );
}), }),
}); });
+24 -42
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);
let target = appNameOrId;
let isService = false;
if (!useContainerIdDirectly) {
// Find the real container ID by appName filter
const findResult = await exec(
`docker ps -q --filter "name=^${appNameOrId}" | head -1`,
);
const containerId = findResult.stdout.trim(); const containerId = findResult.stdout.trim();
if (!containerId) { if (!containerId) {
// Fallback: try as a swarm service // Fallback: try as a swarm service
const svcCommand = `docker service ls -q --filter "name=${appName}" | head -1`; const svcResult = await exec(
const svcResult = serverId `docker service ls -q --filter "name=${appNameOrId}" | head -1`,
? await execAsyncRemote(serverId, svcCommand) );
: await execAsync(svcCommand);
const serviceId = svcResult.stdout.trim(); const serviceId = svcResult.stdout.trim();
if (!serviceId) { if (!serviceId) {
throw new Error(`No container or service found for: ${appName}`); throw new Error(`No container or service found for: ${appNameOrId}`);
} }
isService = true;
// Use docker service logs for swarm } else {
const sinceFlag = since === "all" ? "" : `--since ${since}`; target = containerId;
const baseCommand = `docker service logs --timestamps --raw --tail ${tail} ${sinceFlag} ${appName}`;
const escapedSearch = search?.replace(/'/g, "'\\''") ?? "";
const command = search
? `${baseCommand} 2>&1 | grep -iF '${escapedSearch}'`
: `${baseCommand} 2>&1`;
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;
} }
} }
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 (