Logging provides developers with visibility into process outputs within sandboxes. You can retrieve logs either in batch or streaming.

Complete code examples demonstrating all operations are available on Blaxel’s GitHub: in TypeScript and in Python.

Retrieve logs

In batch

Retrieve logs for a specific process (using either its name or process ID) after it has completed execution. By default, this retrieves standard output (stdout):


import { SandboxInstance } from "@blaxel/core";

const sandbox = await SandboxInstance.get("my-sandbox")
const process = await sandbox.process.exec({name: "test", command: "echo 'Hello world'"})

const logs = await sandbox.process.logs("test");

To retrieve standard error (stderr):


const logs = await sandbox.process.logs("test", "stderr");

To retrieve both stderr and stdout:


const logs = await sandbox.process.logs("test", "all");

Streaming

Stream logs for a specific process (using either its name or process ID):


// This command will output to both stdout and stderr 5 times with a 1 second sleep between each
const command = `sh -c 'for i in $(seq 1 5); do echo "Hello from stdout $i"; echo "Hello from stderr $i" 1>&2; sleep 1; done'`;

await sandbox.process.exec(
    {
        command: command,
        name: "test",
    },
);
const stream = sandbox.process.streamLogs("test", {
    onLog: (log) => {
        logCalled = true;
        console.log("onLog", log);

        logOutput += log + '\n';
    },
    onStdout: (stdout) => {
        stdoutCalled = true;
        console.log("onStdout", stdout);
        stdoutOutput += stdout + '\n';
    },
    onStderr: (stderr) => {
        stderrCalled = true;
        console.log("onStderr", stderr);
        stderrOutput += stderr + '\n';
    },
})

await sandbox.process.wait("test")

stream.close();