From e61e7b8881efb0310b6bcfc4f484d47e857f440d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Thu, 21 Feb 2019 12:19:36 +0100 Subject: [PATCH] improved Logger allowing different log level --- src/examples/InitializeAgreement.ts | 10 +++---- src/keeper/ContractHandler.ts | 10 ++----- src/models/Config.ts | 4 ++- src/ocean/Ocean.ts | 8 ++++- .../ServiceAgreements/ServiceAgreement.ts | 15 +++------- .../ServiceAgreementTemplate.ts | 4 +-- src/utils/Logger.ts | 30 ++++++++++++++----- 7 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/examples/InitializeAgreement.ts b/src/examples/InitializeAgreement.ts index 3073458..7de5ff6 100644 --- a/src/examples/InitializeAgreement.ts +++ b/src/examples/InitializeAgreement.ts @@ -53,13 +53,13 @@ async function exec() { await consumer.requestTokens(100) - const serviceAgreementSignatureResult: any = await ocean + const serviceAgreementSignatureResult = await ocean .signServiceAgreement( ddo.id, accessService.serviceDefinitionId, consumer) - Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId) - Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature) + Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.agreementId) + Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.signature) const service = ddo.findServiceByType("Access") @@ -68,8 +68,8 @@ async function exec() { .initializeServiceAgreement( ddo.id, service.serviceDefinitionId, - serviceAgreementSignatureResult.serviceAgreementId, - serviceAgreementSignatureResult.serviceAgreementSignature, + serviceAgreementSignatureResult.agreementId, + serviceAgreementSignatureResult.signature, (files) => Logger.log(`Got files, first files length in bytes: ${files[0].length}`), consumer, ) diff --git a/src/keeper/ContractHandler.ts b/src/keeper/ContractHandler.ts index d83f93a..4e012c2 100644 --- a/src/keeper/ContractHandler.ts +++ b/src/keeper/ContractHandler.ts @@ -1,5 +1,4 @@ import Contract from "web3-eth-contract" -import ConfigProvider from "../ConfigProvider" import Logger from "../utils/Logger" import Keeper from "./Keeper" import Web3Provider from "./Web3Provider" @@ -28,9 +27,7 @@ export default class ContractHandler { private static async load(what: string, where: string): Promise { const web3 = Web3Provider.getWeb3() - if (ConfigProvider.getConfig().verbose) { - Logger.log("Loading", what, "from", where) - } + Logger.debug("Loading", what, "from", where) const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}.json`) // Logger.log('Loaded artifact', artifact) const code = await web3.eth.getCode(artifact.address) @@ -39,9 +36,8 @@ export default class ContractHandler { throw new Error(`No code deployed at address ${artifact.address}, sorry.`) } const contract = new web3.eth.Contract(artifact.abi, artifact.address) - if (ConfigProvider.getConfig().verbose) { - Logger.log("Getting instance of", what, "from", where, "at address", artifact.address) - } + + Logger.debug("Getting instance of", what, "from", where, "at address", artifact.address) ContractHandler.contracts.set(what, contract) return ContractHandler.contracts.get(what) } diff --git a/src/models/Config.ts b/src/models/Config.ts index 59dfe3c..a61ac34 100644 --- a/src/models/Config.ts +++ b/src/models/Config.ts @@ -1,3 +1,5 @@ +import { LogLevel } from "../utils/Logger" + export default class Config { /* Aquarius Config */ // the url to the aquarius @@ -26,5 +28,5 @@ export default class Config { public threshold: number /* Squid config */ - public verbose: boolean = false + public verbose: boolean | LogLevel } diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 9925f7a..de55fa8 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -16,7 +16,7 @@ import { Service } from "../ddo/Service" import ContractEvent from "../keeper/Event" import Config from "../models/Config" import SecretStoreProvider from "../secretstore/SecretStoreProvider" -import Logger from "../utils/Logger" +import Logger, { LogLevel } from "../utils/Logger" import Account from "./Account" import DID from "./DID" import ServiceAgreement from "./ServiceAgreements/ServiceAgreement" @@ -34,6 +34,12 @@ export default class Ocean { * @return {Promise} */ public static async getInstance(config: Config): Promise { + Logger.setLevel( + typeof config.verbose !== "number" + ? (config.verbose ? LogLevel.Log : LogLevel.None) + : config.verbose + ) + if (!Ocean.instance) { ConfigProvider.setConfig(config) Ocean.instance = new Ocean() diff --git a/src/ocean/ServiceAgreements/ServiceAgreement.ts b/src/ocean/ServiceAgreements/ServiceAgreement.ts index ce3c59a..0a43e14 100644 --- a/src/ocean/ServiceAgreements/ServiceAgreement.ts +++ b/src/ocean/ServiceAgreements/ServiceAgreement.ts @@ -19,21 +19,16 @@ export default class ServiceAgreement extends OceanBase { consumer: Account, ): Promise { - if (ConfigProvider.getConfig().verbose) { - Logger.log("Signing SA with serviceAgreementId", serviceAgreementId) - } + Logger.log("Signing SA with serviceAgreementId", serviceAgreementId) const service = ddo.findServiceById<"Access">(serviceDefinitionId) const values: ValuePair[][] = ServiceAgreement.getValuesFromService(service, serviceAgreementId) const valueHashes: string[] = ServiceAgreement.createValueHashes(values) const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromService(service) - const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(service, serviceAgreementId, - valueHashes, timeoutValues, consumer) + const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(service, serviceAgreementId, valueHashes, timeoutValues, consumer) - if (ConfigProvider.getConfig().verbose) { - Logger.log("SA hash signature:", serviceAgreementHashSignature) - } + Logger.log("SA hash signature:", serviceAgreementHashSignature) return serviceAgreementHashSignature } @@ -48,9 +43,7 @@ export default class ServiceAgreement extends OceanBase { publisher: Account, ): Promise { - if (ConfigProvider.getConfig().verbose) { - Logger.log("Executing SA with serviceAgreementId", serviceAgreementId) - } + Logger.log("Executing SA with serviceAgreementId", serviceAgreementId) const service = ddo.findServiceById<"Access">(serviceDefinitionId) const values: ValuePair[][] = ServiceAgreement.getValuesFromService(service, serviceAgreementId) diff --git a/src/ocean/ServiceAgreements/ServiceAgreementTemplate.ts b/src/ocean/ServiceAgreements/ServiceAgreementTemplate.ts index a7a697c..b8efa58 100644 --- a/src/ocean/ServiceAgreements/ServiceAgreementTemplate.ts +++ b/src/ocean/ServiceAgreements/ServiceAgreementTemplate.ts @@ -172,9 +172,7 @@ export default class ServiceAgreementTemplate extends OceanBase { } as Condition }) - if (ConfigProvider.getConfig().verbose) { - Logger.log("Conditions", JSON.stringify(conditions, null, 2)) - } + Logger.debug("Conditions", JSON.stringify(conditions, null, 2)) return conditions } diff --git a/src/utils/Logger.ts b/src/utils/Logger.ts index d90b70c..1c78d4b 100644 --- a/src/utils/Logger.ts +++ b/src/utils/Logger.ts @@ -1,21 +1,37 @@ +export enum LogLevel { + None = -1, + Error = 0, + Warn = 1, + Log = 2, + Verbose = 3, +} + export default class Logger { - public static log(...args: any[]) { - Logger.dispatch("log", ...args) + private static logLevel: LogLevel = LogLevel.Verbose + + public static setLevel(level: LogLevel) { + this.logLevel = level } public static debug(...args: any[]) { - Logger.dispatch("debug", ...args) + Logger.dispatch("debug", LogLevel.Verbose, ...args) + } + + public static log(...args: any[]) { + Logger.dispatch("log", LogLevel.Log, ...args) } public static warn(...args: any[]) { - Logger.dispatch("warn", ...args) + Logger.dispatch("warn", LogLevel.Warn, ...args) } public static error(...args: any[]) { - Logger.dispatch("error", ...args) + Logger.dispatch("error", LogLevel.Error, ...args) } - private static dispatch(verb: string, ...args: any[]) { - console[verb](...args) + private static dispatch(verb: string, level: LogLevel, ...args: any[]) { + if (this.logLevel >= level) { + console[verb](...args) + } } }