Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Handle incorrect keys to loggy (#1112)
Browse files Browse the repository at this point in the history
* Handle incorrect keys to loggy

When calling Loggy.success with a non-existing key, the reference would not be found, and the call to path.basename in Loggy._log would fail (since its parameter was null). This commit:

- Catches any exceptions in _log, and displays an error (allowing to continue)
- Adds a testing mode to the logger, which is silent but executes logic, and throws an exception upon issues (only to be used in the CLI and lib tests)
- Fixes incorrect or missing keys in calls to the logger

* Remove failing test
  • Loading branch information
spalladino authored and mergify[bot] committed Jul 17, 2019
1 parent 347cc55 commit 63c1535
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/models/network/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export default class NetworkController {
Loggy.spin(__filename, '_uploadSolidityLib', `upload-solidity-lib${libName}`, `Uploading ${libName} library`);
const libInstance = await this.project.setImplementation(libClass, libName);
this.networkFile.addSolidityLib(libName, libInstance);
Loggy.succeed(`upload-solidity-lob${libName}`, `${libName} library uploaded`);
Loggy.succeed(`upload-solidity-lib${libName}`, `${libName} library uploaded`);
}

// Contract model
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ useTestProjectFile();
doNotInstallStdlib();
ZWeb3.initialize(web3.currentProvider);
setArtifactDefaults();
Loggy.silent(true);
Loggy.silent(false);
Loggy.testing(true);

require('chai')
.use(require('chai-as-promised'))
Expand Down
1 change: 1 addition & 0 deletions packages/lib/src/project/ProxyAdminProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class BaseProxyAdminProject extends BaseSimpleProject {
contract,
contractParams,
);
Loggy.spin(__filename, 'upgradeProxy', `action-proxy-${pAddress}`, `Upgrading instance at ${pAddress}`);
await this.proxyAdmin.upgradeProxy(pAddress, implementationAddress, contract, initMethodName, initArgs);
Loggy.succeed(`action-proxy-${pAddress}`, `Instance at ${pAddress} upgraded`);
return contract.at(pAddress);
Expand Down
3 changes: 2 additions & 1 deletion packages/lib/src/project/SimpleProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export default class SimpleProject extends BaseSimpleProject {
contract,
contractParams,
);
Loggy.spin(__filename, 'upgradeProxy', `action-proxy-${pAddress}`, `Upgrading instance at ${pAddress}`);
const proxy = Proxy.at(pAddress, this.txParams);
await proxy.upgradeTo(implementationAddress, initCallData);
Loggy.succeed(`action-proxy-${implementationAddress}`, `Instance at ${pAddress} upgraded`);
Loggy.succeed(`action-proxy-${pAddress}`, `Instance at ${pAddress} upgraded`);
return contract.at(proxyAddress);
}

Expand Down
33 changes: 21 additions & 12 deletions packages/lib/src/utils/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const Loggy: { [key: string]: any } = {
logs: {},
isSilent: true,
isVerbose: false,
isTesting: false,

silent(value: boolean): void {
this.isSilent = value;
Expand All @@ -54,6 +55,10 @@ export const Loggy: { [key: string]: any } = {
this.isVerbose = value;
},

testing(value: boolean): void {
this.isTesting = value;
},

add(
file: string,
fnName: string,
Expand Down Expand Up @@ -113,18 +118,22 @@ export const Loggy: { [key: string]: any } = {
},

_log(reference: string): void {
if (this.isSilent) return;
const { file, fnName, text, spinnerAction, logLevel, logType } = this.logs[reference];
const color = this._getColorFor(logType);
if (this.isVerbose) {
const location = `${path.basename(file)}#${fnName}`;
const message = `[${new Date().toISOString()}@${location}] <${this._actionToText(spinnerAction)}> ${text}`;
const coloredMessage = color ? chalk.keyword(color)(message) : message;
console.error(coloredMessage);
} else if (logLevel === LogLevel.Normal) {
const options = color ? { text, status: spinnerAction, color } : { text, status: spinnerAction };

!spinners.pick(reference) ? spinners.add(reference, options) : spinners.update(reference, options);
try {
if (this.isSilent) return;
const { file, fnName, text, spinnerAction, logLevel, logType } = this.logs[reference];
const color = this._getColorFor(logType);
if (this.isVerbose || this.isTesting) {
const location = `${path.basename(file)}#${fnName}`;
const message = `[${new Date().toISOString()}@${location}] <${this._actionToText(spinnerAction)}> ${text}`;
const coloredMessage = color ? chalk.keyword(color)(message) : message;
if (!this.isTesting) console.error(coloredMessage);
} else if (logLevel === LogLevel.Normal) {
const options = color ? { text, status: spinnerAction, color } : { text, status: spinnerAction };
!spinners.pick(reference) ? spinners.add(reference, options) : spinners.update(reference, options);
}
} catch (err) {
if (this.isTesting) throw new Error(`Error logging ${reference}: ${err}`);
else console.error(`Error logging ${reference}: ${err}`);
}
},

Expand Down
3 changes: 2 additions & 1 deletion packages/lib/test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import Contracts from '../src/artifacts/Contracts';
import { helpers } from '../src/test';
import { Loggy } from '../src/utils/Logger';

Loggy.silent(true);
Loggy.silent(false);
Loggy.testing(true);
ZWeb3.initialize(web3.currentProvider);
setArtifactDefaults();

Expand Down
31 changes: 16 additions & 15 deletions packages/lib/test/src/utils/Logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,19 @@ import {
LogType,
} from '../../../src/utils/Logger';

describe('Loggger', function() {
afterEach('restore logger', function() {
Loggy.silent(true);
describe('Logger', function() {
beforeEach(function () {
Loggy.testing(false);
});

afterEach('restore logger', function() {
Loggy.testing(true);
Loggy.silent(false);
Loggy.verbose(false);
Loggy.logs = {};
sinon.restore();
});

describe('default attributes', function() {
it('is silent and non-verbose by default', function() {
Loggy.isSilent.should.be.true;
Loggy.isVerbose.should.be.false;
});

it(`has a 'logs' object initialized`, function() {
Loggy.logs.should.be.an('object').that.is.empty;
});
});

describe('methods', function() {
describe('#silent', function() {
it('changes isSilent property value', function() {
Expand All @@ -41,7 +35,14 @@ describe('Loggger', function() {
describe('#verbose', function() {
it('changes isVerbose property value', function() {
Loggy.verbose(true);
Loggy.isSilent.should.be.true;
Loggy.isVerbose.should.be.true;
});
});

describe('#testing', function() {
it('changes isTesting property value', function() {
Loggy.testing(true);
Loggy.isTesting.should.be.true;
});
});

Expand Down

0 comments on commit 63c1535

Please sign in to comment.