Skip to content

Commit

Permalink
Merge pull request #218 from valory-xyz/feat/better-logging
Browse files Browse the repository at this point in the history
Export Agent Logs & Better logging
  • Loading branch information
truemiller authored Jul 6, 2024
2 parents 5d14535 + a31a368 commit 19c6b21
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 1,211 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ leak_report
*.dist
*.build
/electron/bins/

# logs
*.log
27 changes: 27 additions & 0 deletions electron/constants/paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const os = require('os');
const path = require('path');
const fs = require('fs');

const dotOperateDirectory =
process.env.NODE_ENV === 'production'
? path.join(os.homedir(), '.operate')
: '.operate';

// Create operate directory if it doesn't exist
if (!fs.existsSync(dotOperateDirectory)) {
fs.mkdirSync(dotOperateDirectory);
}

const paths = {
dotOperateDirectory,
servicesDir: path.join(dotOperateDirectory, 'services'),
venvDir: path.join(dotOperateDirectory, 'venv'),
tempDir: path.join(dotOperateDirectory, 'temp'),
versionFile: path.join(dotOperateDirectory, 'version.txt'),
cliLogFile: path.join(dotOperateDirectory, 'cli.log'),
electronLogFile: path.join(dotOperateDirectory, 'electron.log'),
nextLogFile: path.join(dotOperateDirectory, 'next.log'),
osPearlTempDir: path.join(os.tmpdir(), 'pearl'),
};

module.exports = { paths };
55 changes: 20 additions & 35 deletions electron/install.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
// Installation helpers.
const fs = require('fs');
const os = require('os');
const path = require('path');
const sudo = require('sudo-prompt');
const process = require('process');
const axios = require('axios');
const { spawnSync } = require('child_process');

const { paths } = require('./constants/paths');

/**
* current version of the pearl release
* - use "" (nothing as a suffix) for latest release candidate, for example "0.1.0rc26"
* - use "alpha" for alpha release, for example "0.1.0rc26-alpha"
*/
const OlasMiddlewareVersion = '0.1.0rc64';
const OperateDirectory = path.join(os.homedir(), '.operate');

// Create operate directory if it doesn't exist
if (!fs.existsSync(OperateDirectory)) {
fs.mkdirSync(OperateDirectory);
}

const paths = {
OperateDirectory,
VenvDir: path.join(OperateDirectory, '.operate', 'venv'),
TempDir: path.join(OperateDirectory, '.operate', 'temp'),
VersionFile: path.join(OperateDirectory, '.operate', 'version.txt'),
LogFile: path.join(OperateDirectory, '.operate', 'logs.txt'),
OperateInstallationLog: path.join(os.homedir(), 'operate.log'),
};

