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
5 changes: 3 additions & 2 deletions server/dev-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Plugin } from 'vite';
import { handleNodeRequest } from './utils/http';
import { logInfo } from './utils/logger';
import { loadServerEnv } from './env';
import { resetOctokit } from './github/client';

Expand All @@ -12,8 +13,8 @@ export function apiMiddleware(): Plugin {

server.httpServer?.once('listening', () => {
const hasToken = Boolean(process.env.GITHUB_TOKEN?.trim());
console.log(
`[gitSdm] GitHub API: ${hasToken ? 'authenticated (GITHUB_TOKEN loaded)' : 'unauthenticated — add GITHUB_TOKEN to .env'}`,
logInfo(
`[gitSdm] GitHub API: ${hasToken ? 'authenticated (GITHUB_TOKEN loaded)' : 'unauthenticated — add GITHUB_TOKEN to .env'}`
);
});

Expand Down
5 changes: 3 additions & 2 deletions server/prod-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'node:path';
import { handleApiRequest } from './api-router';
import { resetOctokit } from './github/client';
import { addSecurityHeaders } from './utils/http';
import { logInfo } from './utils/logger';

const distDir = path.resolve(import.meta.dir, '../dist');
const indexFile = path.join(distDir, 'index.html');
Expand Down Expand Up @@ -46,8 +47,8 @@ Bun.serve({
});

const hasToken = Boolean(process.env.GITHUB_TOKEN?.trim());
console.log(`[gitSdm] listening on http://${host}:${port}`);
console.log(`[gitSdm] GitHub API: ${hasToken ? 'authenticated' : 'unauthenticated'}`);
logInfo(`[gitSdm] listening on http://${host}:${port}`);
logInfo(`[gitSdm] GitHub API: ${hasToken ? 'authenticated' : 'unauthenticated'}`);

function safeJoin(root: string, pathname: string): string {
const normalized = pathname.replace(/^[/\\]+/, '');
Expand Down
18 changes: 17 additions & 1 deletion server/utils/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, spyOn } from 'bun:test';
import { logApi, logError } from './logger';
import { logApi, logError, logInfo } from './logger';

describe('utils/logger', () => {
it('logs API request metadata to console.log', () => {
Expand Down Expand Up @@ -76,4 +76,20 @@ describe('utils/logger', () => {
errorSpy.mockRestore();
}
});

it('logs info messages to console.log', () => {
const logSpy = spyOn(console, 'log').mockImplementation(() => {});

logInfo('Server started', { port: 3000 });

expect(logSpy).toHaveBeenCalled();
const arg = logSpy.mock.calls[0][0];
const parsed = JSON.parse(arg);
expect(parsed.level).toBe('info');
expect(parsed.message).toBe('Server started');
expect(parsed.port).toBe(3000);
expect(parsed.timestamp).toBeDefined();

logSpy.mockRestore();
});
});
11 changes: 11 additions & 0 deletions server/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ export function logError(
}),
);
}

export function logInfo(message: string, meta?: Record<string, unknown>): void {
console.log(
JSON.stringify({
level: 'info',
message,
timestamp: new Date().toISOString(),
...meta,
}),
);
}
Loading