Skip to content

Commit

Permalink
Merge pull request #69 from ar-io/logger
Browse files Browse the repository at this point in the history
fix(logger): replace winston with bunyan
  • Loading branch information
dtfiedler authored May 1, 2024
2 parents 590ac76 + f07eb2a commit 9cc5efd
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 208 deletions.
5 changes: 4 additions & 1 deletion bundle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { build } from 'esbuild';
import { polyfillNode } from 'esbuild-plugin-polyfill-node';

const bundle = async () => {
console.log('Building web bundle esm.');
console.log('Building minified web bundle file.');
await build({
entryPoints: ['./src/web/index.ts'],
bundle: true,
Expand All @@ -14,11 +14,14 @@ const bundle = async () => {
polyfillNode({
polyfills: {
crypto: true,
process: true,
fs: true,
},
}),
],
tsconfig: './tsconfig.web.json',
outfile: './bundles/web.bundle.min.js',
external: ['dtrace-provider'],
})
.catch((e) => {
console.log(e);
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3'
version: '3.8'

services:
arlocal:
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@
"arbundles": "0.11.0",
"arweave": "1.14.4",
"axios": "1.4.0",
"setimmediate": "^1.0.5",
"bunyan": "^1.8.15",
"warp-arbundles": "^1.0.4",
"warp-contracts": "1.4.39",
"winston": "^3.11.0"
"warp-contracts": "1.4.39"
},
"lint-staged": {
"**/*.{ts,js,mjs,cjs,md,json}": [
Expand Down
12 changes: 5 additions & 7 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,14 @@ export interface ANTWriteContract {
setName({ name }: { name: string }): Promise<WriteInteractionResult>;
}

/* eslint-disable @typescript-eslint/no-explicit-any */
export interface Logger {
setLogLevel: (level: string) => void;
setLogFormat: (logFormat: string) => void;
info: (message: string, ...args: any[]) => void;
warn: (message: string, ...args: any[]) => void;
error: (message: string, ...args: any[]) => void;
debug: (message: string, ...args: any[]) => void;
info: (message: string, ...args: unknown[]) => void;
warn: (message: string, ...args: unknown[]) => void;
error: (message: string, ...args: unknown[]) => void;
debug: (message: string, ...args: unknown[]) => void;
}
/* eslint-enable @typescript-eslint/no-explicit-any */

export interface HTTPClient {
get<I, K>({
endpoint,
Expand Down
2 changes: 1 addition & 1 deletion src/common/arweave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Arweave from 'arweave';
import Arweave from 'arweave/node/index.js';

export const defaultArweave = Arweave.init({
host: 'ar-io.dev',
Expand Down
70 changes: 29 additions & 41 deletions src/common/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,56 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// small polyfill for winston
import 'setimmediate';
import winston, { createLogger, format, transports } from 'winston';
import bunyan from 'bunyan';

import { Logger } from '../types.js';
import { version } from '../version.js';

export class DefaultLogger implements Logger {
private logger: winston.Logger;
private logger: bunyan.Logger;
private silent = false;
constructor({
level = 'info',
logFormat = 'simple',
}: {
level?: 'info' | 'debug' | 'error' | 'none' | undefined;
logFormat?: 'simple' | 'json' | undefined;
level?: 'info' | 'debug' | 'error' | 'warn' | 'none';
} = {}) {
this.logger = createLogger({
if (level === 'none') {
this.silent = true;
return;
}
this.logger = bunyan.createLogger({
level,
defaultMeta: { client: 'ar-io-sdk', version },
silent: level === 'none',
format: getLogFormat(logFormat),
transports: [new transports.Console()],
name: 'ar-io-sdk',
version,
serializers: bunyan.stdSerializers,
});
}

/* eslint-disable @typescript-eslint/no-explicit-any */
info(message: string, ...args: any[]) {
this.logger.info(message, ...args);
info(message: string, ...args: unknown[]) {
if (this.silent) return;
this.logger.info(...args, message);
}

warn(message: string, ...args: any[]) {
this.logger.warn(message, ...args);
warn(message: string, ...args: unknown[]) {
if (this.silent) return;
this.logger.warn(...args, message);
}

error(message: string, ...args: any[]) {
this.logger.error(message, ...args);
error(message: string, ...args: unknown[]) {
if (this.silent) return;
this.logger.error(...args, message);
}

debug(message: string, ...args: any[]) {
this.logger.debug(message, ...args);
debug(message: string, ...args: unknown[]) {
if (this.silent) return;
this.logger.debug(...args, message);
}

setLogLevel(level: string) {
this.logger.level = level;
if (level === 'none') {
this.silent = true;
return;
}
this.logger.level(level);
}

setLogFormat(logFormat: string) {
this.logger.format = getLogFormat(logFormat);
}
/* eslint-enable @typescript-eslint/no-explicit-any */
}

function getLogFormat(logFormat: string) {
return format.combine(
format((info) => {
if (info.stack && info.level !== 'error') {
delete info.stack;
}
return info;
})(),
format.errors({ stack: true }), // Ensure errors show a stack trace
format.timestamp(),
logFormat === 'json' ? format.json() : format.simple(),
);
}
1 change: 0 additions & 1 deletion src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ export { ArweaveSigner, ArconnectSigner } from 'arbundles';
export * from '../types.js';
export * from '../common/index.js';
export * from '../constants.js';
export * from '../utils/index.js';
1 change: 0 additions & 1 deletion src/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ export { ArweaveSigner, ArconnectSigner } from 'arbundles';
export * from '../types.js';
export * from '../common/index.js';
export * from '../constants.js';
export * from '../utils/index.js';
Loading

0 comments on commit 9cc5efd

Please sign in to comment.