const Env = {
...process.env,
Expand Down Expand Up @@ -152,7 +138,7 @@ async function downloadFile(url, dest) {

async function installTendermintUnix() {
const cwd = process.cwd();
process.chdir(paths.TempDir);
process.chdir(paths.tempDir);

console.log(
appendInstallationLog(
Expand All @@ -164,7 +150,7 @@ async function installTendermintUnix() {
console.log(
appendInstallationLog(`Downloading ${url}, might take a while...`),
);
await downloadFile(url, `${paths.TempDir}/tendermint.tar.gz`);
await downloadFile(url, `${paths.tempDir}/tendermint.tar.gz`);

console.log(appendInstallationLog(`Installing tendermint binary`));
runCmdUnix('tar', ['-xvf', 'tendermint.tar.gz']);
Expand Down Expand Up @@ -226,7 +212,7 @@ function installOperateCli(path) {
}
return new Promise((resolve, _reject) => {
fs.copyFile(
`${OperateDirectory}/venv/bin/operate`,
`${paths.dotOperateDirectory}/venv/bin/operate`,
installPath,
function (error, _stdout, _stderr) {
resolve(!error);
Expand All @@ -247,15 +233,15 @@ function createDirectory(path) {
}

function writeVersion() {
fs.writeFileSync(paths.VersionFile, OlasMiddlewareVersion);
fs.writeFileSync(paths.versionFile, OlasMiddlewareVersion);
}

function versionBumpRequired() {
if (!fs.existsSync(paths.VersionFile)) {
if (!fs.existsSync(paths.versionFile)) {
return true;
}
const olasMiddlewareVersionInFile = fs
.readFileSync(paths.VersionFile)
.readFileSync(paths.versionFile)
.toString();
return olasMiddlewareVersionInFile != OlasMiddlewareVersion;
}
Expand All @@ -279,8 +265,8 @@ function removeInstallationLogFile() {
async function setupDarwin(ipcChannel) {
removeInstallationLogFile();
console.log(appendInstallationLog('Creating required directories'));
await createDirectory(`${OperateDirectory}`);
await createDirectory(`${OperateDirectory}/temp`);
await createDirectory(`${paths.dotOperateDirectory}`);
await createDirectory(`${paths.dotOperateDirectory}/temp`);

console.log(appendInstallationLog('Checking tendermint installation'));
if (!isTendermintInstalledUnix()) {
Expand Down Expand Up @@ -310,19 +296,19 @@ async function setupUbuntu(ipcChannel) {
if (!isPythonInstalledUbuntu()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing Python'));
await installPythonUbuntu(OperateDirectory);
await installPythonUbuntu(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Checking git installation'));
if (!isGitInstalledUbuntu()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing git'));
await installGitUbuntu(OperateDirectory);
await installGitUbuntu(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Creating required directories'));
await createDirectory(`${OperateDirectory}`);
await createDirectory(`${OperateDirectory}/temp`);
await createDirectory(`${paths.dotOperateDirectory}`);
await createDirectory(`${paths.dotOperateDirectory}/temp`);

console.log(appendInstallationLog('Checking tendermint installation'));
if (!isTendermintInstalledUnix()) {
Expand All @@ -331,13 +317,13 @@ async function setupUbuntu(ipcChannel) {
await installTendermintUnix();
}

if (!fs.existsSync(paths.VenvDir)) {
if (!fs.existsSync(paths.venvDir)) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Creating virtual environment'));
createVirtualEnvUnix(paths.VenvDir);
createVirtualEnvUnix(paths.venvDir);

console.log(appendInstallationLog('Installing pearl backend'));
installOperatePackageUnix(OperateDirectory);
installOperatePackageUnix(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Checking if upgrade is required'));
Expand All @@ -347,13 +333,13 @@ async function setupUbuntu(ipcChannel) {
`Upgrading pearl daemon to ${OlasMiddlewareVersion}`,
),
);
reInstallOperatePackageUnix(OperateDirectory);
reInstallOperatePackageUnix(paths.dotOperateDirectory);
writeVersion();
removeLogFile();
}

if (!fs.existsSync(`${OperateDirectory}/venv/bin/operate`)) {
reInstallOperatePackageUnix(OperateDirectory);
if (!fs.existsSync(`${paths.dotOperateDirectory}/venv/bin/operate`)) {
reInstallOperatePackageUnix(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Installing pearl CLI'));
Expand All @@ -364,5 +350,4 @@ module.exports = {
setupDarwin,
setupUbuntu,
Env,
paths,
};
73 changes: 73 additions & 0 deletions electron/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const winston = require('winston');
const { format } = require('logform');
const { paths } = require('./constants/paths');

const { combine, timestamp, printf } = format;

const logFormat = printf(({ level, message, timestamp }) => {
return `${timestamp} ${level}: ${message}`;
});

const customLevels = {
levels: {
error: 0,
warn: 1,
info: 2,
next: 3,
cli: 4,
electron: 5,
},
colors: {
error: 'red',
warn: 'yellow',
info: 'blue',
cli: 'green bold underline',
electron: 'magenta bold underline',
next: 'cyan bold underline',
},
};

// Custom filter for specific levels, otherwise higher levels will include lower levels
const levelFilter = (level) =>
format((info, _opts) => {
return info.level === level ? info : false;
})();

const logger = winston.createLogger({
levels: customLevels.levels,
transports: [
new winston.transports.Console({
level: 'electron', // Set to the highest level so it captures everything.
format: combine(winston.format.colorize(), timestamp(), logFormat),
}),
new winston.transports.File({
filename: 'cli.log',
dirname: paths.dotOperateDirectory,
level: 'cli',
format: combine(levelFilter('cli'), timestamp(), logFormat),
maxFiles: 1,
maxsize: 1024 * 1024 * 10,
}),
new winston.transports.File({
filename: 'electron.log',
dirname: paths.dotOperateDirectory,
level: 'electron',
format: combine(levelFilter('electron'), timestamp(), logFormat),
maxFiles: 1,
maxsize: 1024 * 1024 * 10,
}),
new winston.transports.File({
filename: 'next.log',
dirname: paths.dotOperateDirectory,
level: 'next',
format: combine(levelFilter('next'), timestamp(), logFormat),
maxFiles: 1,
maxsize: 1024 * 1024 * 10,
}),
],
format: combine(timestamp(), logFormat),
});

winston.addColors(customLevels.colors);

module.exports = { logger };
Loading

0 comments on commit 19c6b21

Please sign in to comment.