Template Logging
Retrieve build logs using the SDK.
Default Logger:
javascript
import { Template, defaultBuildLogger } from "e2b";
await Template.build(template, {
alias: "my-template",
onBuildLogs: defaultBuildLogger({
minLevel: "info", // Minimum log level to show
}),
});Python:
python
from e2b import Template, default_build_logger
Template.build(template,
alias="my-template",
on_build_logs=default_build_logger(min_level="info")
)Custom Logger:
Simple logging:
javascript
onBuildLogs: (logEntry) => console.log(logEntry.toString());Custom formatting:
javascript
onBuildLogs: (logEntry) => {
const time = logEntry.timestamp.toISOString();
console.log(`[${time}] ${logEntry.level.toUpperCase()}: ${logEntry.message}`);
};Filter by log level:
javascript
onBuildLogs: (logEntry) => {
if (logEntry.level === "error" || logEntry.level === "warn") {
console.error(logEntry.toString());
}
};LogEntry Structure:
typescript
type LogEntryLevel = 'debug' | 'info' | 'warn' | 'error'
class LogEntry {
constructor(
public readonly timestamp: Date,
public readonly level: LogEntryLevel,
public readonly message: string
)
toString() // Returns formatted log string
}
// Build process start indicator
class LogEntryStart extends LogEntry {
constructor(timestamp: Date, message: string) {
super(timestamp, 'debug', message)
}
}
// Build process end indicator
class LogEntryEnd extends LogEntry {
constructor(timestamp: Date, message: string) {
super(timestamp, 'debug', message)
}
}Using Start/End Indicators:
javascript
if (logEntry instanceof LogEntryStart) {
// Build started
return
}
if (logEntry instanceof LogEntryEnd) {
// Build ended
return
}