Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose logs #385

Merged
merged 1 commit into from
Dec 28, 2024
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
2 changes: 2 additions & 0 deletions packages/express/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { prepareNote55 } from '../../vue/src/db/prepareNote55';
import ENV from './utils/env';
import morgan from 'morgan';
import logger from './logger';
import logsRouter from './routes/logs';

process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at:', promise, 'reason:', reason);
Expand Down Expand Up @@ -49,6 +50,7 @@ app.use(
})
);
app.use(morgan('combined', { stream: { write: (message) => logger.info(message.trim()) } }));
app.use('/logs', logsRouter);

export interface VueClientRequest extends express.Request {
body: ServerRequest;
Expand Down
70 changes: 70 additions & 0 deletions packages/express/src/routes/logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import express from 'express';
import { promises as fs } from 'fs';
import path from 'path';
import { requestIsAdminAuthenticated } from '../couchdb/authentication';
import logger from '../logger';
import process from 'process';

const router = express.Router();

// Get list of available log files
router.get('/', async (req, res) => {
const auth = await requestIsAdminAuthenticated(req);
if (!auth) {
res.status(401).json({ error: 'Unauthorized' });
return;
}

try {
const logsDir = path.join(process.cwd(), 'logs');
const files = await fs.readdir(logsDir);
res.json(files);
} catch (error) {
logger.error('Error reading logs directory:', error);
res.status(500).json({ error: 'Failed to retrieve log files' });
}
});

// Get contents of specific log file
router.get('/:filename', async (req, res) => {
const auth = await requestIsAdminAuthenticated(req);
if (!auth) {
res.status(401).json({ error: 'Unauthorized' });
return;
}

try {
const filename = req.params.filename;
// Sanitize filename to prevent directory traversal
if (filename.includes('..') || !filename.match(/^[a-zA-Z0-9-_.]+$/)) {
res.status(400).json({ error: 'Invalid filename' });
return;
}

const logPath = path.join(process.cwd(), 'logs', filename);
const content = (await fs.readFile(logPath, 'utf-8')).toString();

// If the client requests JSON format
if (req.headers.accept === 'application/json') {
const lines = content
.split('\n')
.filter((line) => line.trim())
.map((line) => {
try {
return JSON.parse(line);
} catch {
return line;
}
});
res.json(lines);
} else {
// Return plain text by default
res.type('text/plain').send(content);
}
} catch (error) {
logger.error(`Error reading log file ${req.params.filename}:`, error);
res.status(500).json({ error: 'Failed to retrieve log file' });
}
});

export default router;
Loading