Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ type ApiResponse<T> = {
async function apiCall<T>(method: string, body?: unknown): Promise<ApiResponse<T>> {

const config = getConfig();
const bearer = config.authToken;

if (!config.authToken) {
if (!bearer) {

return {ok: false, error: 'Not logged in. Run: logfox login'};
return {ok: false, error: 'Not logged in. Run: logfox login or: logfox config set apiKey <key>'};

}

try {

// express-typed-rpc expects method name in URL path, args as body
const response = await fetch(`${config.apiUrl}/v1/${method}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${config.authToken}`,
'Authorization': `Bearer ${bearer}`,
},
body: JSON.stringify(body ?? {}),
});
Expand Down Expand Up @@ -112,38 +112,43 @@ export type LogEntry = {

export type Collector = 'cli' | 'cloudwatch-logs' | 'sdk' | 'vercel' | 'fluentbit';

export async function ingestLogs(teamId: string, appId: string, env: string, logs: LogEntry[]): Promise<ApiResponse<{
success: boolean
logsIngested: number
}>> {
function normalizeEnv(env: string): string {

return apiCall('ingestLogs', {teamId, appId, env, logs});
if (env === 'prod') return 'production';
if (env === 'dev') return 'development';

return env;

}

/**
* Ingest logs via the new /v1/ingest endpoint using API key authentication.
* This is the preferred method for all log ingestion going forward.
*/
export async function ingestLogsV1(
apiKey: string,
export async function ingestLogs(
appId: string,
env: string,
collector: Collector,
logs: LogEntry[],
): Promise<ApiResponse<{success: boolean; logsIngested: number}>> {
collector?: Collector,
): Promise<ApiResponse<{
success: boolean
logsIngested: number
}>> {

const config = getConfig();
const bearer = config.apiKey ?? config.authToken;

if (!bearer) {

return {ok: false, error: 'Not logged in. Run: logfox login or: logfox config set apiKey <key>'};

}

try {

const response = await fetch(`${config.apiUrl}/v1/ingest`, {
const response = await fetch(`${config.apiUrl}/v1/ingestLogs`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
'Authorization': `Bearer ${bearer}`,
},
body: JSON.stringify({appId, env, collector, logs}),
body: JSON.stringify({appId, env: normalizeEnv(env), logs, collector}),
});

if (!response.ok) {
Expand Down
22 changes: 4 additions & 18 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,27 +153,13 @@ export async function run(command: string[], options: RunOptions): Promise<void>
const logsToSend = logBuffer;
logBuffer = [];

// Use API key auth for non-local, or if API key is available
if (config.apiKey) {
const appId = app?.id ?? appName;

const result = await api.ingestLogsV1(config.apiKey, appName, env, 'cli', logsToSend);
const result = await api.ingestLogs(appId, env, logsToSend, 'cli');

if (!result.ok) {
if (!result.ok) {

console.error(`[logfox] Failed to send logs: ${result.error}`);

}

} else if (isLocal && config.teamId && app) {

// Fall back to old auth token method for local
const result = await api.ingestLogs(config.teamId, app.id, 'local', logsToSend);

if (!result.ok) {

console.error(`[logfox] Failed to send logs: ${result.error}`);

}
console.error(`[logfox] Failed to send logs: ${result.error}`);

}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/setup-cloudwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export async function setupCloudwatch(): Promise<void> {
}

const lambdaEnv = {
LOGFOX_ENDPOINT: `${config.apiUrl}/v1/ingest`,
LOGFOX_ENDPOINT: `${config.apiUrl}/v1/ingestLogs`,
LOGFOX_API_KEY: apiKey,
LOG_GROUP_MAPPINGS: JSON.stringify(logGroupMappings),
};
Expand Down
Loading