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:
parent
fce88aa528
commit
e61e7b8881
@ -53,13 +53,13 @@ async function exec() {
|
|||||||
|
|
||||||
await consumer.requestTokens(100)
|
await consumer.requestTokens(100)
|
||||||
|
|
||||||
const serviceAgreementSignatureResult: any = await ocean
|
const serviceAgreementSignatureResult = await ocean
|
||||||
.signServiceAgreement(
|
.signServiceAgreement(
|
||||||
ddo.id,
|
ddo.id,
|
||||||
accessService.serviceDefinitionId,
|
accessService.serviceDefinitionId,
|
||||||
consumer)
|
consumer)
|
||||||
Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId)
|
Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.agreementId)
|
||||||
Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature)
|
Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.signature)
|
||||||
|
|
||||||
const service = ddo.findServiceByType("Access")
|
const service = ddo.findServiceByType("Access")
|
||||||
|
|
||||||
@ -68,8 +68,8 @@ async function exec() {
|
|||||||
.initializeServiceAgreement(
|
.initializeServiceAgreement(
|
||||||
ddo.id,
|
ddo.id,
|
||||||
service.serviceDefinitionId,
|
service.serviceDefinitionId,
|
||||||
serviceAgreementSignatureResult.serviceAgreementId,
|
serviceAgreementSignatureResult.agreementId,
|
||||||
serviceAgreementSignatureResult.serviceAgreementSignature,
|
serviceAgreementSignatureResult.signature,
|
||||||
(files) => Logger.log(`Got files, first files length in bytes: ${files[0].length}`),
|
(files) => Logger.log(`Got files, first files length in bytes: ${files[0].length}`),
|
||||||
consumer,
|
consumer,
|
||||||
)
|
)
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import Contract from "web3-eth-contract"
|
import Contract from "web3-eth-contract"
|
||||||
import ConfigProvider from "../ConfigProvider"
|
|
||||||
import Logger from "../utils/Logger"
|
import Logger from "../utils/Logger"
|
||||||
import Keeper from "./Keeper"
|
import Keeper from "./Keeper"
|
||||||
import Web3Provider from "./Web3Provider"
|
import Web3Provider from "./Web3Provider"
|
||||||
@ -28,9 +27,7 @@ export default class ContractHandler {
|
|||||||
|
|
||||||
private static async load(what: string, where: string): Promise<Contract> {
|
private static async load(what: string, where: string): Promise<Contract> {
|
||||||
const web3 = Web3Provider.getWeb3()
|
const web3 = Web3Provider.getWeb3()
|
||||||
if (ConfigProvider.getConfig().verbose) {
|
Logger.debug("Loading", what, "from", where)
|
||||||
Logger.log("Loading", what, "from", where)
|
|
||||||
}
|
|
||||||
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}.json`)
|
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}.json`)
|
||||||
// Logger.log('Loaded artifact', artifact)
|
// Logger.log('Loaded artifact', artifact)
|
||||||
const code = await web3.eth.getCode(artifact.address)
|
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.`)
|
throw new Error(`No code deployed at address ${artifact.address}, sorry.`)
|
||||||
}
|
}
|
||||||
const contract = new web3.eth.Contract(artifact.abi, artifact.address)
|
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)
|
ContractHandler.contracts.set(what, contract)
|
||||||
return ContractHandler.contracts.get(what)
|
return ContractHandler.contracts.get(what)
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import { LogLevel } from "../utils/Logger"
|
||||||
|
|
||||||
export default class Config {
|
export default class Config {
|
||||||
/* Aquarius Config */
|
/* Aquarius Config */
|
||||||
// the url to the aquarius
|
// the url to the aquarius
|
||||||
@ -26,5 +28,5 @@ export default class Config {
|
|||||||
public threshold: number
|
public threshold: number
|
||||||
|
|
||||||
/* Squid config */
|
/* Squid config */
|
||||||
public verbose: boolean = false
|
public verbose: boolean | LogLevel
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ import { Service } from "../ddo/Service"
|
|||||||
import ContractEvent from "../keeper/Event"
|
import ContractEvent from "../keeper/Event"
|
||||||
import Config from "../models/Config"
|
import Config from "../models/Config"
|
||||||
import SecretStoreProvider from "../secretstore/SecretStoreProvider"
|
import SecretStoreProvider from "../secretstore/SecretStoreProvider"
|
||||||
import Logger from "../utils/Logger"
|
import Logger, { LogLevel } from "../utils/Logger"
|
||||||
import Account from "./Account"
|
import Account from "./Account"
|
||||||
import DID from "./DID"
|
import DID from "./DID"
|
||||||
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
|
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
|
||||||
@ -34,6 +34,12 @@ export default class Ocean {
|
|||||||
* @return {Promise<Ocean>}
|
* @return {Promise<Ocean>}
|
||||||
*/
|
*/
|
||||||
public static async getInstance(config: Config): 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) {
|
if (!Ocean.instance) {
|
||||||
ConfigProvider.setConfig(config)
|
ConfigProvider.setConfig(config)
|
||||||
Ocean.instance = new Ocean()
|
Ocean.instance = new Ocean()
|
||||||
|
@ -19,21 +19,16 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
consumer: Account,
|
consumer: Account,
|
||||||
): Promise<string> {
|
): 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 service = ddo.findServiceById<"Access">(serviceDefinitionId)
|
||||||
const values: ValuePair[][] = ServiceAgreement.getValuesFromService(service, serviceAgreementId)
|
const values: ValuePair[][] = ServiceAgreement.getValuesFromService(service, serviceAgreementId)
|
||||||
const valueHashes: string[] = ServiceAgreement.createValueHashes(values)
|
const valueHashes: string[] = ServiceAgreement.createValueHashes(values)
|
||||||
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromService(service)
|
const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromService(service)
|
||||||
|
|
||||||
const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(service, serviceAgreementId,
|
const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(service, serviceAgreementId, valueHashes, timeoutValues, consumer)
|
||||||
valueHashes, timeoutValues, consumer)
|
|
||||||
|
|
||||||
if (ConfigProvider.getConfig().verbose) {
|
|
||||||
Logger.log("SA hash signature:", serviceAgreementHashSignature)
|
Logger.log("SA hash signature:", serviceAgreementHashSignature)
|
||||||
}
|
|
||||||
|
|
||||||
return serviceAgreementHashSignature
|
return serviceAgreementHashSignature
|
||||||
}
|
}
|
||||||
@ -48,9 +43,7 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
publisher: Account,
|
publisher: Account,
|
||||||
): Promise<ServiceAgreement> {
|
): 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 service = ddo.findServiceById<"Access">(serviceDefinitionId)
|
||||||
const values: ValuePair[][] = ServiceAgreement.getValuesFromService(service, serviceAgreementId)
|
const values: ValuePair[][] = ServiceAgreement.getValuesFromService(service, serviceAgreementId)
|
||||||
|
@ -172,9 +172,7 @@ export default class ServiceAgreementTemplate extends OceanBase {
|
|||||||
} as Condition
|
} as Condition
|
||||||
})
|
})
|
||||||
|
|
||||||
if (ConfigProvider.getConfig().verbose) {
|
Logger.debug("Conditions", JSON.stringify(conditions, null, 2))
|
||||||
Logger.log("Conditions", JSON.stringify(conditions, null, 2))
|
|
||||||
}
|
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,37 @@
|
|||||||
|
export enum LogLevel {
|
||||||
|
None = -1,
|
||||||
|
Error = 0,
|
||||||
|
Warn = 1,
|
||||||
|
Log = 2,
|
||||||
|
Verbose = 3,
|
||||||
|
}
|
||||||
|
|
||||||
export default class Logger {
|
export default class Logger {
|
||||||
public static log(...args: any[]) {
|
private static logLevel: LogLevel = LogLevel.Verbose
|
||||||
Logger.dispatch("log", ...args)
|
|
||||||
|
public static setLevel(level: LogLevel) {
|
||||||
|
this.logLevel = level
|
||||||
}
|
}
|
||||||
|
|
||||||
public static debug(...args: any[]) {
|
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[]) {
|
public static warn(...args: any[]) {
|
||||||
Logger.dispatch("warn", ...args)
|
Logger.dispatch("warn", LogLevel.Warn, ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static error(...args: any[]) {
|
public static error(...args: any[]) {
|
||||||
Logger.dispatch("error", ...args)
|
Logger.dispatch("error", LogLevel.Error, ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
private static dispatch(verb: string, ...args: any[]) {
|
private static dispatch(verb: string, level: LogLevel, ...args: any[]) {
|
||||||
|
if (this.logLevel >= level) {
|
||||||
console[verb](...args)
|
console[verb](...args)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user