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

Implement logging for LibSourcify #1082

Merged
merged 7 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
5 changes: 5 additions & 0 deletions packages/lib-sourcify/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { setLogger, setLevel, ILogger } from './lib/logger';

export * from './lib/validation';
export * from './lib/verification';
export * from './lib/CheckedContract';
export * from './lib/types';
export * from './lib/solidityCompiler';
export const setLibSourcifyLogger = setLogger;
export const setLibSourcifyLoggerLevel = setLevel;
export type ILibSourcifyLogger = ILogger;
14 changes: 8 additions & 6 deletions packages/lib-sourcify/src/lib/CheckedContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { storeByHash } from './validation';
import { decode as decodeBytecode } from '@ethereum-sourcify/bytecode-utils';
import { ipfsHash } from './hashFunctions/ipfsHash';
import { swarmBzzr0Hash, swarmBzzr1Hash } from './hashFunctions/swarmHash';
import { logError, logInfo, logWarn } from './logger';
// I used this file as an example of how to use it

// TODO: find a better place for these constants. Reminder: this sould work also in the browser
const IPFS_PREFIX = 'dweb:/ipfs/';
Expand Down Expand Up @@ -338,29 +340,29 @@ export async function performFetch(
hash?: string,
fileName?: string
): Promise<string | null> {
console.log(`Fetching the file ${fileName} from ${url}...`);
logInfo(`Fetching the file ${fileName} from ${url}...`);
marcocastignoli marked this conversation as resolved.
Show resolved Hide resolved
const res = await fetchWithTimeout(url, { timeout: FETCH_TIMEOUT }).catch(
(err) => {
if (err.type === 'aborted')
console.log(
logWarn(
`Fetching the file ${fileName} from ${url} timed out. Timeout: ${FETCH_TIMEOUT}ms`
);
else console.log(err);
else logError(err);
}
);

if (res) {
if (res.status === 200) {
const content = await res.text();
if (hash && Web3.utils.keccak256(content) !== hash) {
console.log("The calculated and the provided hash don't match.");
logError("The calculated and the provided hash don't match.");
return null;
}

console.log(`Successfully fetched the file ${fileName}`);
logInfo(`Successfully fetched the file ${fileName}`);
return content;
} else {
console.log(
logError(
`Fetching the file ${fileName} failed with status: ${res?.status}`
);
return null;
Expand Down
58 changes: 58 additions & 0 deletions packages/lib-sourcify/src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export interface ILogger {
logLevel: number;
log: (level: number, message: string) => void;
setLevel: (level: number) => void;
}

// Default logger behavior
export const DefaultLogger: ILogger = {
logLevel: 1,
marcocastignoli marked this conversation as resolved.
Show resolved Hide resolved
setLevel(level: number) {
this.logLevel = level;
},
log(level, msg) {
if (level <= this.logLevel) {
switch (level) {
case 1:
console.error(msg);
break;
case 2:
console.warn(msg);
break;
case 3:
console.info(msg);
break;
case 4:
console.debug(msg);
break;
}
}
},
};

// Logger variable that will be used throughout the application
let AppLogger: ILogger = DefaultLogger;

export function setLogger(logger: ILogger) {
AppLogger = logger;
}

export function setLevel(level: number) {
AppLogger.setLevel(level);
}

export function logError(message: string) {
AppLogger.log(1, message);
}

export function logWarn(message: string) {
AppLogger.log(2, message);
}

export function logInfo(message: string) {
AppLogger.log(3, message);
}

export function logDebug(message: string) {
AppLogger.log(3, message);
}
28 changes: 28 additions & 0 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,39 @@ import { resolveRefs } from "json-refs";
import { initDeprecatedRoutes } from "./deprecated.routes";
import { getAddress, isAddress } from "ethers/lib/utils";
import { logger } from "../common/loggerLoki";
import { setLibSourcifyLogger } from "@ethereum-sourcify/lib-sourcify";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fileUpload = require("express-fileupload");

const MemoryStore = createMemoryStore(session);

// here we override the standard LibSourcify's Logger with a custom one
setLibSourcifyLogger({
// No need to set again the logger level because it's set here
logLevel: 4,
marcocastignoli marked this conversation as resolved.
Show resolved Hide resolved
setLevel(level: number) {
this.logLevel = level;
},
log(level, msg) {
if (level <= this.logLevel) {
switch (level) {
case 1:
logger.error(msg);
break;
case 2:
logger.warn(msg);
break;
case 3:
logger.info(msg);
break;
case 4:
logger.debug(msg);
break;
}
}
},
});

marcocastignoli marked this conversation as resolved.
Show resolved Hide resolved
export class Server {
app: express.Application;
repository = config.repository.path;
Expand Down