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

Export Agent Logs & Better logging #218

Merged
merged 20 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
f2ea544
refactor: Remove unused log file paths in electron/install.js
truemiller Jul 4, 2024
90144f4
Create winston logger module for better logging
truemiller Jul 4, 2024
f64b1dd
chore: Refactor logging in electron/main.js and add winston logger mo…
truemiller Jul 4, 2024
4a6c5dd
Remove unused brew script
truemiller Jul 4, 2024
1c5ab45
Attach winston logger to macUpdater
truemiller Jul 4, 2024
7ce448d
refactor: Remove unused log file paths in electron/main.js
truemiller Jul 4, 2024
06a9513
refactor: Update file paths for log files in electron/main.js and ele…
truemiller Jul 5, 2024
bc9bf56
assign logs to .operate folder
truemiller Jul 5, 2024
34cd493
fix venv, temp and version paths
truemiller Jul 5, 2024
833c5d6
cleanup paths object
truemiller Jul 5, 2024
db78790
move paths to an appropriate constants file
truemiller Jul 5, 2024
2d90e81
update paths for log writes
truemiller Jul 5, 2024
727b50a
better log exports
truemiller Jul 5, 2024
2bfbb3e
Merge branch 'main' into feat/better-logging
truemiller Jul 5, 2024
f7eaa6c
Update electron/logger.js
truemiller Jul 5, 2024
140fee5
refactor: Update file paths for log files in electron/main.js and ele…
truemiller Jul 5, 2024
bb54dd4
Merge branch 'feat/better-logging' of github.com:valory-xyz/olas-oper…
truemiller Jul 5, 2024
b96e931
refactor: Update log file paths and add destination path for sanitiza…
truemiller Jul 5, 2024
d6b2d40
refactor: Update log file paths and add destination path for sanitiza…
truemiller Jul 5, 2024
a31a368
Extend santization regex to support Linux and Mac.
truemiller Jul 5, 2024
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
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
Loading