Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add format option for logs command

Tomas Valentad500105e1dd2
diff --git a/apps/docs/src/app/cli/commands/page.mdx b/apps/docs/src/app/cli/commands/page.mdx
index 01d8fce51..34bb6b35f 100644
--- a/apps/docs/src/app/cli/commands/page.mdx
+++ b/apps/docs/src/app/cli/commands/page.mdx
@@ -275,4 +275,8 @@ You can use `e2b sandbox list` to get a list of running sandboxes and their IDs
<Option type="-f, --follow">
Enable streaming logs until the sandbox is closed.
</Option>
+ <Option name="format" type="--format">
+ .option('--format <format>', 'keep streaming logs until the sandbox is closed', LogFormat.PRETTY)
+ Enable streaming logs until the sandbox is closed.
+ </Option>
</Options>
diff --git a/packages/cli/src/commands/sandbox/logs.ts b/packages/cli/src/commands/sandbox/logs.ts
index 7fceaca31..b1e6b95df 100644
--- a/packages/cli/src/commands/sandbox/logs.ts
+++ b/packages/cli/src/commands/sandbox/logs.ts
@@ -76,19 +76,26 @@ function isLevelIncluded(level: LogLevel, allowedLevel?: LogLevel) {
}
}
-function formatLogLevels() {
- return Object.values(LogLevel).map(level => asBold(level)).join(', ')
+function formatEnum(e: { [key: string]: string }) {
+ return Object.values(e).map(level => asBold(level)).join(', ')
+}
+
+enum LogFormat {
+ JSON = 'json',
+ PRETTY = 'pretty',
}
export const logsCommand = new commander.Command('logs')
.description('show logs for sandbox')
.argument('<sandboxID>', `show logs for sandbox specified by ${asBold('<sandboxID>')}`)
.alias('lg')
- .option('-l, --level <level>', `filter logs by level (${formatLogLevels})`, LogLevel.INFO)
+ .option('-l, --level <level>', `filter logs by level (${formatEnum(LogLevel)})`, LogLevel.INFO)
.option('-f, --follow', 'keep streaming logs until the sandbox is closed')
+ .option('--format <format>', `specify format for printing logs (${formatEnum(LogFormat)})`, LogFormat.PRETTY)
.action(async (sandboxID: string, opts?: {
level: string,
follow: boolean,
+ format: string,
}) => {
try {
const level = opts?.level.toUpperCase() as LogLevel | undefined
@@ -96,6 +103,11 @@ export const logsCommand = new commander.Command('logs')
throw new Error(`Invalid log level: ${level}`)
}
+ const format = opts?.format.toLowerCase() as LogFormat | undefined
+ if (format && !Object.values(LogFormat).includes(format)) {
+ throw new Error(`Invalid log format: ${format}`)
+ }
+
const apiKey = ensureAPIKey()
const getIsRunning = waitForSandboxEnd(apiKey, sandboxID)
@@ -104,7 +116,9 @@ export const logsCommand = new commander.Command('logs')
let isFirstRun = true
let firstLogsPrinted = false
- console.log(`\nLogs for sandbox ${asBold(sandboxID)}:`)
+ if (format === LogFormat.PRETTY) {
+ console.log(`\nLogs for sandbox ${asBold(sandboxID)}:`)
+ }
const isRunningPromise = listSandboxes({ apiKey }).then(r => r.find(s => getLongID(s.sandboxID, s.clientID) === sandboxID)).then(s => !!s)
@@ -118,18 +132,22 @@ export const logsCommand = new commander.Command('logs')
}
for (const log of logs) {
- printLog(log.timestamp, log.line, level)
+ printLog(log.timestamp, log.line, level, format)
}
const isRunning = await isRunningPromise
if (!isRunning && logs.length === 0 && isFirstRun) {
- console.log(`\nStopped printing logs — sandbox ${withUnderline('not found')}`)
+ if (format === LogFormat.PRETTY) {
+ console.log(`\nStopped printing logs — sandbox ${withUnderline('not found')}`)
+ }
break
}
if (!isRunning) {
- console.log(`\nStopped printing logs — sandbox is ${withUnderline('closed')}`)
+ if (format === LogFormat.PRETTY) {
+ console.log(`\nStopped printing logs — sandbox is ${withUnderline('closed')}`)
+ }
break
}
@@ -168,7 +186,7 @@ export const logsCommand = new commander.Command('logs')
}
})
-function printLog(timestamp: string, line: string, allowedLevel?: LogLevel) {
+function printLog(timestamp: string, line: string, allowedLevel: LogLevel | undefined, format: LogFormat | undefined) {
const log = JSON.parse(line)
const time = `[${new Date(timestamp).toISOString().replace(/T/, ' ').replace(/\..+/, '')}]`
@@ -195,15 +213,19 @@ function printLog(timestamp: string, line: string, allowedLevel?: LogLevel) {
delete log['traceID']
delete log['instanceID']
- delete log['sandboxID']
delete log['source_type']
delete log['teamID']
delete log['source']
delete log['service']
delete log['envID']
- delete log['level']
- console.log(`${asTimestamp(time)} ${level} ` + util.inspect(log, { colors: true, depth: null, maxArrayLength: Infinity, sorted: true, compact: true, breakLength: Infinity }))
+ if (format === LogFormat.JSON) {
+ console.log(JSON.stringify(log))
+ } else {
+ delete log['sandboxID']
+ delete log['level']
+ console.log(`${asTimestamp(time)} ${level} ` + util.inspect(log, { colors: true, depth: null, maxArrayLength: Infinity, sorted: true, compact: true, breakLength: Infinity }))
+ }
}
export async function listSandboxLogs({