1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

improved Logger allowing different log level

This commit is contained in:
Pedro Gutiérrez 2019-02-21 12:19:36 +01:00 committed by Pedro Gutiérrez
parent fce88aa528
commit e61e7b8881
7 changed files with 46 additions and 35 deletions

View File

@ -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,
)

View File

@ -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<Contract> {
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)
}

View File

@ -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
}

View File

@ -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<Ocean>}
*/
public static async getInstance(config: Config): Promise<Ocean> {
Logger.setLevel(
typeof config.verbose !== "number"
? (config.verbose ? LogLevel.Log : LogLevel.None)
: <LogLevel>config.verbose
)
if (!Ocean.instance) {
ConfigProvider.setConfig(config)
Ocean.instance = new Ocean()

View File

@ -19,21 +19,16 @@ export default class ServiceAgreement extends OceanBase {
consumer: Account,
): Promise<string> {
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<ServiceAgreement> {
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)

View File

@ -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
}

View File

@ -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)
}
}
}