From 8d2ff2a8f55a9da2ab39bfe345a85dd809a24544 Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Mon, 27 Jul 2020 13:49:03 +0200 Subject: [PATCH] updating logging behaviour --- package.json | 2 +- src/delete.js | 6 +++--- src/index.js | 35 +++++++++++++++++++---------------- src/list.js | 6 +++--- src/load.js | 10 ++++++---- src/move.js | 16 ++++++++++------ src/save.js | 6 +++--- 7 files changed, 45 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 1156fd0..c474b98 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@swrlab/node-storage-wrapper", - "version": "0.0.4", + "version": "0.0.5", "description": "Wrapping AWS S3, GCP GCS, file storage", "main": "./src/index.js", "engines": { diff --git a/src/delete.js b/src/delete.js index 1bb5a4e..85c8c09 100644 --- a/src/delete.js +++ b/src/delete.js @@ -23,7 +23,7 @@ module.exports = async function (uri) { path = structure.join('/'); // log progress - this.log('log', ['storage.delete.s3 >', uri]); + this.sdk.log(this, 'log', ['storage.delete.s3 >', uri]); // delete from aws await this.sdk.s3 @@ -42,7 +42,7 @@ module.exports = async function (uri) { path = structure.join('/'); // log progress - this.log('log', ['storage.delete.gs >', uri]); + this.sdk.log(this, 'log', ['storage.delete.gs >', uri]); // delete from gcp await this.sdk.gs.bucket(bucket).file(path).delete(path); @@ -51,7 +51,7 @@ module.exports = async function (uri) { return Promise.resolve(); } else { // log progress - this.log('log', ['storage.delete.local >', uri]); + this.sdk.log(this, 'log', ['storage.delete.local >', uri]); // delete file await deleteLocalFile(this, uri); diff --git a/src/index.js b/src/index.js index 71a0631..17c0ade 100644 --- a/src/index.js +++ b/src/index.js @@ -29,24 +29,27 @@ function StorageWrapper(config) { AWS.config.loadFromPath(config.s3); this.sdk.s3 = new AWS.S3(); - // enable logging - this.log = config.logging - ? (level, message) => { - if (message instanceof Array) { - message = message.join(' '); - } - - if (level == 'log') { - console.log(message); - } else if (level == 'warn') { - console.warn(message); - } else if (level == 'error') { - console.error(message); - } + // configure logging + this.config = { + logging: config.logging, + }; + + // set logging + this.sdk.log = (that, level, message) => { + if (message instanceof Array) { + message = message.join(' '); + } + + if (level == 'log') { + that.config.logging ? console.log(message) : null; + } else if (level == 'warn') { + that.config.logging ? console.warn(message) : null; + } else if (level == 'error') { + that.config.logging ? console.error(message) : null; } - : null; + }; - this.log('log', ['storage.index', 'loaded config >', JSON.stringify({ config })]); + this.sdk.log(this, 'log', ['storage.index', 'loaded config >', JSON.stringify({ config })]); } // enable utils diff --git a/src/list.js b/src/list.js index 14feaaa..4144893 100644 --- a/src/list.js +++ b/src/list.js @@ -49,7 +49,7 @@ module.exports = async function (uri) { path = structure.join('/'); // log progress - this.log('log', ['storage.list.aws >', uri]); + this.sdk.log(this, 'log', ['storage.list.aws >', uri]); // load file var next; @@ -75,7 +75,7 @@ module.exports = async function (uri) { path = structure.join('/'); // log request - this.log('log', ['storage.list.gcp >', uri]); + this.sdk.log(this, 'log', ['storage.list.gcp >', uri]); // load file file = await this.sdk.gs.bucket(bucket).getFiles({ @@ -86,7 +86,7 @@ module.exports = async function (uri) { return Promise.resolve(file[0]); } else { // log request - this.log('log', ['storage.list.local >', uri]); + this.sdk.log(this, 'log', ['storage.list.local >', uri]); // local file let file = await listLocalFiles(this, uri); diff --git a/src/load.js b/src/load.js index 2bcf096..8b984c8 100644 --- a/src/load.js +++ b/src/load.js @@ -25,8 +25,10 @@ module.exports = async function (uri) { bucket = structure.shift(); path = structure.join('/'); + console.log(this); + // log progress - this.log('log', ['storage.load.aws >', uri]); + this.sdk.log(this, 'log', ['storage.load.aws >', uri]); // load file let file = await this.sdk.s3 @@ -44,7 +46,7 @@ module.exports = async function (uri) { bucket = structure.shift(); path = structure.join('/'); - this.log('log', ['storage.load gcp >', uri]); + this.sdk.log(this, 'log', ['storage.load gcp >', uri]); // load file file = await this.sdk.gs.bucket(bucket).file(path).download(); @@ -56,7 +58,7 @@ module.exports = async function (uri) { uri.substr(0, 8).toLowerCase() == 'https://' ) { // log progress - this.log('log', ['storage.load.https >', uri]); + this.sdk.log(this, 'log', ['storage.load.https >', uri]); // public http(s) endpoint let file = await fetch(uri); @@ -71,7 +73,7 @@ module.exports = async function (uri) { // local file let file = await loadLocalFile(this, uri); - this.log('log', ['storage.load.local >', uri]); + this.sdk.log(this, 'log', ['storage.load.local >', uri]); return Promise.resolve(file); } diff --git a/src/move.js b/src/move.js index 2d814bc..a74c589 100644 --- a/src/move.js +++ b/src/move.js @@ -29,13 +29,17 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) { // move file within gcs if (keepOriginal != true) { // move is productive/ destructive - this.log('log', ['storage.move.gcp2gcp >', sourceUri, destinationUri]); + this.sdk.log(this, 'log', ['storage.move.gcp2gcp >', sourceUri, destinationUri]); // move file await this.sdk.gs.bucket(bucket).file(path).move(destinationUri); } else { // mode is dev, only copy file - this.log('log', ['storage.move.gcp2gcp (only copying) >', sourceUri, destinationUri]); + this.sdk.log(this, 'log', [ + 'storage.move.gcp2gcp (only copying) >', + sourceUri, + destinationUri, + ]); // copy file await this.sdk.gs.bucket(bucket).file(path).copy(destinationUri); @@ -55,7 +59,7 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) { path = structure.join('/'); // always copying - this.log('log', ['storage.move.aws2aws copying >', sourceUri, destinationUri]); + this.sdk.log(this, 'log', ['storage.move.aws2aws copying >', sourceUri, destinationUri]); // copy file await this.sdk.s3 @@ -69,7 +73,7 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) { // move file within gcs if (keepOriginal != true) { // move is productive/ destructive - this.log('log', ['storage.move.aws2aws deleting source >', sourceUri]); + this.sdk.log(this, 'log', ['storage.move.aws2aws deleting source >', sourceUri]); // parse source structure = sourceUri.substr(5).split('/'); @@ -89,7 +93,7 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) { return Promise.resolve(); } else { // any to any transfer - this.log('log', ['storage.move.any2any >', keepOriginal, sourceUri, destinationUri]); + this.sdk.log(this, 'log', ['storage.move.any2any >', keepOriginal, sourceUri, destinationUri]); // download file blob = await storage.load(sourceUri); @@ -101,7 +105,7 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) { if (keepOriginal != true) { await storage.delete(sourceUri); } else { - this.log('log', [ + this.sdk.log(this, 'log', [ 'storage.move.any2any not deleting sourceUri >', keepOriginal, sourceUri, diff --git a/src/save.js b/src/save.js index 97f1e94..91affc0 100644 --- a/src/save.js +++ b/src/save.js @@ -36,7 +36,7 @@ module.exports = async function (uri, buffer) { path = structure.join('/'); // log progress - this.log('log', ['storage.save.s3 >', uri]); + this.sdk.log(this, 'log', ['storage.save.s3 >', uri]); // upload to aws await this.sdk.s3 @@ -60,7 +60,7 @@ module.exports = async function (uri, buffer) { await saveLocalFile(this, tempFilePath, buffer); // log progress - this.log('log', ['storage.save.gs >', uri]); + this.sdk.log(this, 'log', ['storage.save.gs >', uri]); // upload file to gcs await this.sdk.gs.bucket(bucket).upload(tempFilePath, { @@ -78,7 +78,7 @@ module.exports = async function (uri, buffer) { // local file // log progress - this.log('log', ['storage.save.local >', uri]); + this.sdk.log(this, 'log', ['storage.save.local >', uri]); // save file let file = await saveLocalFile(this, uri, buffer);