log-vista
Version:
LogVista Agent - Lightweight system monitoring and log collection for any project/language
278 lines (242 loc) ⢠10.3 kB
JavaScript
const { Command } = require('commander');
const path = require('path');
const fs = require('fs');
const os = require('os');
const program = new Command();
// Get version from package.json
const packageJson = require('../package.json');
program
.name('logvista')
.description('LogVista Agent - Universal monitoring for any language/project')
.version(packageJson.version);
// Global config directory
const getConfigDir = () => {
const platform = os.platform();
if (platform === 'win32') {
return path.join(os.homedir(), 'AppData', 'Roaming', 'LogVista');
} else {
return path.join(os.homedir(), '.logvista');
}
};
const getConfigPath = () => path.join(getConfigDir(), 'agent.config.json');
// Init command - Initialize LogVista in current directory
program
.command('init')
.description('Initialize LogVista monitoring in current directory')
.option('-u, --url <url>', 'Central system URL', 'http://localhost:3001')
.option('-t, --token <token>', 'Agent authentication token')
.option('-n, --name <name>', 'Project name', path.basename(process.cwd()))
.action(async (options) => {
const currentDir = process.cwd();
const projectName = options.name;
console.log(`š Initializing LogVista monitoring for: ${projectName}`);
console.log(`š Project directory: ${currentDir}`);
// Create local config
const localConfig = {
central_system: {
url: options.url,
token: options.token || 'your-agent-token-here'
},
collection: {
interval: 30,
batch_size: 100,
retry_attempts: 3,
retry_delay: 5000
},
projects: [
{
project_name: projectName,
pwd_path: currentDir,
custom_log_paths: [
// Common log patterns for different languages
path.join(currentDir, 'logs', '*.log'),
path.join(currentDir, 'log', '*.log'),
path.join(currentDir, 'storage', 'logs', '*.log'), // Laravel PHP
path.join(currentDir, 'var', 'log', '*.log'), // Symfony PHP
path.join(currentDir, 'Logs', '*.log'), // .NET
path.join(currentDir, 'bin', 'Debug', 'netcoreapp*', '*.log'), // .NET Core
path.join(currentDir, '*.log'), // Go, Python, Node.js in root
path.join(currentDir, 'tmp', '*.log'), // Rails Ruby
path.join(currentDir, 'log', '**', '*.log') // Nested logs
].filter(logPath => {
// Check if any of these paths exist
const globPattern = logPath.replace('*', '');
const dir = path.dirname(globPattern);
return fs.existsSync(dir);
}),
enabled: true
}
]
};
// Create .logvista directory if it doesn't exist
const logvistaDir = path.join(currentDir, '.logvista');
if (!fs.existsSync(logvistaDir)) {
fs.mkdirSync(logvistaDir);
}
// Write local config
const localConfigPath = path.join(logvistaDir, 'config.json');
fs.writeFileSync(localConfigPath, JSON.stringify(localConfig, null, 2));
// Create .gitignore entry
const gitignorePath = path.join(currentDir, '.gitignore');
const gitignoreEntry = '\n# LogVista\n.logvista/\n';
if (fs.existsSync(gitignorePath)) {
const gitignoreContent = fs.readFileSync(gitignorePath, 'utf8');
if (!gitignoreContent.includes('.logvista/')) {
fs.appendFileSync(gitignorePath, gitignoreEntry);
console.log('ā
Added .logvista/ to .gitignore');
}
} else {
fs.writeFileSync(gitignorePath, gitignoreEntry);
console.log('ā
Created .gitignore with LogVista entries');
}
console.log('ā
LogVista configuration created successfully!');
console.log(`š Config file: ${localConfigPath}`);
console.log('\nš Next steps:');
console.log('1. Update the agent token in the config file');
console.log('2. Run "logvista start" to begin monitoring');
console.log('3. Check "logvista status" to verify everything is working');
});
// Start command - Start monitoring
program
.command('start')
.description('Start LogVista monitoring')
.option('-d, --daemon', 'Run as daemon process')
.option('-c, --config <path>', 'Custom config file path')
.action((options) => {
const currentDir = process.cwd();
const configPath = options.config || path.join(currentDir, '.logvista', 'config.json');
if (!fs.existsSync(configPath)) {
console.error('ā No LogVista configuration found!');
console.log('Run "logvista init" first to initialize monitoring.');
process.exit(1);
}
console.log('š Starting LogVista Agent...');
console.log(`š Using config: ${configPath}`);
// Set environment variable for the agent to use this config
process.env.LOGVISTA_CONFIG_PATH = configPath;
// Start the agent
require('../src/index.js');
});
// Status command - Check agent status
program
.command('status')
.description('Check LogVista agent status')
.action(() => {
const currentDir = process.cwd();
const configPath = path.join(currentDir, '.logvista', 'config.json');
if (!fs.existsSync(configPath)) {
console.log('ā LogVista not initialized in this directory');
console.log('Run "logvista init" to get started');
return;
}
console.log('š LogVista Status:');
console.log(`š Project: ${path.basename(currentDir)}`);
console.log(`š Config: ${configPath}`);
try {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
console.log(`š Central System: ${config.central_system.url}`);
console.log(`š Token: ${config.central_system.token ? 'ā
Configured' : 'ā Missing'}`);
console.log(`š Projects: ${config.projects.length}`);
config.projects.forEach((project, index) => {
console.log(` ${index + 1}. ${project.project_name} (${project.enabled ? 'enabled' : 'disabled'})`);
console.log(` š Path: ${project.pwd_path}`);
console.log(` š Log paths: ${project.custom_log_paths.length} configured`);
});
} catch (error) {
console.error('ā Error reading config:', error.message);
}
});
// Stop command - Stop monitoring
program
.command('stop')
.description('Stop LogVista monitoring')
.action(() => {
console.log('š Stopping LogVista Agent...');
// In a real implementation, this would send a signal to stop the daemon
// For now, we'll just show the message
console.log('ā
LogVista Agent stopped');
});
// Config command - Manage configuration
program
.command('config')
.description('Manage LogVista configuration')
.option('--show', 'Show current configuration')
.option('--edit', 'Edit configuration file')
.option('--set <key=value>', 'Set configuration value')
.action((options) => {
const currentDir = process.cwd();
const configPath = path.join(currentDir, '.logvista', 'config.json');
if (!fs.existsSync(configPath)) {
console.error('ā No LogVista configuration found!');
console.log('Run "logvista init" first to initialize monitoring.');
return;
}
if (options.show) {
console.log('š Current Configuration:');
const config = fs.readFileSync(configPath, 'utf8');
console.log(config);
} else if (options.edit) {
console.log(`š Opening config file: ${configPath}`);
console.log('Edit the file and save when done.');
// In a real implementation, this could open the default editor
} else {
console.log('Use --show to view config or --edit to modify it');
}
});
// Global install/setup
program
.command('install')
.description('Install LogVista globally (admin/sudo required)')
.action(() => {
console.log('š§ Installing LogVista globally...');
console.log('This command sets up LogVista for system-wide use.');
const configDir = getConfigDir();
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
console.log(`ā
Created config directory: ${configDir}`);
}
// Create a default global config if it doesn't exist
const globalConfigPath = getConfigPath();
if (!fs.existsSync(globalConfigPath)) {
const defaultConfig = {
central_system: {
url: 'http://localhost:3001',
token: 'your-global-agent-token-here'
},
collection: {
interval: 30,
batch_size: 100,
retry_attempts: 3,
retry_delay: 5000
},
projects: []
};
fs.writeFileSync(globalConfigPath, JSON.stringify(defaultConfig, null, 2));
console.log(`ā
Created global config: ${globalConfigPath}`);
}
console.log('ā
LogVista installed successfully!');
console.log('\nš Next steps:');
console.log('1. Navigate to any project directory');
console.log('2. Run "logvista init" to set up monitoring');
console.log('3. Run "logvista start" to begin monitoring');
});
// Help examples
program.on('--help', () => {
console.log('');
console.log('Examples:');
console.log(' $ logvista init # Initialize in current directory');
console.log(' $ logvista init --name "MyApp" # Initialize with custom name');
console.log(' $ logvista start # Start monitoring');
console.log(' $ logvista status # Check status');
console.log(' $ logvista config --show # Show configuration');
console.log('');
console.log('Supported Languages/Frameworks:');
console.log(' ⢠Node.js, Python, Go, Rust');
console.log(' ⢠PHP (Laravel, Symfony, etc.)');
console.log(' ⢠.NET Core/.NET Framework');
console.log(' ⢠Java (Spring Boot, etc.)');
console.log(' ⢠Ruby (Rails, etc.)');
console.log(' ⢠Any application that writes to log files');
});
program.parse();